Understanding Terraform Configuration language(HCL)

#TerraWeek Challenge by TWS Community Builders

·

4 min read

1.What is HCL?

Hashicorp Configuration Language (HCL) is the language used to write configuration files for Terraform. It is a rich language designed to be relatively easy for humans to read and write. HCL describes the desired state of infrastructure resources.

This article will cover the syntax and structure of HCL, variables, data types, and operators in HCL, and how to write Terraform code with HCL.

2.Familiarize yourself with the HCL syntax used in Terraform

HCL is a declarative language that focuses on defining the desired state of infrastructure resources. The syntax of HCL is similar to that of other configuration languages like JSON, YAML, and XML.

The Terraform language syntax is built around two key syntax constructs: arguments and blocks. To write Terraform code with HCL, you will typically create a file with a .tf extension.

Blocks:

A block in HCL is a container for one or more attributes that define a resource. A block is identified by its name, which is usually the name of the resource being defined. For example, a block that creates a folder in your system might look like this:

In this example, the block name is local_file, and the block type is the resource. The block also has a label "test" which is a reference name for this particular resource.

Now there are different types of blocks:

i) Resource Block:

A resource block declares a resource of a given type with a given local name. The name is used to refer to this resource from elsewhere in the same Terraform module. The identifiers mentioned within the scope of a resource block have no significance outside the block.

Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records.

Example:

ii) Terraform Block:

The “terraform” block is used to specify settings for the Terraform execution environment, such as the required Terraform version, required Terraform providers and any backend configuration settings.

Example:

iii) Provider Block:

A provider block specifies the details of the provider being used. The provider is responsible for creating and managing resources in a specific infrastructure. The provider block is required for every Terraform configuration file.

Example:

There are many other blocks like Data block, Module block, Output block, Variable block etc...

Arguments:

An argument is a key-value pair. For example,

Name = Sudipa,

Role = DevOps

so here Name is the key and the value is Sudipa, This is called an Argument.

In HCL, a syntax construct that assigns a value to a name. Arguments have the form <IDENTIFIER> = <EXPRESSION>, and they appear within blocks.

3.Understand variables, data types, and operators in HCL

Variables: In general-purpose programming, a variable is a symbolic name associated with a value. In HCL, variables are used to store values that can be used throughout the configuration. Variables are defined using the variable block and can have a default value or can be set from the command line when running Terraform. Here's an example of a variable block:

variable "instance_count" {
  type    = number
  default = 1
}

In this example, we define a variable called instance_count with a default value of 1 and a type of number.

Now let's Create variables.tf file and define a variable then use the variable in a main.tf file.

In the below image I have declared ami and instance_type in variables.tf and called both in main.tf as var.ami and var.instance_type.

And we can see the AWS instance is created successfully.

Data Types:

HCL has several built-in data types, including:

  • String: a sequence of characters enclosed in double quotes, like "hello world"

  • Number: an integer or floating-point number, like 42 or 3.14

  • Boolean: a value that can be either true or false

  • List: an ordered collection of values, like [1, 2, 3]

  • Map: an unordered collection of key-value pairs, like {name = "John", age = 30}

Operators:

HCL has a variety of operators that can be used to perform operations on values. Some of the most common operators include:

  • +, -, *, /: arithmetic operators for addition, subtraction, multiplication, and division

  • &&, ||, !: logical operators for AND, OR, and NOT operations

  • \==, !=, <, \>, <=, \>=: comparison operators for equality and inequality, and less than or greater than comparisons.

4.Writing Terraform configurations using HCL syntax

Let's see a basic example of creating a Docker container via Terraform.

Input:

Here I have mentioned required_providers(Docker) under the terrafrm_block.

Then under resource_block, I created the image and container.

Execution:

Now we have written the code, let's see how to execute it.

To execute a main.tf file we need to run the below commands:

terraform init -> terraform plan -> terraform apply

Terraform init will initialize the providers.

Terraform plan command to see the execution plan and what resources will be created and how they will be configured.

Then we have to enter Terraform apply command, the actual execution will happen through this.

Below is the output:

Github Link:

https://github.com/SudipaDas/TerraWeek.git

If this post was helpful, please follow and click the 💚 button below to show your support.

_ Thank you for reading!

_Sudipa