Terraform 中的参数化 YAML 模板
Parameterized YAML template in Terraform
我要为一个商业项目重构一些代码。在其他方面,从 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,而是完全摆脱它并使用适当的地形资源:
- https://www.terraform.io/docs/providers/aws/r/sns_topic.html
- https://www.terraform.io/docs/providers/aws/r/sns_topic_subscription.html
下面是一些示例代码:
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
}
我要为一个商业项目重构一些代码。在其他方面,从 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,而是完全摆脱它并使用适当的地形资源:
- https://www.terraform.io/docs/providers/aws/r/sns_topic.html
- https://www.terraform.io/docs/providers/aws/r/sns_topic_subscription.html
下面是一些示例代码:
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
}