Types of Blocks in Terraform
Types of Blocks in Terraform
Terraform is an infrastructure as a code tool that allows you to define and configure your
infrastructure using your declarative code. Its syntax is very simple. Terraform uses a
configuration language to describe the desired state of your infrastructure, and this language
is composed of various block types. These block types play a crucial role in defining and
configuring infrastructure in Terraform.
In Terraform, a block is a fundamental unit used to define and configure different aspects of
your infrastructure. Blocks are written in HashiCorp Configuration Language (HCL) and
allow you to declare resources, providers, variables, outputs, and other configuration
elements within your Terraform code.
Each block serves a specific purpose and has its own syntax and set of properties. Here are
some common block types used in Terraform:
Terraform Block
• It is used to define global configuration and behaviour for terraform execution
• Setting the required Terraform version.
• Configuring the backend for storing the state file.
• Defining experimental or optional features.
• Specifying variables used across multiple modules or configurations.
Example:
Provider block
Provider blocks specifies special type of module that allows Terraform to interact with various
cloud-hosting platforms or data centers. Providers must be configured with proper credentials
before we can use them. In previous article we exported access key into our environment and
that allowed us to deploy resources. Versions and download locations of providers are often
specified inside the terraform block, but you can also specify it inside this block as well.
Resource Block
Resource blocks are used to manage resources such as compute instances, virtual networks,
databases, buckets, or DNS resources. This block type is the backbone of any terraform
configuration because it represents actual resources with majority of other block types playing
supporting role.
Variable Block
This block is often called an input variable block. Variable block provides parameters for
terraform modules and allow users to customize the data provided to other terraform modules
without modifying the source.
Variables are often in their own file called variables.tf. To use a variable, it needs to be
declared as a block. One block for each variable.
Terraform has a strict order of precedence for variable setting. Here it is, from highest to
lowest:
1. Command line (-var and var-file)
2. *.auto.tfvars or *auto.tfvars.json
3. terraform.tfvars.json
4. terraform.tfvars file
5. Env variables
6. Variable defaults
Locals Block
Often called local variables block, this block is used to keep frequently referenced values or
expressions to keep the code clean and tidy.
Locals block can hold many variables inside. Expressions in local values aren not limited to
literal constants. They can also reference other values in the module to transform or combine
them. These variables can be accessed using local.var_name notation, note that it is
called local. when used to access values inside.
Data Block
Data block's primary purpose is to load or query data from APIs other than Terraform's. It can
be used to provide flexibility to your configuration or to connect different workspaces. One
way we would use data block in future articles is to query AWS API to get a list of active
Availability Zones to deploy resources in.
Data is then accessed using dot notation using var identifier. For example: var.variable_1
Module Block
Modules are containers for multiple resources that are used together. A module consists
of .tf and/or .tf.json files stored in a directory. It is the primary way to package and reuse
resources in Terraform.
Every Terraform configuration has at least one model (root module) which contains resources
defined in the .tf files. Test configuration we created in the third part of these series is a
module.
Modules are a great way to compartmentalize reusable collections of resources in multiple
configurations.
Here is an example of a module:
Output Block
This is a block which is almost always present in all configurations, along
with main.tf and variables.tf block. It allows Terraform to output structured data about your
configuration. This output can be used by users to see data like IPs or resources names in one
convenient place. Another use case involves using this data in other Terraform workspace or
sharing data between modules.
Provisioner Block
Provisioners allows us to specify actions to be performed on local or remote machines to
prepare resources for service.
There are two types of Terraform provisioners: local-exec and remote-exec.
local-exec invokes local executable after a resource is created. It runs the process on the
machine running Terraform, meaning the machine where you run terraform apply. This is
most likely your own computer.
remote-exec invokes remote executable, something like an EC2 instance on AWS.
This is an example of a provisioner for an EC2 instance. This example contains both 'local-
exec' and a remote-exec: