Terraform:列出到字符串

时间:2019-08-12 05:43:30

标签: terraform

我正在尝试使用Terraform模块terraform-aws-modules/security-group/aws创建一个安全组。这将需要从aws_vpcs数据源获取的vpc id。 vpc ID需要一个字符串值,但是aws_vpcs数据源返回一个具有单个值的列表。请找到

-

data "aws_vpcs" "this" {
  tags = {
    "Name" = "example"
  }
}

module "route53_sg" {
  source = "terraform-aws-modules/security-group/aws"

  name        = "R53_health_checkers"
  description = "Security group for Route53 health checkers"
  vpc_id      = element([data.aws_vpcs.this.ids], 0)
  ingress_cidr_blocks = [
...
...
...
  ]
  ingress_rules = ["https-443-tcp"]
}




$ terraform apply
data.aws_lb.ext_alb: Refreshing state...
data.aws_vpcs.this: Refreshing state...

Error: Invalid value for module argument

  on main.tf line 75, in module "route53_sg":
  75:   vpc_id      = element([data.aws_vpcs.this.ids], 0)

The given value is not suitable for child module variable "vpc_id" defined at
.terraform/modules/route53_sg/terraform-aws-modules-terraform-aws-security-group-d55e4de/variables.tf:10,1-18:
string required.



vpc_id is expecting a Single string. FOLLOWING is a result from Output.tf

$ terraform apply
data.aws_lb.ext_alb: Refreshing state...
data.aws_vpcs.this: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

vpc = [
  "vpc-08067a598522a7b30",
]

1 个答案:

答案 0 :(得分:2)

data.aws_vpcs.this.ids已经是一个列表,您无需将其放入另一个列表中。

尝试:

vpc_id = element(data.aws_vpcs.this.ids, 0)

编辑:回答评论中的问题: 似乎返回的ids是一个集合而不是列表,如此处类似问题所述: https://github.com/terraform-providers/terraform-provider-aws/issues/7522

如果您使用的是0.12.x: 你可以做

vpc_id = element(tolist(data.aws_vpcs.this.ids), 0)

如果您使用的是0.11.x:可以

vpc_id = element(split(",", join(",", data.aws_vpcs.this.ids))), 0)