Meta-Arguments in Terraform

Meta-Arguments in Terraform

·

5 min read

When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage several of the same resources, you can use either count or for_each, which removes the need to write a separate block of code for each one. Using these options reduces overhead and makes your code neater.

count is what is known as a ‘meta-argument’ defined by the Terraform language. Meta-arguments help achieve certain requirements within the resource block.

Count

The count meta-argument accepts a whole number and creates the number of instances of the resource specified.

When each instance is created, it has its own distinct infrastructure object associated with it, so each can be managed separately. When the configuration is applied, each object can be created, destroyed, or updated as appropriate.

eg.

 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 = 4
        ami = "ami-0c7217cdde317cfec"
        instance_type = "t2.micro"

        tags = {
                Name = "Server ${count.index}"
}
}

for_each

Like the count argument, the for_each meta-argument creates multiple instances of a module or resource block. However, instead of specifying the number of resources, the for_each meta-argument accepts a map or a set of strings. This is useful when multiple resources are required that have different values. Consider our Active directory groups example, with each group requiring a different owner.

 terraform {

required_providers {

aws = {

source = "hashicorp/aws"

version = "~> 4.16"

}

}

required_version = ">= 1.2.0"

}



provider "aws" {

region = "us-east-1"

}



locals {

ami_ids = toset([

"ami-0b0dcb5067f052a63",

"ami-08c40ec9ead489470",

])

}



resource "aws_instance" "server" {

for_each = local.ami_ids



ami = each.key

instance_type = "t2.micro"

tags = {

Name = "Server ${each.key}"

}

}



Multiple key value iteration

locals {

ami_ids = {

"linux" :"ami-0b0dcb5067f052a63",

"ubuntu": "ami-08c40ec9ead489470",

}

}



resource "aws_instance" "server" {

for_each = local.ami_ids



ami = each.value

instance_type = "t2.micro"



tags = {

Name = "Server ${each.key}"

}

}

Task-01

Create the above Infrastructure as code and demonstrate the use of Count and for_each.

Meta argument- count

The first part of the code is called the "terraform" block. Here, we say what version of Terraform we need and which providers are necessary. In this case, we're asking for the AWS provider, and we want it to come from the "hashicorp/aws" module with a version of 4.16 or higher.

Next, we have the "provider" block, where we set up the configuration for the AWS provider. In this example, we're specifying the region as ap-south-1.

After that, we get to the "aws_instance" part, which is like a recipe for creating EC2 instances. We use the "count" thing to make four instances, each with a specific AMI and instance type. The "tags" part helps give each instance a unique name based on its count.

To get things started, run "terraform init" to set up the Terraform project.

Then, run "terraform plan".

Finally, run "terraform apply" to actually create those four instances.

Congratulations! You've just made four new EC2 instances successfully.

Meta argument - for_each

Start by creating a "locals" section to gather different AMI IDs for our instances. We use the "toset" function to change a list of names into a unique set.

In the "resource" part, set up the "aws_instance" resource and use the "for_each" method to make an instance for each AMI ID in our "ami_ids" map. The "ami" is set to the current key (the AMI ID), and the "instance_type" is set to "t2.micro." We also use the "tags" to give each instance a unique name based on the current key.

Run the Terraform apply command.

See two new instances created successfully, thanks to a simple process of going through different sets of information.

Now, let's use the "for_each" method with a map containing various key-value pairs. This helps us create instances with different AMIs. We have a map with two pairs: the key is the AMI's name, and the value is its actual ID.

In the "resource" part, we use "each.value" to get the current value (AMI ID) and set the "ami" to it. Also, we use "each.key" to get the current key (AMI name) and use it to give a unique name to each instance using "tags."

Using "for_each" with maps is handy to make several instances with different settings in just one block of code.

Run Terraform apply.

Now, observe two new instances made using the 'for_each' method with maps. It simplifies handling various sets of information.

Write about meta-arguments and its use in Terraform.

Meta-arguments in Terraform are special settings that help decide how resources get handled—whether they get made, updated, or taken away. They're not just for one type of resource; instead, they let you set things up for all your resources in Terraform.

The main job of meta-arguments is to handle connections between resources. For example, with the "depends_on" meta-argument, you can say that one resource needs another to be there first. This comes in handy when you're making resources that rely on others, like a load balancer needing an auto-scaling group.

Another way to use meta-arguments a lot is to control the order in which resources are made. The "count" and "for_each" meta-arguments help make many instances of a resource. Also, the "lifecycle" meta-argument helps decide when resources get made, updated, or taken away.

The "provider" meta-argument is important too. It says which provider manages a specific resource. Providers are like plugins that Terraform uses to manage resources, such as AWS, Google Cloud, or Azure.

In short, meta-arguments are a handy feature in Terraform, letting you finely control how resources behave. Using these settings well helps Terraform users build stronger and more flexible infrastructure through code.


Thanks for reading until here. See you in the next one.