如何使用 Terraform 在 AWS SSM 参数中存储三元素元组? [英] How can I store a three element tuple in AWS SSM parameter with Terraform?

查看:14
本文介绍了如何使用 Terraform 在 AWS SSM 参数中存储三元素元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating private subnets with 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 参数:

Later I'm trying to create SSM parameter with:

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

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

As subnets resource is making three subnets, it raises the following error:

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

Inappropriate value for attribute "value": string required.

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

How should I pass this three element tuple to the StringList type parameter?

推荐答案

value 参数rel="nofollow noreferrer">aws_ssm_parameter resource 必须是字符串类型,无论指定的 type 是什么.事实上,AWS 总是希望参数是字符串类型,如 API 文档 并在 this answerStringList 类型本质上是元数据,客户端期望它是一个字符串,其中包含由逗号字符连接在一起的其他字符串.

The 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 this answer and the StringList type is essentially metadata for the client to expect it to be a string that contains other strings concatenated together by a comma character.

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

To convert your tuple type from aws_subnet.private.*.id into a list you can join it with the join function like this:

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

这篇关于如何使用 Terraform 在 AWS SSM 参数中存储三元素元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆