创建多个SNS主题的电子邮件订阅

huangapple go评论69阅读模式
英文:

Create subscription for multiple sns topics with email list

问题

I'm dynamically creating multiple SNS topics based on naming conventions. I'm trying to subscribe multiple people to each topic, but I can only use one for_each or count field. How can I solve this without hardcoding multiple resources?

variables.tf

variable "system" {
  default = ["1", "2"]
}

variable "alert_level" {
  default = ["error", "exception", "info", "warning"]
}

variable "email_subscription" {
  default = ["a@company.com", "b@company.com"]
}

sns.tf

locals {
  alerts = flatten([
    for source in var.system:
    formatlist("%s-%s", source, var.alert_level)
  ])
}

resource "aws_sns_topic" "alerts" {
  count = length(local.alerts)
  name = "${var.BASENAME}-${var.ENVIRONMENT}-${local.alerts[count.index]}"
}

resource "aws_sns_topic_subscription" "sns-topic" {
  count = length(local.alerts)
  topic_arn = "arn:aws:sns:us-east-1:123:${var.BASENAME}-${var.ENVIRONMENT}-${local.alerts[count.index]}"
  protocol = "email"
  endpoint = var.email_subscription # how can I loop over var.email_subscription?
}
英文:

I'm dynamically creating multiple SNS topics based on naming conventions. I'm trying to subscribe multiple people to each topic, but I can only use one for_each or count field. How can I solve this without hardcoding multiple resources?

variables.tf

variable "system" {
  default = ["1", "2"]
}

variable "alert_level" {
  default = ["error", "exception", "info", "warning"]
}

variable "email_subscription" {
  default = ["a@company.com", "b@company.com"]
}

sns.tf

locals {
  alerts = flatten([
    for source in var.system:
    formatlist("%s-%s", source, var.alert_level)
  ])
}

resource "aws_sns_topic" "alerts" {
  count             = length(local.alerts)
  name              = "${var.BASENAME}-${var.ENVIRONMENT}-${local.alerts[count.index]}"
}

resource "aws_sns_topic_subscription" "sns-topic" {
  count     = length(local.alerts)
  topic_arn = "arn:aws:sns:us-east-1:123:${var.BASENAME}-${var.ENVIRONMENT}-${local.alerts[count.index]}"
  protocol  = "email"
  endpoint  = var.email_subscription # how can I loop over var.email_subscription?
}

答案1

得分: 2

你可以使用setproduct()函数,它会为你生成给定集合的所有组合(文档:https://developer.hashicorp.com/terraform/language/functions/setproduct)。

然后,迭代setproduct(var.alert_level, var.email_subscription)的结果来创建你的主题。以下是示例代码:

locals {
  product = setproduct(var.alert_level, var.email_subscription)
}

resource "aws_sns_topic_subscription" "sns-topic" {
  count     = length(local.product)
  topic_arn = "arn:aws:sns:us-east-1:111122223333:${var.BASENAME}-${var.ENVIRONMENT}-${local.product[count.index][0]}"
  protocol  = "email"
  endpoint  = local.product[count.index][1]
}
英文:

You can use setproduct() which gives you all combinations of the given sets (Docs: https://developer.hashicorp.com/terraform/language/functions/setproduct)

And then iterate over the result of setproduct(var.alert_level, var.email_subscription) to create your topics. Here how it would look like:

locals {
  product = setproduct(var.alert_level, var.email_subscription)
}

resource "aws_sns_topic_subscription" "sns-topic" {
  count     = length(local.product)
  topic_arn = "arn:aws:sns:us-east-1:111122223333:${var.BASENAME}-${var.ENVIRONMENT}-${local.product[count.index][0]}"
  protocol  = "email"
  endpoint  = local.product[count.index][1]
}

huangapple
  • 本文由 发表于 2023年3月4日 01:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630233.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定