gpt4 book ai didi

json - 模板正文包含无效的 JSON : invalid character

转载 作者:行者123 更新时间:2023-12-03 07:23:45 29 4
gpt4 key购买 nike

我有一个用于创建 SNS 主题和订阅的 CloudFormation 模板

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources" : {
"EmailSNSTopic": {
"Type" : "AWS::SNS::Topic",
"Properties" : {
"DisplayName" : "${display_name}"
}
},
"MySubscription": {
"Type": "AWS::SNS::Subscription",
"Properties": {
"TopicArn" : { "Ref" : "EmailSNSTopic" },
"${details}"
}
}
},
"Outputs" : {
"ARN" : {
"Description" : "Email SNS Topic ARN",
"Value" : { "Ref" : "EmailSNSTopic" }
}
}
}

我正在尝试通过 terrform 进行调用。

但我不断收到此错误错误:“template_body”包含无效的 JSON:查找对象键字符串开头的字符“{”无效

我的 Terraform 配置如下所示。

provider "aws" {
region = "eu-west-2"
}

data "template_file" "sns_stack" {
template = file("${path.module}/templates/email-sns-stack.json.tpl")
vars = {
display_name = var.display_name
details = join(",", formatlist("{ \"Endpoint\": \"%s\", \"Protocol\": \"%s\" }", var.email_list, var.protocol))
}
}

resource "aws_cloudformation_stack" "sns_topic" {
name = var.stack_name
template_body = data.template_file.sns_stack.rendered
tags = merge(
map("Name", var.stack_name)
)
}

我的variables.tf看起来像这样

  default     = "Admin"
}
variable "email_list" {
default = [
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5a3aaaa85a3aaaaeba6aaa8" rel="noreferrer noopener nofollow">[email protected]</a>",
"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b595a497b595a4915585456" rel="noreferrer noopener nofollow">[email protected]</a>"
]
}

variable "protocol" {
default = "email"
}
variable "stack_name" {
default = "sns-test"
}

我希望 ${details} 应该吐出我的端点和协议(protocol),但事实并非如此。

我做错了什么?

最佳答案

您想要实现的目标相当复杂,但可行。您可以使用以下模板来执行此操作:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources" : ${jsonencode(
merge({for idx, email_address in email_list:
"EmailSubs${idx}" => {
Type = "AWS::SNS::Subscription"
Properties = {
"Endpoint" = email_address
"Protocol" = protocol
"TopicArn" = { "Ref" = "EmailSNSTopic" }
}
}},
{
"EmailSNSTopic" = {
"Type" = "AWS::SNS::Topic",
"Properties" = {
"DisplayName" = "${display_name}"
}
}}

))},
"Outputs" : {
"ARN" : {
"Description" : "Email SNS Topic ARN",
"Value" : { "Ref" : "EmailSNSTopic" }
}
}

}

和 TF 代码:

locals {

template_body = templatefile("./email-sns-stack2.json.tpl", {
display_name = var.display_name
email_list = var.email_list
protocol = var.protocol
})

}

resource "aws_cloudformation_stack" "sns_topic" {
name = var.stack_name
template_body = local.template_body
tags = merge(
map("Name", var.stack_name)
)
}

关于json - 模板正文包含无效的 JSON : invalid character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65738453/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com