From b25834999c4fb995737b3cf83185a4dcdd5398dc Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:13:34 +0530 Subject: [PATCH 01/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..b0f709f9 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 @@ -38,7 +38,7 @@ 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) #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}" @@ -122,4 +122,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) From 9dc7cbf46e6e4b00d6e1b71b61ea53d1b48f1c99 Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:14:57 +0530 Subject: [PATCH 02/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 b0f709f9..e154ae89 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 @@ -38,7 +38,9 @@ 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) #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. + for_each = toset(data.aws_availability_zones.my_azones.names) #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}" From 58d57ff1e9608d085aaa7db145eac01fee444e37 Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:33:41 +0530 Subject: [PATCH 03/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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 e154ae89..47c6a7bd 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,15 +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 - for_each = toset(data.aws_availability_zones.my_azones.names) #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 + + # Create EC2 Instance in all Availabilty Zones of a VPC + + for_each = toset(data.aws_availability_zones.my_azones.names) + + #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 From 41bc283be4093e32be371d974ade9eacc69fdb9e Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:34:39 +0530 Subject: [PATCH 04/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 47c6a7bd..77c1761f 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 @@ -53,8 +53,8 @@ resource "aws_instance" "myec2vm" { } } -#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. +#output: Will create instances in each availability zones and with name tag given and each.key, "/n" 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, "/n" if for_each is used then count or any other meta-argument cannot be used. ``` From f3cf31b1a832f9ec90c89caa0d99bc7798bd3d13 Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:35:11 +0530 Subject: [PATCH 05/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 77c1761f..c2ae8aa9 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 @@ -53,8 +53,8 @@ resource "aws_instance" "myec2vm" { } } -#output: Will create instances in each availability zones and with name tag given and each.key, "/n" 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, "/n" if for_each is used then count or any other meta-argument cannot be used. +#output: Will create instances in each availability zones and with name tag given and each.key, /n 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, /n if for_each is used then count or any other meta-argument cannot be used. ``` From 0773d7973c2cb674b6925333ae4129414a1e2f42 Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:35:30 +0530 Subject: [PATCH 06/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 c2ae8aa9..9f42926e 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 @@ -53,8 +53,8 @@ resource "aws_instance" "myec2vm" { } } -#output: Will create instances in each availability zones and with name tag given and each.key, /n 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, /n if for_each is used then count or any other meta-argument cannot be used. +#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. ``` From cba29234113c74e61a72bd3895fba817a69d259f Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:37:29 +0530 Subject: [PATCH 07/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9f42926e..bd5bfddd 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 @@ -53,8 +53,8 @@ resource "aws_instance" "myec2vm" { } } -#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. +##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. ``` From 81d8f9a6229a74a8993248fe331355752365fe5b Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:37:53 +0530 Subject: [PATCH 08/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 bd5bfddd..c62d8c9b 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 @@ -53,8 +53,8 @@ resource "aws_instance" "myec2vm" { } } -##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. +## 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. ``` From 05dc4e5797f1765a207a9b283a064cd6d65a9080 Mon Sep 17 00:00:00 2001 From: pavan <31065669+pavankumar636@users.noreply.github.com> Date: Tue, 9 Nov 2021 10:38:48 +0530 Subject: [PATCH 09/12] Update README.md --- .../05-02-MetaArgument-for_each/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c62d8c9b..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 @@ -54,7 +54,7 @@ resource "aws_instance" "myec2vm" { } ## 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. +## ** 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.** ``` From cd4c79885efedec47791f887a6ed175a63ae9a7b Mon Sep 17 00:00:00 2001 From: pavankumar636 Date: Tue, 9 Nov 2021 10:50:33 +0530 Subject: [PATCH 10/12] push --- .../terraform-manifests/c5-ec2instance.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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..125a6499 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,7 +6,7 @@ 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 From 1dcb9892f32c0b915f930a3826a6bfd0a2028c3f Mon Sep 17 00:00:00 2001 From: pavankumar636 Date: Tue, 9 Nov 2021 10:54:24 +0530 Subject: [PATCH 11/12] push --- .../terraform-manifests/c5-ec2instance.tf | 2 ++ 1 file changed, 2 insertions(+) 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 125a6499..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 @@ -10,7 +10,9 @@ resource "aws_instance" "myec2vm" { 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}" } } + From 3bbd1af8251dfca3b1d2ccb3f63d6235995eabca Mon Sep 17 00:00:00 2001 From: pavankumar636 Date: Tue, 9 Nov 2021 11:04:27 +0530 Subject: [PATCH 12/12] update --- .../terraform-manifests/c5-ec2instance.tf | 1 + 1 file changed, 1 insertion(+) 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}" }