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 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-08c40ec9ead489470"
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}"
}
}
Our main.tf looks something like this...
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.22.0"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-east-1"
}
locals {
ami_ids_list = toset([
"ami-0b0dcb5067f052a63",
"ami-08c40ec9ead489470",
])
}
resource "aws_instance" "server_for_each_list" {
for_each = local.ami_ids_list
ami = each.key
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
locals {
ami_ids_map = {
"linux" = "ami-0b0dcb5067f052a63",
"ubuntu" = "ami-08c40ec9ead489470",
}
}
resource "aws_instance" "server_for_each_map" {
for_each = local.ami_ids_map
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "Server ${each.key}"
}
}
Run terraform init
command
Run terraform apply
command
we can see, 4 added
Go to EC2 and check the 4 instances created.
Meta-arguments and their use in Terraform.
Meta-arguments in Terraform are special directives that are used to control and manipulate the behaviour of resources and modules within your configuration. These meta-arguments provide flexibility and automation, enabling you to manage resources more dynamically and efficiently.
Some common meta-arguments in Terraform include count
, for_each
, and depends_on
. Let's focus on the use of count
and for_each
since those are the ones demonstrated in your provided Terraform code.
Count Meta-Argument:
The
count
meta-argument is used to create a specified number of instances of a resource or module. You specify the desired count, and Terraform will generate that many instances.It is useful when you need to create multiple similar resources with the same configuration but a different name or index. Each instance created has its own distinct infrastructure object associated with it, so they can be managed separately.
In your example, the "server_count" resource creates four AWS instances using the
count
meta-argument. Each instance has a unique name based on thecount.index
.
For Each Meta-Argument:
The
for_each
meta-argument is used to create multiple instances of a resource or module, but instead of specifying the number of instances, it accepts a map or a set of values. Each value in the map or set corresponds to a separate instance.It is especially useful when you need to create multiple resources with different configurations based on a list of values or a map of values.
In your example, you demonstrated the use of
for_each
with both a list and a map of AMI IDs. The "server_for_each_list" resource creates instances for each item in the list, and the "server_for_each_map" resource creates instances for each key-value pair in the map.
Thank you so much for reading
Follow me on LinkedIn to see interesting posts like this : )