在 Terraform 变量中配置内插列表以创建 SNS 订阅

Configure interpolated list in Terraform variable to create SNS subscriptions

我正在尝试在我的文件顶部配置一个列表,以列出应订阅 SNS 主题的所有 SQS 资源。它抛出一个 "resource variables must be three parts: TYPE.NAME.ATTR"

我使用局部变量是因为它们似乎支持内插值而变量不支持。

locals {
    update-subscribers = [
        "${var.prefix}-${terraform.workspace}-contribution-updates"
      ]
}

这是我的 sns 主题订阅的片段。

resource "aws_sns_topic_subscription" "subscription" {
  count = "${length(locals.update-subscribers.*)}"
  topic_arn = "${aws-sns-update-topic.topic.arn}"
  protocol = "sqs"
  endpoint = "arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${element(locals.update-subscribers, count.index)}"
  endpoint_auto_confirms = true
}

如果能够使用我的变量列表就好了,这样我就可以切换工作区而不会在 AWS 站点上出现任何问题。我能找到的所有示例都指向 CIDR 设置的静态列表,而我希望我的列表基于内插字符串。我也试过

locals.contribution-update-subscribers[count.index]

Terraform 也不喜欢那样。我的文件应该如何设置以支持此功能或是否可以支持?

此处给出的配置有两个问题:

  • 访问本地值的对象名称称为local,而不是locals
  • 您不需要(目前也不能)使用 splat 语法来计算已经是列表的元素数。

解决这两个问题会得到以下配置,我认为应该可行:

resource "aws_sns_topic_subscription" "subscription" {
  count = "${length(local.update-subscribers)}"

  topic_arn              = "${aws_sns_update_topic.topic.arn}"
  protocol               = "sqs"
  endpoint               = "arn:aws:sqs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${local.update-subscribers[count.index])}"
  endpoint_auto_confirms = true
}

虽然在 Terraform 语言的标识符中允许使用破折号以允许在其他系统中使用不同的命名方案,但惯用的风格是对 Terraform 本身定义的名称使用下划线,例如您的本地值名称 update-subscribers.