Terraform Meta Arguments
Terraform Meta Arguments are special configuration blocks used to define global settings and behaviors for a Terraform configuration. These arguments are specified within the terraform
block at the beginning of a Terraform configuration file.
Commonly used meta arguments include:
required_providers
: Specifies the required provider plugins and their versions.required_version
: Sets the minimum required version of Terraform.backend
: Configures the backend where Terraform state is stored.variable
: Defines input variables and their default values.output
: Declares output values generated by Terraform.
You can control various aspects of your Terraform environment and ensure compatibility with specific versions and provider dependencies. It helps in maintaining consistent infrastructure deployments and managing project requirements efficiently.
To work with Terraform, follow these steps:
Launch an instance where Terraform is installed and navigate to the
terraform-course/terraform-aws
directory.Create a new folder named "day-4-meta" and create a
main.tf
file inside it.
The main.tf
file should contain the following Terraform configuration:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "server" {
count = 5
ami = "ami-06ca3ca175f37dd66"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
}
}
After creating the main.tf
file, run terraform init
to initialize Terraform and install the required provider plugins. Make sure to export the access key ID and secret access key for authentication.
Next, run terraform plan
to preview the changes that Terraform will apply based on the configuration.
Before running terraform apply
, take note of the instances currently running to have a reference.
Finally, execute terraform apply
to provision 5 EC2 instances in the "us-east-1" region. Each instance will use the specified AMI and have a unique name based on the count.
To extend the configuration and create instances with different AMIs for each name, we can modify the code as follows:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
}
locals {
instances_name = {
"Rohit" = "ami-06ca3ca175f37dd66"
"Balgovind" = "ami-06ca3ca175f37dd66"
"Saurav" = "ami-04132f301c3e4f138"
"Suri" = "ami-04132f301c3e4f138"
"Rahul" = "ami-04132f301c3e4f138"
}
}
resource "aws_instance" "server" {
for_each = toset(keys(local.instances_name))
ami = local.instances_name[each.key]
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
In this configuration, we use keys(local.instances_name)
to extract the keys from the instances_name
object. The resulting list of keys is converted to a set using toset()
. We then access the corresponding AMI value using local.instances_name[each.key]
.
Upon running terraform apply
, the provisioned instances will have different AMIs based on the names specified in the instances_name
local variable.
Stay tuned for more exciting learnings on our Terraform journey! In the next blog, I'll start pushing the codes to the git repository.
#Terraform #InfrastructureAsCode #DevOps #LearningTerraform #HCL