The Terraform block is the configuration block that is used in the terraform configuration file (example. tf) to state the required terraform version and provider version required to run your terraform configuration files (.tf)
Terraform Block Syntax
All the required settings to run the configuration file are mentioned in the terraform block below is the sample syntax of the terraform block.
terraform {
required_version = "value"
required_providers {
<Required provide> = {
version = ">Value"
source = "<Terraform registry>/Namespace/Provider>"
}
}
}
1. Terraform
This line signifies the beginning of a Terraform configuration block. Everything within this block defines the infrastructure you want to manage using Terraform.
2. Required_version = "value"
This line specifies the minimum Terraform version required to execute this configuration. Terraform checks your installed version against this value to ensure compatibility. Replace "value" with the actual required version number (e.g., required_version = ">= 1.2.0").
3. Required_providers
This block defines the infrastructure providers required for this configuration. Providers are plugins that interact with specific cloud platforms or services like AWS, Azure, GCP, etc.
4. <Required provider> = {
Replace <Required provider> with the actual name of the provider you need (e.g., aws = {} for AWS). This section defines configurations specific to that provider.
5. Version = ">Value"
This line specifies the minimum required version for the provider plugin. Terraform will attempt to find and use a compatible version from the Terraform Registry. Replace ">Value" with the desired minimum version constraint (e.g., version = ">= 3.80.0").
6. Source = "<Terraform registry>/Namespace/Provider"
This line specifies the location of the provider plugin. Terraform searches the Terraform Registry for the provider with the given details.
- <Terraform registry>: Replace this with the Terraform Registry URL (usually "registry.terraform.io").
- <Namespace>: This represents the namespace or organization that owns the provider plugin (e.g., "hashicorp").
- <Provider>: This is the name of the specific provider plugin (e.g., "was").
Passing Metadata to Providers
Using Terraform and AWS as the cloud provider, you are managing infrastructure across various environments (development, staging, and production).
In real time your terraform project structure looks like as the following:
terraform/
├── dev/
│ ├── main.tf
│ └── variables.tf
├── staging/
│ ├── main.tf
│ └── variables.tf
└── prod/
├── main.tf
└── variables.tf
The Terraform configuration files are located in a directory specific to each environment (dev, staging, and prod).
Provider Configuration and Metadata:
In each environment's main.tf, you configure the AWS provider along with metadata indicating the environment:
terraform {
provider_meta "aws" {
environment = "development" # or "staging" or "production" based on the environment
team = "devops" # This could be another metadata attribute
}
}
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "ExampleInstance"
Environment = "development" # or "staging" or "production"
}
}
Provider Metadata: The terraform block with provider_meta allows you to attach metadata to the AWS provider. In this example, you're indicating the environment and the team responsible for managing the resources.
Provider_meta "aws" Block
This block, nested within the terraform block, is specific to Terraform 0.13 and later versions. It allows you to provide metadata to the AWS provider plugin.
- aws: This keyword specifies that the metadata is intended for the AWS provider.
- environment = "development" (or "staging" or "production")
- This line defines a metadata key named "environment" and assigns a value based on your deployment environment (development, staging, production). This information could be helpful for the provider to perform environment-specific actions or configurations.
- You can add more metadata attributes here as defined by the AWS provider schema (if available). Consult the AWS provider documentation for supported metadata keys.
- Important note: Not all providers support provider_meta. Check the provider's documentation for compatibility.
Terraform uses the information to deploy resources with the correct environment tag when you apply the Terraform configuration for a particular environment (for example, terraform apply within the dev directory).
From Configuration to Initialization: How the Terraform Block Works with terraform init
Let's have a look at an example Terraform block utilising the WA provider. The provider version can be any version; in this case, I'm using the most recent one, as indicated by the blow syntax.
terraform {
required_version = "~> 1.8.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.49.0"
}
}
}
After you are applying the "terraform init" terraform will initialize the backed and downloads the required providers mentioned in the terraform block and it will be stored in the file called ".terraform.lock.hcl".

Initializing the backend... to Terraform has been successfully initialized!: These lines show the progress of the terraform init command. Terraform is initializing the backend, which is a storage mechanism for Terraform state (information about the resources managed by Terraform). It's then initializing provider plugins, which are plugins that allow Terraform to interact with specific cloud platforms or services (like AWS in this case). In this case, Terraform found and installed the hashicorp/aws version 5.49.0 provider plugin.
Terraform has created a lock file... to include this file in your version control repository: These lines explain that Terraform has created a lock file named .terraform.lock.hcl. This file records the selections of provider versions made during initialization. It's recommended to include this file in your version control system to ensure consistent infrastructure provisioning across deployments.
You may now begin working with Terraform... to all Terraform commands should now work.: These lines indicate that Terraform is now initialized and ready for use. You can proceed with writing your Terraform configuration to define your infrastructure and use commands like terraform plan to see the changes Terraform will make and terraform apply to actually create or modify the infrastructure resources.
People Also Ask
Terraform Cheat Sheet - Read
Similar Reads
What Is Terraform Lock FIle?
The terraform lock file is named "Terraform. lock.hcl" It will generated by the terraform itself and it will make sure that the same infrastructure will be created if multiple users are working. It serves as a central repository for the particular provider and module versions that you have used in y
5 min read
Modules Block in Terraform
Pre-requisite: Terraform Users can define and provision infrastructure resources declaratively using the open-source infrastructure as code (IaC) tool known as Terraform. It enables teams to effectively manage their infrastructure across a range of cloud providers and on-premises settings. The capab
6 min read
Terraform Provider Block
In Terraform, a "provider block" is a configuration block used to define the specific provider and its settings that Terraform will use to manage and interact with infrastructure resources. Providers are responsible for understanding API interactions and exposing resources. For example, AWS, Azure,
8 min read
What is Terraform Console Command ?
The terraform console command in Terraform opens an interactive console where you can evaluate and explore Terraform expressions and resource state in real-time.Why We Should use Terraform Console ?The terraform console command is a feature of the Terraform CLI that launches an interactive environme
5 min read
What is a Block in Programming?
In programming, a block is a set of statements that are grouped and treated as a single unit. Blocks are used to define the scope of variables, control the flow of execution in conditional statements and loops, and encapsulate code in functions, methods, or classes. Table of Content What is a Block
6 min read
What is Terraform Cloud?: Complete Tutorial
Terraform Cloud is HashiCorp's managed service for infrastructure automation that is engineered to help teams manage infrastructure with Terraform in a collaborative and efficient manner. Terraform Cloud provides a cloud-based platform where users can securely run, manage, and collaborate on Terrafo
11 min read
Terraform Backend Block
A backend block is used to specify where the Terraform state file which keeps track of all the infrastructure resources is stored in case of any changes. Normally, this state file is kept in a local instance, however, it may not be the best practice, especially for groups or large projects. In such
12 min read
Terraform Resources
A Terraform resource is like a building block in a blueprint for your infrastructure. Think of it as a specific piece of the infrastructure that you want to create, manage, or update. For example, it could be a virtual machine, a database, a storage bucket or a load balancer.When using Terraform, yo
13 min read
Introduction to Terraform
Many people wonder why we use Terraform when there are already so many Infrastructure as Code (IaC) tools out there. So, before learning Terraform, letâs understand why it was created.Terraform was made to solve some common problems with existing IaC tools. Some tools, like AWS CloudFormation, only
15 min read
Terraform for_each with Index
Terraform is currently one of the most popular tools for implementing infrastructure as code (IaC) for cloud and on-premises infrastructures. Its feature for_each loop, allows users to describe and manipulate many resources simultaneously. In contrast, in a configuration. Although at some times, it
11 min read