HCL Basics: Clear Guide to Terraform Syntax

HCL Basics form the foundation for writing infrastructure as code with Terraform. If you’re new to Terraform or just getting started with infrastructure automation, understanding HashiCorp Configuration Language (HCL) is essential. In this guide, we’ll break down the core concepts of HCL in a way that’s easy to grasp—whether you’re a beginner or looking to refresh your knowledge.

What Is HCL and Why Use It?

HashiCorp Configuration Language, or HCL, is a human-readable configuration language designed specifically for use with Terraform. It’s simple, structured, and flexible—making it easier to manage cloud and on-prem infrastructure.

Terraform uses HCL to define infrastructure resources in configuration files with a .tf extension. These files declare what infrastructure should look like rather than describing how to create it, which aligns with Terraform’s declarative nature.

By understanding HCL basics, you can start creating reusable, version-controlled configurations to automate everything from VMs to databases.

The Structure of an HCL File

An HCL file is made up of blocks and arguments.

  • Blocks: These are the top-level structures defined with curly braces {}.
  • Arguments: Key-value pairs inside blocks that specify the behavior or values for that resource.

A typical Terraform configuration includes a resource block that defines a specific component like a file, virtual machine, or cloud storage bucket.

Example:

resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "We love pets"
}

In this example:

  • local_file is the resource type.
  • pet is the logical name given to the resource.
  • filename and content are arguments specific to the local_file resource type.

Understanding Resource Blocks in HCL

Every resource in Terraform is represented as a block in HCL. The resource keyword begins the block, followed by the resource type and a unique name.

The resource type usually follows the format provider_resource. For instance, in local_file, “local” is the provider and “file” is the resource. Terraform uses this to determine which provider plugin to download during initialization.

Each resource type requires specific arguments. For local_file, the filename is required, while others like content are optional or context-specific.

Understanding HCL basics means being able to identify the provider, the resource type, and supply the correct arguments within each block.

Creating a File with HCL and Terraform

Let’s walk through a simple use case: creating a text file locally.

Create a directory for your Terraform project:

mkdir ~/terraform-local-file
cd ~/terraform-local-file

Create a file called local.tf and add the following configuration:

resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "We love pets"
}

Run the Terraform workflow:

terraform init   # Downloads provider plugin
terraform plan # Shows execution plan
terraform apply # Applies changes

Confirm the file was created:

cat /root/pets.txt

This hands-on example helps solidify your understanding of HCL basics and how resource blocks work.

Exploring Other Resource Types in HCL

Besides local files, Terraform supports a wide variety of resource types using different providers. Examples include:

  • aws_instance for EC2 virtual machines in AWS
  • aws_s3_bucket for S3 storage buckets
  • azure_virtual_machine for Azure VMs
  • google_compute_instance for GCP instances

Each resource has its own set of required and optional arguments. For instance, aws_instance typically requires an AMI ID and instance type, while aws_s3_bucket may need a bucket name and access control settings.

To effectively use HCL, it’s important to refer to the official Terraform documentation. This helps you identify what arguments are needed for each resource type and how to use them correctly.

How Terraform Interprets HCL Files

When you run terraform init, the tool reads your HCL file and identifies the providers required. It downloads the necessary plugins and sets up the working directory.

Next, running terraform plan shows you what actions will be taken to match the real-world infrastructure to the desired state defined in your HCL file.

Finally, terraform apply carries out those changes. The tool will prompt you to confirm before proceeding.

These commands form the core workflow and help you safely manage resources defined using HCL.

Navigating Provider Documentation

Terraform supports hundreds of providers, and each one has a different set of resources and configurations. For example:

  • The local provider supports only local_file.
  • The AWS provider supports over 300 resources including compute, networking, and storage.

To find which arguments are needed for a specific resource, visit the Terraform Registry. This official documentation outlines all supported providers, resource types, arguments, and examples.

When working with HCL basics, documentation becomes your best ally. It ensures your configurations are valid and optimized.

Conclusion

Mastering HCL basics is the first step to becoming confident with Terraform. From understanding blocks and arguments to creating your first local resource, this foundational knowledge allows you to write effective and reusable configuration files.

Once you’re comfortable with HCL, you’ll be ready to move on to more advanced concepts like modules, variables, and state management. And remember—practice is key. The more you write and run HCL configurations, the more natural it becomes.

FAQ — HCL Basics

Q1: What is HCL used for in Terraform?

HCL (HashiCorp Configuration Language) is used to define infrastructure in a declarative way for Terraform.

Q2: Can I use JSON instead of HCL?

Yes, Terraform supports JSON, but HCL is preferred due to its simplicity and readability.

Q3: Do all resource types use the same arguments?

No. Each resource type has its own set of required and optional arguments depending on the provider.

Q4: Is HCL case-sensitive?

Yes. HCL is case-sensitive, so be careful when writing resource types and arguments.

Q5: Where can I learn more about HCL syntax?

The Terraform documentation is the best source for accurate, up-to-date information.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top