HCL Language – 2 – HCL Syntax Fundamentals

HCL Syntax Fundamentals

HashiCorp Configuration Language (HCL) is a domain-specific language developed by HashiCorp for configuring infrastructure as code. HCL is commonly used with tools like Terraform, Packer, and Consul to define and manage infrastructure resources. Understanding the fundamental syntax of HCL is essential for creating and maintaining these configurations.

Declarations and Assignments

In HCL, configurations are expressed using a series of declarations and assignments. Here’s how you declare and assign values in HCL:


resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
    

In this example, we declare an AWS EC2 instance resource named “example.” We assign values to properties like “ami” and “instance_type.”

Comments

HCL allows for comments using the “#” symbol. Comments are ignored during configuration parsing and are useful for adding explanations or notes within your code.


# This is a comment
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
    
Blocks

HCL configurations are organized into blocks. Blocks define resources, variables, or modules. They are enclosed in braces “{}” and consist of key-value pairs. The block type (“resource,” “variable,” etc.) is followed by a label and a block of key-value assignments.


resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}
    

In this example, we have a resource block for an AWS EC2 instance with the label “example.” It includes key-value assignments for properties like “ami” and “instance_type.”

Variables and Interpolation

HCL allows you to define variables and interpolate their values into your configurations. Variables are declared with the “variable” block, and their values can be interpolated using the “${}” syntax.


variable "region" {
  description = "AWS region"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  availability_zone = "${var.region}-a"
}
    

Here, we define a variable “region” and interpolate its value into the “availability_zone” property of the AWS instance resource.

Conditional Expressions

HCL supports conditional expressions that enable you to make decisions based on conditions. These expressions use the “count” and “for_each” arguments to control resource creation and configuration.


resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  count         = var.create_instances ? 3 : 0
}
    

In this example, the number of instances created is controlled by the “create_instances” variable, which is evaluated using a conditional expression.

Resource Blocks and References

HCL configurations often involve creating resources and referencing them. You can reference resources within the same configuration by using the resource type and label.


resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

resource "aws_security_group" "example_sg" {
  name = "${aws_instance.example.name}-sg"
}
    

In this example, the security group resource references the EC2 instance by using “${aws_instance.example.name}” to construct the group’s name based on the instance’s name.

Conclusion

Understanding the syntax fundamentals of HCL is crucial for effectively using it in tools like Terraform to define infrastructure configurations. With declarations, comments, blocks, variables, and conditional expressions, you can create complex and dynamic infrastructure as code.