简体   繁体   English

Terraform 中的参数化 YAML 模板

[英]Parameterized YAML template in Terraform

I am about to refactor a couple of code for a business project.我即将为一个商业项目重构一些代码。 Among other tings, converting from JSON to YAML templates is necessary.除其他外,需要从 JSON 转换为 YAML 模板。 I use terraform for infrastructure deployment.我使用 terraform 进行基础架构部署。

I have this JSON template cf_sns.json.tpl file:我有这个 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" }
  }
}
}

This is a main.tf file using this template file:这是使用此模板文件的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"))

  }
}

I pass ["myemail", "myOtherEmail"] to var.sns_subscription_email_adress_list .我将["myemail", "myOtherEmail"]传递给var.sns_subscription_email_adress_list I had to use this approach with a cloudformation resource since Terraform does not support the email protocol for a sns subspription.我不得不将此方法与 cloudformation 资源一起使用,因为 Terraform 不支持用于 sns 订阅的email协议。

How can I refactor the cf_sns.json.tpl to a YAML file together with the data resource mentioned above in the main.tf file?如何将cf_sns.json.tpl重构为 YAML 文件以及上述main.tf文件中的数据资源? Particularly, I have no clue how to properly pass the sns_subscription_list as YAML array.特别是,我不知道如何正确地将 sns_subscription_list 作为 YAML 数组传递。

That cf_sns.json.tpl is AWS CloudFormation code, if you are already using terraform just refactor that all the way, not just convert from JSON to YAML but completely get rid of that and use the proper terraform resources: That cf_sns.json.tpl is AWS CloudFormation code, if you are already using terraform just refactor that all the way, not just convert from JSON to YAML but completely get rid of that and use the proper terraform resources:

Here is some sample code:这是一些示例代码:

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 cloudformation 模板移植到 terraform - Porting a cloudformation template to terraform 在 Terraform 中将 Yaml 转换为排序的 Json 数据 - Convert Yaml to Sorted Json data in Terraform 将列表变量传递给 terraform 中的 JSON 模板 - Pass list variable to JSON template in terraform 如何在 terraform 模板文件中将字符串转换为数字 - How to convert string to number in terraform template file 如何将 AWS Cloudformation 的 YAML 连接格式写入 Terraform 格式? - How can I write a YAML Join Format of AWS Cloudformation to Terraform format? 错误:无法读取输入 object(不是模板?):将 YAML 转换为 JSON 时出错:Z6EEDC03A68A62933C763E185CA2BCZ 时出现错误:在上下文中不允许使用 Z6EEDC03A68A62933C76FZD674 映射值744 - error: failed to read input object (not a Template?): error converting YAML to JSON: yaml: mapping values are not allowed in this context 多行Yaml输入到胡子模板输出为JSON - multi-line yaml input to mustache template outputs as JSON 如何将字符串列表传递给 terragrunt / terraform 中的数据模板 - How to pass a list of string or strings to data template in terragrunt / terraform 如何在Terraform的ARM模板中使用custom_data参数? - How to use custom_data parameter in ARM template in Terraform? 如何在 aws cloudformation yaml 模板中格式化数据类型 json 的参数? - How to format parameter of data type json in a aws cloudformation yaml template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM