如何使用 Terraform 在 AWS SSM 参数中存储三元素元组?

How can I store a three element tuple in AWS SSM parameter with Terraform?

我正在使用 Terraform 创建私有子网:

resource "aws_subnet" "private" {
  count = length(data.aws_availability_zones.available.names)

  vpc_id                  = aws_vpc.main_vpc.id
  cidr_block              = cidrsubnet(var.vpc_cidr, 8, count.index + 10)
  availability_zone       = element(data.aws_availability_zones.available.names, count.index)
  map_public_ip_on_launch = false

  tags = {
    Name = "${var.client_code}-${var.environment}-private-${element(data.aws_availability_zones.available.names, count.index)}"
  }
}

稍后我尝试使用以下方法创建 SSM 参数:

resource "aws_ssm_parameter" "private_subnets_ids" {
  name  = "/${var.client_code}-${var.environment}/backend/SUBNET_IDS"
  type  = "StringList"
  value = aws_subnet.private.*.id
}

由于子网资源正在创建三个子网,因此引发了以下错误:

   4:   value = aws_subnet.private.*.id
    |----------------
    | aws_subnet.private is tuple with 3 elements

Inappropriate value for attribute "value": string required.

如何将这个三元素元组传递给 StringList 类型参数?

value parameter for the aws_ssm_parameter resource needs to be a string type regardless of the type specified. In fact, AWS always expects parameters to be of a string type as seen in the API docs and mentioned in StringList 类型本质上是元数据,客户端希望它是一个字符串,其中包含由逗号字符连接在一起的其他字符串。

要将元组类型从 aws_subnet.private.*.id 转换为列表,您可以像这样使用 join function 加入它:

resource "aws_ssm_parameter" "private_subnets_ids" {
  name  = "/${var.client_code}-${var.environment}/backend/SUBNET_IDS"
  type  = "StringList"
  value = join(",", aws_subnet.private.*.id)
}

完整示例如下:

resource "aws_ssm_parameter" "SubnetIDs" {
  name        = "SubnetIDs"
  description = "SubnetIDs"
  type        = "StringList"
  value       = join(", ", aws_subnet.private-subnet.*.id)
}