Day 69- 90DaysOfDevOps
Meta-Arguments in Terraform

Hey Learners! Welcome back. We understood the concept of different Blocks used in Terraform. In this challenge, we'll understand the special arguments that can be used with Resource Blocks and Modules. Meta-arguments help achieve certain requirements within the Resource Block. Let's start...
Meta-Arguments:-
Meta-arguments in Terraform are the special arguments that can be used with resource blocks and modules to control their behaviour or influence the infrastructure provisioning process. They provide additional configuration options beyond the regular resource-specific arguments.
Meta-arguments in Terraform:-
count
for_each
provider
depends_on
lifecycle
1- count:- Controls resource instantiation by setting the number of instances created based on a given condition or variable. To manage several similar objects, without writing a separate resource block use the count argument in the resource block. Appropriate if your instances are almost identical.
The count meta-argument accepts a whole number and creates that many instances of the resource or module.
resource "aws_instance" "Example" {
count = 2
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
tags = {
Name = "Example_Instance_${count.index}"
}
}
Apply the changes using the terraform apply command.

Go to the AWS console and check the instances created with names.
Note:- The distinct index number (starting with 0) corresponding to the instance.

2- for_each:- Allows creating multiple instances of a resource based on a map or set of strings. You can specify for_each argument instead of count if some of the resource arguments need distinct values that can't be directly delivered from an integer.
Each instance is created with its unique key-value pair.
locals {
instances = {
Ubuntu = "ami-0f5ee92e2d63afc18"
RedHat = "ami-0645cf88151eb2007"
}
}
resource "aws_instance" "Instance" {
for_each = local.instances
ami = each.value
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
Provision the configuration by using the terraform apply command. Go to the AWS console and check the Name and AMI ID for created instances.


TRY THIS- If you want identical instances as created with count argument but with different names use for_each as shown below
resource "aws_instance" "practice" {
for_each = toset(["Ubuntu", "RedHat"])
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
3- provider:- Specifies the provider configuration for a resource. It allows selecting a specific provider or version for a resource.
Each provider can have a default configuration, and any number of alternate configurations that include an extra name segment or "alias".
provider "aws" {
region = "ap-south-1"
}
provider "aws" {
alias = "east"
region = "us-east-1"
}
resource "aws_instance" "east" {
provider = aws.east
ami = "ami-0fc5d935ebf8bc3bc" #make sure AMI id should from specified region
instance_type = "t2.micro"
tags = {
Name = "instance-east"
}
}
Apply the changes with the terraform apply command. Go to the AWS console and check whether the instance is created or not in a specified region.

4- depends_on:- Specifies dependencies between resources. It ensures that one resource is created or updated before another resource.
5- lifecycle:- Defines lifecycle rules for managing resource updates, replacements, and deletions.
An instance created by the following configuration will not destroy even if we use the terraform destroy command.
resource "aws_instance" "lifecycle" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
lifecycle {
prevent_destroy = true
}
}

To destroy the resource created as above just comment or delete the lifecycle argument block as shown below then use the terraform destroy command.

This is how we can use the Meta-arguments with resource blocks in Terraform to achieve certain requirements.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!




