[英]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.