Patch for_each loop through template variable.

The for_each loop that builds multiple resources now correctly sets values for each resource. Previously we did not descend into each object.
This commit is contained in:
mharb 2023-10-02 16:43:12 -04:00
parent c6efa83224
commit dfb31a3d74
5 changed files with 22 additions and 18 deletions

View File

@ -7,22 +7,23 @@ module "ec2_ssh_key" {
} }
module "standalone_ec2" { module "standalone_ec2" {
for_each = var.ec2_template[*] # Create a new list for each ec2_template object
for_each = { for idx, val in var.ec2_template : idx => val }
source = "terraform-aws-modules/ec2-instance/aws" source = "terraform-aws-modules/ec2-instance/aws"
name = each.value["hostname"] name = each.value.hostname
ami = each.value["ami"] ami = each.value.ami
instance_type = each.value["family"] instance_type = each.value.family
key_name = module.ec2_ssh_key.key_pair_name key_name = module.ec2_ssh_key.key_pair_name
monitoring = true monitoring = true
vpc_security_group_ids = [module.ec2_rdp_sg.security_group_id] vpc_security_group_ids = [module.ec2_rdp_sg.security_group_id]
subnet_id = each.value["subnet"] subnet_id = each.value.subnet
root_block_device = [ root_block_device = [
{ {
volume_size = each.value["disksize"] volume_size = each.value.disksize
encrypted = true encrypted = true
} }
] ]

View File

@ -1,11 +1,12 @@
module "db_psql" { module "db_psql" {
source = "terraform-aws-modules/rds/aws" source = "terraform-aws-modules/rds/aws"
for_each = var.db_template[*] for_each = { for idx, val in var.db_template : idx => val }
identifier = each.value["name"] identifier = each.value.name
engine = each.value["engine"] engine = each.value.engine
engine_version = each.value["engine_version"] engine_version = each.value.engine_version
instance_class = each.value["family"] family = each.value.family
instance_class = each.value.class
tags = var.tags tags = var.tags
} }

View File

@ -35,10 +35,11 @@ ec2_template = [
db_template = [ db_template = [
{ {
name = "RDS1" name = "rds1"
engine = "postgres" engine = "postgres"
engine_version = 11 engine_version = 11
family = "db.t3.micro" class = "db.t3.micro"
family = "postgres"
subnet = "10.1.5.0/24" subnet = "10.1.5.0/24"
} }
] ]

View File

@ -64,6 +64,7 @@ variable "db_template" {
name = string name = string
engine = string engine = string
engine_version = number engine_version = number
class = string
family = string family = string
subnet = string subnet = string
})) }))