Open In App

What is Terraform Block?

Last Updated : 20 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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".

terraform-init

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


Next Article
Article Tags :

Similar Reads