terraform - 如何将两个模板文件连接成一个文件并传递到 terraform 中的 ECS 容器任务定义
问题描述
我有两个模板文件。我想将这些模板文件合并为一个,并将它们传递到aws_ecs_task_definition资源中的 ECS 属性container_definitions 。
Terraform 版本 => v0.14.9
nginx.tpl.json:
[
{
"name": "nginx",
"image": "public.ecr.aws/nginx/nginx:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
]
redis.json.tpl:
[
{
"name": "redis",
"image": "public.ecr.aws/peakon/redis:6.0.5",
"portMappings": [
{
"containerPort": 6379,
"hostPort": 6379,
"protocol": "tcp"
}
]
}
]
当像下面这样手动组合两个模板文件时,它正在工作。但是随着 Terraform concat或format出现错误。
[
{
"name": "nginx",
"image": "public.ecr.aws/nginx/nginx:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
},
{
"name": "redis",
"image": "public.ecr.aws/peakon/redis:6.0.5",
"portMappings": [
{
"containerPort": 6379,
"hostPort": 6379,
"protocol": "tcp"
}
]
}
]
data "template_file" "ecs_task" {
template = format("%s%s",file("./ecs/templates/nginx.json.tpl"),
file("./ecs/templates/redis.json. tpl")
)
} => Here I need to combine the two template files and then pass them onto the container_definitions to the below resource.
resource "aws_ecs_task_definition" "testapp" {
family = "testapp"
network_mode = "awsvpc"
cpu = 256
memory = 512
container_definitions = data.template_file.ecs_task.rendered # I'm getting the following error.
}
错误:invalid character '}' looking for the beginning of object key string
有人可以帮我解决这个问题吗?
解决方案
更新
从文件中删除括号
{
"name": "nginx",
"image": "public.ecr.aws/nginx/nginx:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
和
{
"name": "redis",
"image": "public.ecr.aws/peakon/redis:6.0.5",
"portMappings": [
{
"containerPort": 6379,
"hostPort": 6379,
"protocol": "tcp"
}
]
}
然后代替 "%s%s"
. 似乎您缺少逗号: "[%s,%s]"
。
推荐阅读
- c - 为什么我的循环永远打印出同样的东西?
- math - 找到产生给定值的最小硬币集
- python - 表示空匿名函数的最惯用方式?
- git - Git 推送到主分支。未找到返回存储库。但我能够进行更改,甚至解决冲突
- python - 如何优雅地组织这个多线程 python 代码?
- ios - 在具有某些 AVAudioSession 配置的 iOS 15 中,Core Haptics 无法播放
- java - 为什么我不能在员工部门输入字符串字母?
- linux - 与 find 一起使用的 exec mv 命令会删除文件吗?
- javascript - 将数据从循环传递到前端
- machine-learning - 在 Slurm 中,如何批量提交多个实验运行,并一个接一个地连续执行?