liveBook · Manning

Block type

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A block type is a Terraform HCL construct category that determines a block's purpose and how its contents are interpreted.

A block type is the first word in a Terraform HCL block. It determines the block’s purpose and how its labels, arguments, attributes, and subblocks are interpreted. Blocks act as Terraform’s nouns: they represent configuration or infrastructure that can be described and connected declaratively. section 2.2 section 2.2.1

Listing 2.7 HCL block components
type "label" {                            #1
  parameter1 = "value"                    #2
  parameter2 = "value"                    #3
  parameter3 = true                       #4
  parameter4 = 12                         #5
  parameter5 = {                          #6
    key = "value"
  }
 
  parameter6 = null                       #7
 
  subblock {                              #8
    sub_parameter1 = "value"
    sub_parameter2 = "value"
    sub_parameter3 = "value"
  }
 
  subblock {                              #9
    sub_parameter1 = "value"
    sub_parameter2 = "value"
    sub_parameter3 = "value"
  }
}

A generic HCL block shows the position of the type, optional label, arguments, attribute values, and repeated subblocks. section 2.2

The type comes before any quoted labels and the body enclosed in braces. Labels identify a subtype or instance, while arguments and subblocks modify the behavior defined by the type. section 2.2

Terraform HCL block types provide distinct configuration roles, including:

  • terraform: configures Terraform and the current workspace. section 2.2.1
  • provider: configures provider-specific settings. section 2.2.1
  • resource: creates and updates a corresponding infrastructure component. section 2.2.1
  • data: reads existing infrastructure without creating it. section 2.2.1
  • variable and locals: represent external inputs and module-scoped internal values. section 2.2.1
  • module: packages reusable HCL configuration. section 2.2.1

Connecting a data block to a resource

A data block looks up subnets, and a resource block creates an instance in the first returned subnet. The type establishes each block’s role; labels identify the AWS subtype and instance name; the resource references the data block’s ids attribute. section 2.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines block structure and catalogs major types, but does not detail each type’s complete arguments or behavior.
Examples
Uses generic HCL syntax plus AWS subnet lookup and instance resource configuration.
What it assumes
Assumes familiarity with Terraform HCL basics while defining block types, labels, arguments, and subblocks explicitly.
What you can do after
Evaluate Terraform HCL blocks by type, identify their labels and contents, and connect data lookups to resources.
How it connects
Connects HCL block types with labels, arguments, subblocks, resources, data sources, providers, and modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.