首页 > 解决方案 > 将地图转换为 terraform aws_autoscaling_group 的列表

问题描述

我有以下子网 ID 映射作为要在 Terraformaws_autoscaling_group资源中使用的变量:

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

和变量为

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

但这不起作用,因为 Terraform 抱怨它需要一个列表但正在查看地图。

只需临时设置一个区域即可:

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

和变量为:

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

并通过vpc_zone_identifier = "${var.subnet_ids}"

所以非常想要一种能够使用地图并能够根据选择的区域传入列表的方法

谢谢

标签: amazon-web-servicesterraformterraform-provider-aws

解决方案


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.


推荐阅读