在terraform中,如何使用地图列表清单?

时间:2018-05-03 10:01:06

标签: terraform

我想使用terraform中的地图列表。 我有3个环境,每个环有多个子网。

segmented_subnets = {
  rest = {
    dev = [
      "subnet-", #a
      "subnet-", #b
    ]
    qa = [
      "subnet-"
      "subnet-"
    ]
  }
  dashboard = {
    dev = [
      "subnet-",
      "subnet-",
    ]
    qa = [
      "subnet-"
      "subnet-"
    ]
  }
  login = {
    dev = [
      "subnet-",
      "subnet-",
    ]
    qa = [
      "subnet-"
      "subnet-"
    ]
  }

}

我想访问这些子网, 我试过了:

"${element(var.segmented_subnets.rest.["${terraform.workspace}"], count.index)}"

或:

"$${element(var.segmented_subnets.rest.["${terraform.workspace}"], count.index)}"

无法访问此变量

1 个答案:

答案 0 :(得分:0)

完成数据使用:

data "aws_subnet_ids" "foo" {
  vpc_id = "${var.vpcs["${terraform.workspace}"]}"

  tags {
    Name = "bar*"
  }
}

用于:

    resource "aws_instance" "ec2-foo" {
...
      instance_type        = "t2.small"
      subnet_id            = "${data.aws_subnet_ids.foo.ids[count.index]}"
...