Terraform Configurations and the HashiCorp Language
1. Terraform Configurations and the HashiCorp Language
Let’s get started with an overview of Terraform configurations and the Hashicorp Language. This course focuses on the core Terraform workflow: author, initialize, plan, and apply. In this module, you’ll explore how these individual phases fit together to transform code to cloud resources. Let’s start with the author phase, where you write Terraform code in . tf files. Before writing code, start by creating directories for a Terraform configuration. A Terraform configuration is a complete document in the Terraform language that tells Terraform how to manage a given collection of infrastructure. The configuration is stored with a . tf extension. A Terraform directory can consist of multiple files and directories. A Terraform configuration consists of: A root module, also referred to as the root configuration file, And an optional tree for child modules. Child modules are optional, and can be variables, outputs, providers, and so forth. You can have a series of resources and other code constructs within a single root configuration, but the best practice is to logically separate your files. The root module is the working directory in which Terraform commands are run. In that directory, Terraform will look for any . tf files and use them to create a plan and infrastructure elements. Now that you know how to create a directory structure, let’s explore the language used for Terraform configurations. The language used to write configurations is the HashiCorp Configuration language, or HCL. Generic HCL syntax is shown in this example. HCL is used to create and manage API-based resources, predominantly in the cloud. Resources are infrastructure objects such as virtual machines, storage buckets, containers, and networks. Terraform uses HCL to define resources in your Google Cloud environment, create dependencies with those resources, and define the data to be fetched. Despite some commonalities, HCL is a configuration language, not a programming language. It’s a JSON-based variant that’s human and machine friendly. The simplicity of HCL makes Terraform accessible to developers. HCL includes a limited set of primitives such as variables, resources, outputs, and modules. It does not include any traditional statements or control loops. The logic is expressed through assignments, count, and interpolation functions. Let’s explore HCL syntax in detail. Blocks are lines of code that belong to a certain type. Examples include resource, variable, and output. A block can be simple or nested to include another block type. Arguments are part of a block and used to allocate a value to a name. Some blocks have mandatory arguments, while others are optional. Identifiers are names of an argument, block type, or any Terraform-specific constructs. Identifiers can include letters, underscores, hyphens, and digits, but cannot start with a digit. Expressions can be used to assign a value to an identifier within a code block. These expressions can be simple or complex. Comment syntax starts with a # for a single-line comment. Remember, HCL is declarative by nature, which means you define the end state of an infrastructure. Therefore the order of the blocks or files does not matter.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.