HCL Syntax
One of the key features of Terraform is its HCL syntax, which stands for HashiCorp Configuration Language. HCL is structured configuration syntax developed and created by HashiCorp, which they use across a range of their product suite.
It is used to define the configuration and services of your infrastructure in a single format to ensure a consistency across multiple products and platforms.
Variables and Data Types
Section titled “Variables and Data Types”Terraform HCL supports several data types, including string, number, bool, list, and map. You can use these data types
to define variables that hold values for your infrastructure resources. Here’s an example of defining a variable in
Terraform HCL:
variable "region" { type = string default = "us-west-2"}This example defines a variable named region of type string with a default value of us-west-2. You can reference this
variable later in your Terraform code as var.region. The ${var.region} interpolation syntax is only needed inside
quoted strings, such as "my-bucket-${var.region}".
Blocks and Attributes
Section titled “Blocks and Attributes”In Terraform HCL, you define infrastructure resources using blocks and attributes. A block is a container for one or more attributes, and each attribute defines a property of the resource. Here’s an example of defining an AWS S3 bucket in Terraform HCL:
resource "aws_s3_bucket" "my_bucket" { # S3 buckets are private by default bucket = "my-bucket"}In this example, we define an AWS S3 bucket named my_bucket with a bucket name of my-bucket. The aws_s3_bucket block is the resource block that creates the S3 bucket, and my_bucket is the name we use to reference it in our configuration.
Expressions
Section titled “Expressions”Terraform HCL supports expressions that allow you to manipulate and combine values. For example, you can use expressions to concatenate strings, perform arithmetic operations, and access nested attributes. Here’s an example of using an expression to concatenate strings:
resource "aws_s3_bucket" "my_bucket" { # S3 buckets are private by default bucket = "my-bucket-${var.region}"}In this example, we use the ${var.region} expression to concatenate the region variable value with the bucket name to
create a unique name for the S3 bucket.
Functions
Section titled “Functions”Terraform HCL also supports functions that allow you to perform complex operations on values. For example, you can use
the format function to format strings, the lookup function to retrieve values from a map, and the cidrsubnet function to
calculate CIDR subnets. Here’s an example of using the format function to format a string:
resource "aws_security_group_rule" "allow_all_traffic" { type = "ingress" from_port = 0 to_port = 0 protocol = "tcp" cidr_blocks = [format("%s/0", var.vpc_cidr_block)]}In this example, we use the format function to format the vpc_cidr_block variable value into a CIDR block format with a
/0 subnet mask.
Enforcing Consistent Configuration Style
Section titled “Enforcing Consistent Configuration Style”Terraform HCL provides flexibility in how you layout elements such as resources and attributes in your configuration. HCL does have style conventions for ease of readability and consistency in configuration across files and modules developed by teams.
The command below formats the terraform code in guidance with the style conventions. This is applied across all files and sub-folder files in your working directory.
terraform fmtPassing the -recursive flag will process files in subdirectories. By default, only the current directory is processed.
Conclusion
Section titled “Conclusion”This section covered the core building blocks of HCL: variables and data types, blocks and attributes, expressions, and functions. Together, these let you define infrastructure resources as code and keep your configurations readable and consistent across your project.