🎓 A1Learner
🔍
0 of 50 lessons completed

HCL: Blocks, Arguments and Attributes

HCL is the language Terraform files are written in. Its building blocks are blocks, arguments and attributes.

A block in full

The word resource is the block type. The two labels name the type and the resource.

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = "Hello from Terraform."
}

An argument

The name sits on the left of the equals sign. The value sits on the right.

filename = "hello.txt"

An attribute

An attribute reads a value back. This one reads the filename of the hello file.

local_file.hello.filename

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "2.9.1"
    }
  }
}

resource "local_file" "hello" {
  filename = "hello.txt"
  content  = "Hello from Terraform."
}

# Commands:
# terraform init
# terraform validate
# terraform apply
# terraform destroy

Key points

  • HCL stands for HashiCorp Configuration Language.
  • A block is a group of settings, opened with braces.
  • A block starts with a type, such as resource, and often a label.
  • An argument is a named value inside a block, set with an equals sign.
  • An attribute is a value Terraform reads back from a resource after it is built.
  • Arguments set what you want. Attributes report what Terraform made.

The resource block is the example. Its type is local_file, its name is hello, and its arguments set the filename and content. Apply creates the file, and destroy removes it.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What are the named values inside a block called?