Skip to main content

Understand Infrastructure as Code (IaC) Concepts

·496 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
#

TermDescription
Block NameThe first keyword in the block (resource)
Resource TypeSpecifies the provider and resource (local_file)
ProviderThe first part of the resource type (local)
ResourceThe second part of the resource type (file)
Resource NameThe user-defined name for this resource (pet)
ArgumentsConfiguration 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
#

CommandDescription
terraform initChecks all configuration files and downloads the necessary provider plugins.
terraform planShows the execution plan — a preview of the changes Terraform will make (resources created, modified, or destroyed).
terraform applyExecutes the planned changes and provisions the resources defined in your configuration file.
terraform showShows 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 NamePurpose
main.tfMain configuration file containing resource definitions
variables.tfContains variable declarations
outputs.tfContains outputs from resources
provider.tfContains provider definition
terraform.tfConfigures Terraform behaviour

Related notes#

Part of my Terraform Associate certification series:

  1. Understand Infrastructure as Code (IaC) Concepts (this note)
  2. Terraform Provider Basics
  3. Variables, Resource Attributes, and Dependencies
  4. Terraform State
  5. Using the Terraform CLI

Related

CronJob

·161 words·1 min