diff --git a/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/README.md b/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/README.md index 5dc1f974..317d751b 100644 --- a/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/README.md +++ b/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/README.md @@ -37,13 +37,26 @@ resource "aws_instance" "myec2vm" { user_data = file("${path.module}/app1-install.sh") key_name = var.instance_keypair vpc_security_group_ids = [ aws_security_group.vpc-ssh.id, aws_security_group.vpc-web.id ] - # Create EC2 Instance in all Availabilty Zones of a VPC + + # Create EC2 Instance in all Availabilty Zones of a VPC + for_each = toset(data.aws_availability_zones.my_azones.names) - availability_zone = each.key # You can also use each.value because for list items each.key == each.value + + #toset: converts list into set, In set, removes duplicates, + #changes either string or int to highest number of arguments form ( if more strings are present in the list, + #it will convert number as well to string; arranges in order. + + availability_zone = each.key # You can also use each.value because for list items each.key == each.value + tags = { "Name" = "For-Each-Demo-${each.key}" } } + +## output: Will create instances in each availability zones and with name tag given and each.key, for every instance it will have name as, For-Each-Demo-ap-south-1, For-Each-Demo-ap-south-2.... +## ** Note: Both meta-arguments cannot be used in one resource block, if for_each is used then count or any other meta-argument cannot be used.** + + ``` ## Step-04: c6-outputs.tf @@ -122,4 +135,4 @@ rm -rf terraform.tfstate* - [Data Source: aws_availability_zones](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) - [for_each Meta-Argument](https://www.terraform.io/docs/language/meta-arguments/for_each.html) - [tomap Function](https://www.terraform.io/docs/language/functions/tomap.html) -- [toset Function](https://www.terraform.io/docs/language/functions/toset.html) \ No newline at end of file +- [toset Function](https://www.terraform.io/docs/language/functions/toset.html) diff --git a/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/terraform-manifests/c5-ec2instance.tf b/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/terraform-manifests/c5-ec2instance.tf index b727d580..1edeecf8 100644 --- a/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/terraform-manifests/c5-ec2instance.tf +++ b/05-Terraform-Loops-MetaArguments-SplatOperator/05-02-MetaArgument-for_each/terraform-manifests/c5-ec2instance.tf @@ -17,6 +17,7 @@ resource "aws_instance" "myec2vm" { # Create EC2 Instance in all Availabilty Zones of a VPC for_each = toset(data.aws_availability_zones.my_azones.names) availability_zone = each.key # You can also use each.value because for list items each.key == each.value + # for map it should have value. tags = { "Name" = "for_each-Demo-${each.value}" } diff --git a/05-Terraform-Loops-MetaArguments-SplatOperator/05-04-for_each-with-az-instancetype-check/terraform-manifests/c5-ec2instance.tf b/05-Terraform-Loops-MetaArguments-SplatOperator/05-04-for_each-with-az-instancetype-check/terraform-manifests/c5-ec2instance.tf index 33612051..3d02524a 100644 --- a/05-Terraform-Loops-MetaArguments-SplatOperator/05-04-for_each-with-az-instancetype-check/terraform-manifests/c5-ec2instance.tf +++ b/05-Terraform-Loops-MetaArguments-SplatOperator/05-04-for_each-with-az-instancetype-check/terraform-manifests/c5-ec2instance.tf @@ -6,11 +6,13 @@ resource "aws_instance" "myec2vm" { key_name = var.instance_keypair vpc_security_group_ids = [ aws_security_group.vpc-ssh.id, aws_security_group.vpc-web.id ] # Create EC2 Instance in all Availabilty Zones of a VPC - #for_each = toset(data.aws_availability_zones.my_azones.names) + # ** for_each = toset(data.aws_availability_zones.my_azones.names) ** for_each = toset(keys({for az, details in data.aws_ec2_instance_type_offerings.my_ins_type: az => details.instance_types if length(details.instance_types) != 0 })) availability_zone = each.key # You can also use each.value because for list items each.key == each.value + # But for Map, only vlaue should be given. tags = { "Name" = "For-Each-Demo-${each.key}" } } +