Skip to main content

Understand Infrastructure as Code (IaC) Concepts

·464 words·3 mins
Jack Warner
Author
Jack Warner
A little blog by me

Understand Infrastructure as Code (IaC) Concepts
#

Overview
#

HCL (HashiCorp Configuration Language) is a declarative language used by Terraform to define infrastructure resources.


HCL Resource Block
#

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

This file would be called local.tf.

Key Terminology
#

Term Description
Block Name The first keyword in the block (resource)
Resource Type Specifies the provider and resource (local_file)
Provider The first part of the resource type (local)
Resource The second part of the resource type (file)
Resource Name The user-defined name for this resource (pet)
Arguments Configuration parameters within the block (filename, content)

Note: A resource is an object that Terraform manages.


Provisioning a Resource
#

There are four steps to provision a resource:

  1. Write the configuration file.
  2. Initialize the directory.
  3. Plan the deployment.
  4. Apply the configuration.

Terraform Commands
#

Command Description
terraform init Checks all configuration files and downloads the necessary provider plugins.
terraform plan Shows the execution plan — a preview of the changes Terraform will make (resources created, modified, or destroyed).
terraform apply Executes the planned changes and provisions the resources defined in your configuration file.
terraform show Shows the current state of your infrastructure, including all provisioned resources and their attributes.

Updating and Destroying Infrastructure
#

resource "local_file" "pet" {
    filename = "/root/pets.txt"
    content  = "We love pets and animals!"
    file_permission = "0644"
}

Running terraform plan shows 1 to add — this is because we added a new argument called file_permission. When we run terraform apply, it will update the existing resource with the new argument.

Note: terraform apply will destroy and recreate the resource because we changed an argument that cannot be updated in place.

terraform destroy will destroy the resource defined in the configuration file. It will prompt you to confirm the destruction before proceeding. Once confirmed, Terraform will remove the resource from your infrastructure.


Multiple Resources
#

Terraform will create everything under the root folder. For example, given two separate resource files:

resource "local_file" "pet" {
    filename = "/root/pets.txt"
    content  = "We love pets!"
}
resource "local_file" "pet2" {
    filename = "/root/pets2.txt"
    content  = "We love pets and animals!"
}

When we run terraform apply, it will create both pets.txt and pets2.txt under the root folder.

You can also put both resources in a single file:

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

resource "local_file" "pet2" {
    filename = "/root/pets2.txt"
    content  = "We love pets and animals!"
}

This file, called main.tf, will create both pets.txt and pets2.txt under the root folder when we run terraform apply.


Common Terraform File Conventions
#

File Name Purpose
main.tf Main configuration file containing resource definitions
variables.tf Contains variable declarations
outputs.tf Contains outputs from resources
provider.tf Contains provider definition
terraform.tf Configures Terraform behaviour

Related