Skip to main content
In this article, you’ll learn the fundamentals of HCL—the language used to define infrastructure with Terraform. We demonstrate HCL syntax, create a simple local file resource, and walk through essential Terraform commands to initialize, plan, and apply configurations.

Understanding HCL Syntax

HCL files consist of blocks and arguments. Blocks are defined using curly braces and contain key-value pair arguments that represent configuration data. In Terraform, each block describes a specific aspect of your infrastructure and lists the resources you wish to create. For instance, you might want to create a file on the local system where Terraform is installed. First, create a directory for your configuration file in the /root directory:
Within this directory, create a configuration file (e.g., local.tf) and define a generic block structure:

Creating a Local File Resource

Next, define a resource block in local.tf to create a local file. Inside the block, specify the file name and content using block arguments:

Breaking Down the Configuration

  1. Block Identification
    The block starts with the resource keyword and is identified by curly braces. It consists of three parts:
    • Resource Type:
      local_file indicates that the local provider is used.
    • Resource Name:
      The logical name pet uniquely identifies this resource.
    • Block Arguments:
      These key-value pairs specify resource parameters. For example:
      • filename sets the absolute path /root/pets.txt where the file is created.
      • content provides the text content for the file.
  2. Resource Type Requirements
    The local_file resource requires the arguments filename and content. When working with other providers such as AWS, Azure, or GCP, different resource types may require a different set of arguments. Consult Terraform’s documentation for details on the necessary arguments for each resource type.
Below are additional examples for other providers:

AWS EC2 Instance Example

AWS S3 Bucket Example

Terraform Workflow

A typical Terraform workflow involves the following steps:
  1. Write the Configuration File:
    Create and edit your Terraform configuration file (e.g., local.tf).
  2. Initialize the Working Directory:
    This step checks your configuration file and downloads the necessary provider plugins.
When running terraform init, Terraform identifies the use of the local provider based on your resource configuration.
Example output:
  1. Review the Execution Plan:
    Use the terraform plan command to see the proposed actions before applying changes.
    The output provides a diff-like summary showing what will be created, modified, or destroyed. For example, a plus symbol (+) next to the local_file.pet resource indicates that it will be created. Example excerpt:
  2. Apply the Configuration:
    Execute the following command to apply the configuration and create the resource:
    Confirm the execution by typing yes when prompted. Example output:
After applying the configuration, verify the creation of the file using the cat command or inspect the resource details with:
Example terraform show output:

Reviewing the Configuration

Let’s recap the key components of the local.tf file:
  • Resource Block: Uses the local_file resource type (local provider) to create a file.
  • Required Arguments: The filename and content arguments are mandatory.
  • Optional Arguments: Directory and file permissions can also be specified and are evident during the plan stage.
For additional resource types and their required arguments, refer to the Terraform documentation.
The image shows a diagram and text detailing a provider's argument reference, including optional and required arguments for file creation and permissions.

Conclusion

In this article, we explored the basics of HCL by creating our first Terraform resource. We discussed HCL syntax, detailed the configuration for a local file resource, and walked through the Terraform workflow—from initialization and planning to applying changes. Now it’s time to dive into hands-on labs and further your understanding of HCL and Terraform. Happy coding!

Watch Video