【问题标题】:Parameterized YAML template in TerraformTerraform 中的参数化 YAML 模板
【发布时间】:2020-09-27 15:15:58
【问题描述】:

我即将为一个商业项目重构一些代码。除其他外,从 JSON 转换为 YAML 模板是必要的。我使用 terraform 进行基础架构部署。

我有这个 JSON 模板 cf_sns.json.tpl 文件:

{
 "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "SNSTopic": {
      "Type": "AWS::SNS::Topic",
      "Properties": {
        "TopicName": "${sns_topic_name}",
        "KmsMasterKeyId": "${kms_key_id}",
        "DisplayName": "${sns_topic_name}",
        "Subscription": [
          "${sns_subscription_list}"
        ]
      }
    }
  },
  "Outputs" : {
  "SNSTopicARN" : {
    "Description": "The SNS Topic Arn",  
    "Value" : { "Ref" : "SNSTopic" }
  }
}
}

这是使用此模板文件的main.tf 文件:

data "template_file" "this" {
  template = "${file("${path.module}/templates/cf_sns.json.tpl")}"
  vars = {
    kms_key_id            = var.kms_key_id
    sns_topic_name        = var.sns_topic_name
    sns_subscription_list = join(",", formatlist("{\"Endpoint\": \"%s\",\"Protocol\": \"%s\"}", var.sns_subscription_email_address_list, "email"))

  }
}

我将["myemail", "myOtherEmail"] 传递给var.sns_subscription_email_adress_list。 我不得不将这种方法与 cloudformation 资源一起使用,因为 Terraform 不支持用于 sns 子脚本的 email 协议。

如何将cf_sns.json.tpl 与上述main.tf 文件中提到的数据资源一起重构为YAML 文件?特别是,我不知道如何正确地将 sns_subscription_list 作为 YAML 数组传递。

【问题讨论】:

  • 这是 Terraform 0.11?
  • 是terraform 0.12.25

标签: json yaml refactoring terraform amazon-cloudformation


【解决方案1】:

cf_sns.json.tpl 是 AWS CloudFormation 代码,如果您已经在使用 terraform,只需一路重构,不仅仅是从 JSON 转换为 YAML,而是完全摆脱它并使用适当的 terraform 资源:

这里是一些示例代码:

resource "aws_sns_topic" "SNSTopic" {
  name              = var.sns_topic_name
  kms_master_key_id = var.kms_key_id
  display_name      = var.sns_topic_name 
}

output "SNSTopicARN" {
  value = aws_sns_topic.SNSTopic.arn
}

【讨论】:

  • 我忘了强调,terraform 不支持订阅协议“电子邮件”。这就是我选择 cloudformation 资源的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 2020-06-12
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多