【问题标题】:How to create a template using terraform loop如何使用 terraform 循环创建模板
【发布时间】:2020-09-12 09:37:29
【问题描述】:

我想通过基于列表变量(email_addresses)的 loping 使用 terraform template_file 创建 CFT。 以下是我要生成的变量和模板。

variables:-

emails_addresses = ["sample-1@gmail.com", "sample-2@gmail.com"]
sns_arn = "arn:aws:sns:us-east-1:xxxxxx:xxxx"
protocol = "email"

期望模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "sample-1": {
            "Type": "AWS::SNS::Subscription",
            "Properties": {
                "Endpoint": "sample-1@gmail.com",
                "Protocol": "email",
                "TopicArn": "arn:aws:sns:us-east-1:xxxx:xxxxx"
            }
        },
        "sample-2": {
            "Type": "AWS::SNS::Subscription",
            "Properties": {
                "Endpoint": "sample-2@gmil.com",
                "Protocol": "email",
                "TopicArn": "arn:aws:sns:us-east-1:xxx:xxxx"
            }
        }
    }
}

CFT 中的资源名称可以是一些随机字符串,但在多个计划/申请的情况下,每封邮件应相同。

【问题讨论】:

  • 下面是我试图改变的一些例子。 data "template_file" "cloudformation_sns_stack" { template = file("${path.module}/templates/email-sns-stack.json.tpl") vars = { display_name = var.display_name subscriptions = join( ",", formatlist( "{ \"Endpoint\": \"%s\", \"Protocol\": \"%s\" }", var.email_addresses, var.protocol, ), ) } }

标签: amazon-web-services terraform amazon-sns terraform-provider-aws


【解决方案1】:

由于json,这个有点棘手。另外我会使用templatefile 而不是template_file,因为您可以将列表传递给它。

variable "emails_addresses" {
  default = ["sample-1@gmail.com", "sample-2@gmail.com"]
}

variable "sns_arn" {
  default = "arn:aws:sns:us-east-1:xxxxxx:xxxx"
}

variable "protocol" {
  default = "email"
}

output "test" {
   value = templatefile("./email-sns-stack.json.tpl", {
     emails_addresses = var.emails_addresses,
     sns_arn = var.sns_arn,
     protocol = var.protocol  
   })
 }

email-sns-stack.json.tpl 在哪里:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": ${jsonencode(
        {for email_address in emails_addresses: 
        split("@",email_address)[0] => {
          Type = "AWS::SNS::Subscription"
          Properties = {
                "Endpoint" = email_address
                "Protocol" = protocol
                "TopicArn" = sns_arn          
          }
        }})}
}

经过漂亮的 json 格式以提高可读性的输出:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "sample-1": {
      "Properties": {
        "Endpoint": "sample-1@gmail.com",
        "Protocol": "email",
        "TopicArn": "arn:aws:sns:us-east-1:xxxxxx:xxxx"
      },
      "Type": "AWS::SNS::Subscription"
    },
    "sample-2": {
      "Properties": {
        "Endpoint": "sample-2@gmail.com",
        "Protocol": "email",
        "TopicArn": "arn:aws:sns:us-east-1:xxxxxx:xxxx"
      },
      "Type": "AWS::SNS::Subscription"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2020-06-05
    • 2023-04-06
    • 2020-09-06
    • 2011-07-01
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多