Skip to content

Template 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.

In this section, we will explore the basics of Terraform HCL syntax and how you will use it to create, modify, and manage your infrastructure resources!

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 by using the ${var.region} syntax.

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" {
bucket = "my-bucket"
acl = "private"
}

In this example, we define an AWS S3 bucket named my_bucket with a bucket name of my-bucket and an Access Control List (ACL) of private. The aws_s3_bucket block is the resource block that creates the S3 bucket, and my_bucket attribute is the name of the bucket.

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" {
bucket = "my-bucket-${var.region}"
acl = "private"
}

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.

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.

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.

Terminal window
terraform fmt

Passing the -recursive flag will process files in subdirectories. By default, only the current directory is processed.


Terraform HCL syntax provides a powerful and easy-to-use way to define infrastructure resources as code. With HCL, you can define variables, blocks, attributes, expressions, and functions to create, modify, and manage your cloud infrastructure resources programmatically.