如何使用 Terraform 从 VPC 获取子网列表

How to get Subnet list from VPC with terraform

我已尝试获取所有子网 ID,以使用以下代码添加带有 terraform 的 aws 批处理:

data "aws_subnet_ids" "test_subnet_ids" {
  vpc_id = "default"
}
data "aws_subnet" "test_subnet" {
  count = "${length(data.aws_subnet_ids.test_subnet_ids.ids)}"
  id    = "${tolist(data.aws_subnet_ids.test_subnet_ids.ids)[count.index]}"
}

output "subnet_cidr_blocks" {
  value = ["${data.aws_subnet.test_subnet.*.id}"]
}

幸运的是,当我这样测试时它工作正常。但是当我尝试与批处理地形集成时,如:

resource "aws_batch_compute_environment" "test-qr-processor" {
  compute_environment_name = "test-qr-processor-test"
  compute_resources {
    instance_role = "${aws_iam_instance_profile.test-ec2-role.arn}"
    instance_type = [
      "optimal"
    ]
    max_vcpus = 256
    min_vcpus = 0
    security_group_ids = [
      "${aws_security_group.test-processor-batch.id}"
    ]
    subnets = ["${data.aws_subnet.test_subnet.*.id}"]
    type = "EC2"
  }
  service_role = "${aws_iam_role.test-batch-service-role.arn}"
  type = "MANAGED"
  depends_on = [ "aws_iam_role_policy_attachment.test-batch-service-role" ]
}

我遇到了以下错误信息,

Error: Incorrect attribute value type

on terraform.tf line 142, in resource "aws_batch_compute_environment" "test-processor": 142: subnets = ["${data.aws_subnet.test_subnet.*.id}"]

Inappropriate value for attribute "subnets": element 0: string required.

请告诉我原因,谢谢。

"${data.aws_subnet.test_subnet.*.id}" 已经是 string array 类型。

你应该输入没有 [ ]

的值

编写如下代码:

subnets = "${data.aws_subnet.test_subnet.*.id}"

See :

Here's A document about Resource: aws_batch_compute_environment