我将为业务项目重构几个代码。除其他外,还需要从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数组传递。
[那个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
}