Ever wondered how to efficiently manage multiple resources in Terraform without writing repetitive code? Enter meta-arguments - the secret sauce to streamline your infrastructure management! ๐ก Let's dive in and demystify count and for_each.
Count: ๐ Count, a versatile meta-argument, lets you create a specified number of resource instances effortlessly. Each instance is uniquely identifiable, allowing separate management. Here's a snippet illustrating its power:
resource "aws_instance" "server" {
count = 4
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "Server ${count.index}"
}
}
For_each: ๐ Similar to count, for_each generates multiple resource instances. However, it operates differently by accepting a map or a set of strings, making it ideal for diverse resource configurations. Let's explore its magic:
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: ๐ Taking for_each up a notch, it enables iteration over multiple key-value pairs, perfect for nuanced configurations:
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: Hands-on Exploration! ๐ Ready to harness the power of count and for_each? Create an infrastructure using Terraform and demonstrate their prowess.
Create the above Infrastructure as code and demonstrate the use of Count and for_each.
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.
Previously, we spun up EC2 instances. In the same resource block, we can add a count key-value pair to spin multiple instances. Create a count_main.tf file containing providers and resources.
Initialise the terraform file with terraform init command.
Now, use terraform apply the command to create the instances.
Navigate top the EC2 management console to view the instances.
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.
Create a for_each terraform file and include the string values you want to spin up the servers with.
COPY
COPY
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-0889a44b331db0194", "ami-007855ac798b5175e", ]) } resource "aws_instance" "server" { for_each = local.ami_ids ami = each.key instance_type = "t2.micro" tags = { Name = "Server ${each.key}" } }
Use terraform apply to create the servers based on string values that are picked up by for-each in the resource block.
We can check the newly created instance in the AWS management console.
We can also use key-value pairs as string values as a input to spin up instances.
COPY
COPY
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } required_version = ">= 1.2.0" } provider "aws" { region = "us-east-1" } #Multiple key value iteration locals { ami_ids = { "linux" :"ami-0889a44b331db0194", "ubuntu": "ami-007855ac798b5175e", } } resource "aws_instance" "server" { for_each = local.ami_ids ami = each.value instance_type = "t2.micro" tags = { Name = "Server ${each.key}" } }
With terraform apply new instances will be created on the key name that was passed.
Navigate to the console to check the newly spun instances.
Dive deeper into meta-arguments and unleash Terraform's full potential in managing your infrastructure with ease. Stay tuned for an enriching Terraform journey! #Terraform #InfrastructureAsCode #Automation #CloudComputing ๐ป๐