convert map to list for terraform aws_autoscaling_group

Viewed 5338

I have the following map of subnet ids as a variable to be used in a Terraform aws_autoscaling_group resource:

subnet_ids = {
  "us-east-1" = "subnet-123abc,subnet-456def,subnet-789ghi"
  "us-west-2" = "subnet-1a2b3c,subnet-4c5d6e,subnet-7g8h9i"
}

and variable as

variable subnet_ids {
  description = "subnet ids"
  type        = "map"
}

but this not working as Terraform is complaining that it needs a list but is seeing a map.

This worked by just setting up one region temporarily:

subnet_ids = ["subnet-123abc", "subnet-456def", "subnet-789ghi"]

and variable as:

variable subnet_ids {
  description = "subnet ids"
  type        = "list"
}

and passed as vpc_zone_identifier = "${var.subnet_ids}"

So pretty much want a way to be able to use the map and be able to pass in lists based on the region picked

Thanks

3 Answers

Try using the lookup command and a variable to represent your region to choose a value from your map:

variable region {
   description = "aws region"
   default = "us-east-1"
}

...

vpc_zone_identifier = ["${lookup(var.subnet_ids, var.region)}"]

See the documentation for more examples.

For anyone facing issues with security groups for autoscaling group resource in terraform, this is how you pass map to list

this is what worked

security_groups = ["${split(",",lookup(var.security_groups, var.aws_region))}"]

Or...

variable region {
   description = "aws region"
   default = "us-east-1"
}

...

vpc_zone_identifier = ["${var.subnet_ids["${var.region}"]}"]
Related