【发布时间】:2021-03-28 09:57:49
【问题描述】:
我正在与 Terraform 一起使用一些容器部署 ECS 服务。代码很长,所以我试图将所有逻辑移至 locals.tf。
现在,我在容器定义中的代码是这样的:
module "prometheus" {
source = "../aws-modules/ecs-service"
[... more code ...]
volumes = local.prometheus_volumes
cpu = var.prometheus_cpu
memory = var.prometheus_memory
container_definitions = <<DEFINITION
[
{
"image": "${var.prometheus_container_image}",
"name": "${lower(local.prometheus_name)}",
"networkMode": "${var.prometheus_network_mode}",
"portMappings": [
{
"containerPort": ${var.prometheus_container_port},
"hostPort": ${var.prometheus_container_port},
"protocol": "tcp"
}
],
"command": [
"/bin/prometheus",
"--config.file=/etc/prometheus/prometheus.yml",
"--web.enable-lifecycle",
"--storage.tsdb.path=/prom_data"
],
[... more code ...]
}
]
DEFINITION
这是按预期工作的,但我试图让它像这样:
module "prometheus" {
source = "../aws-modules/ecs-service"
[... more code ...]
volumes = local.prometheus_volumes
cpu = var.prometheus_cpu
memory = var.prometheus_memory
container_definitions = <<DEFINITION
[
{
"image": "${var.prometheus_container_image}",
"name": "${lower(local.prometheus_name)}",
"networkMode": "${var.prometheus_network_mode}",
"portMappings": [
{
"containerPort": ${var.prometheus_container_port},
"hostPort": ${var.prometheus_container_port},
"protocol": "tcp"
}
],
"command": "${local.prometheus_container_command}",
[... more code ...]
}
]
DEFINITION
并在 locals.tf 上声明:
prometheus_container_command = <<DEFINITION
[
"/bin/prometheus",
"--config.file=/etc/prometheus/prometheus.yml",
"--web.enable-lifecycle",
"--storage.tsdb.path=/prom_data"
]
DEFINITION
我在 containerDefinitions 上做过类似的事情,但从来没有使用数组,所以我遇到了这个错误 (如果我删除换行符,那么错误会从“/bin/prometheus”指向“/”)
!错误:ECS 任务定义 container_definitions 无效:解码 JSON 时出错:字符串文字中的字符“\n”无效 在 ../aws-modules/ecs-service/main.tf 第 147 行,在资源“aws_ecs_task_definition”“ecs_task_definition”中: 147:容器定义= var.container_definitions
如果我删除“[”:
!错误:ECS 任务定义 container_definitions 无效:解码 JSON 时出错:数组元素后的字符“t”无效 在 ../aws-modules/ecs-service/main.tf 第 147 行,在资源“aws_ecs_task_definition”“ecs_task_definition”中: 147:容器定义= var.container_definitions
我不知道我还能尝试什么(我试过删除“[”,没有“DEFINITION”等...)
你们中的一些人尝试过声明一个数组并在 ECS containerDefinition 上使用它? 非常感谢。
【问题讨论】:
标签: amazon-web-services terraform amazon-ecs terraform-provider-aws