英文:
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]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论