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
