개발하자

테라포름: 문자열 목록

Cuire 2022. 12. 7. 00:55
반응형

테라포름: 문자열 목록

나는 테라폼 모듈을 사용하여 Security Group을 만들려고 한다. 데이터 소스에서 가져온 VPC가 필요합니다. VPC ID에는 문자열 값이 필요하지만 데이터 소스는 값이 하나인 목록을 반환합니다.

찾아주세요

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",
]



이미 목록이므로 다른 목록에 추가할 필요가 없습니다.

시도:

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

: 댓글의 질문에 대한 답변: 여기서 유사한 이슈에서 언급한 바와 같이 반환된 것은 목록이 아닌 세트인 것 같습니다.

를 사용하는 경우: 할 수 있습니다.

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

를 사용하는 경우: 할 수 있습니다.

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

반응형