What are Meta-Arguments?
Meta-arguments
are special arguments used to modify the behavior of resources or blocks. They provide additional functionality beyond the standard resource configuration.
Meta-arguments
allow you to customize resource behavior, define dependencies between resources, control resource lifecycle, and associate providers with resources or blocks. It helps achieving certain requirements within the resource block.
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.
What is 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.
What is 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.
Use Cases of Meta-Arguments
Some of the use cases of meta arguments are: -
Dynamic resource creation: Use
count
to create a variable number of instances based on conditions.Managing dependencies:
depends_on
ensures resources are created or modified in the correct order.Fine-grained lifecycle management:
lifecycle
controls resource behavior during updates or replacements.Multi-cloud provider support:
provider
associates different providers with specific resources, enabling multi-cloud management.Custom provisioning and configuration:
provisioner
executes scripts or commands during resource creation or destruction, allowing custom actions.
TASK : Create the IaC and demonstrate the use of Count and for_each
A. Let's first move ahead with count
meta-argument.
Step 1: Create a terraform.tf
file to declare AWS providers required for the system.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
required_version = ">= 1.2.0"
}
Step 2: Create a provider.tf
file to specify the AWS region required for instance.
provider "aws" {
region = "us-east-1"
}
Step 3: Create a main.tf
and pass the details of AWS instance. Using the count meta argument you can name the instances as per the number of counts you have passed on.
resource "aws_instance" "server" {
count = 2
ami = "ami-0c7217cdde317cfec"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
}
}
Step 4: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init
Step 5: Once you initialize all the plugins required for AWS, now execute the terraform plan
which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.
Step 6: Finally, use the command terraform apply
it will apply the changes to create or update resources as needed.
Step 7: You can verify, 2 new EC2 instances were successfully created and named as per the count argument.
Step 8: Once you are done with the newly created instance we can use terraform destroy
command which will delete the complete infrastructure.
B. Let's now create infrastructure with for_each
meta-argument.
Step 1: We will use the same terraform.tf
and provider.tf
file.
Step 2: Edit main.tf
file and pass the details of AWS Instance. Using the for_each meta argument you can name the instances as per iteration of the locals ami ids.
locals {
ami_ids = toset([
"ami-022e1a32d3f742bd8",
"ami-053b0d53c279acc90",
])
}
resource "aws_instance" "server" {
for_each = local.ami_ids
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
Step 3: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init
Step 4: Once you initialize all the plugins required for AWS, now execute the terraform plan
which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.
Step 5: Finally, use the command terraform apply
it will apply the changes to create or update resources as needed.
Step 6: You can see 2 new instances were successfully created and named after ami id as we are using for_each.
Step 7: Once you are done with the newly created instance we can use terraform destroy
command which will delete the complete infrastructure.
C. Meta argument: multiple key-value iterations.
Step 1: We will use the same terraform.tf
and provider.tf
file.
Step 2: Create a main.tf
and pass the details of AWS providers and aws instance details. Using the for_each meta argument you can name the instances as per iteration of the locals ami ids but here we are passing both key: value pairs.
# Multiple key value iteration
locals {
ami_ids = {
"amazonlinux" :"ami-022e1a32d3f742bd8",
"ubuntu": "ami-053b0d53c279acc90",
}
}
resource "aws_instance" "server" {
for_each = local.ami_ids
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
Step 3: Now the first step is to initialize the working directory with the necessary plugins and modules by executing terraform init
Step 4: Once you initialize all the plugins required for AWS, now execute the terraform plan
which will create an execution plan by analyzing the changes required to achieve the desired state of your infrastructure.
Step 5: Finally, use the command terraform apply
it will apply the changes to create or update resources as needed.
Step 6: You can see two new instances were successfully created by taking the name using key values of ami ids.
Step 7: Once you are done with the newly created instance we can use terraform destroy
command which will delete the complete infrastructure.
Conclusion
In Terraform, meta-arguments
such as count
and for_each
provide powerful ways to dynamically manage resources and configurations.
Count Meta-Argument: Allows for creating multiple instances of a resource based on a numeric count, offering scalability and flexibility in managing infrastructure. It's useful when you need a fixed number of identical resources.
For_Each Meta-Argument: Enables the creation of resources based on a map or set of objects, providing more granular control over resource configurations. It's beneficial for scenarios where resources have varying configurations or when working with dynamic data.
In conclusion, meta-arguments
like count and for-each significantly enhance Terraform's capabilities by enabling dynamic resource creation and configuration. They empower users to efficiently manage infrastructure at scale and adapt to changing requirements with ease.
Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨
👆The information presented above is based on my interpretation. Suggestions are always welcome.😊
~Smriti Sharma✌