将 ECS containerDefinition 命令移动到本地 - Terraform
Moving ECS containerDefinition command to locals - Terraform
我正在使用 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 上做过类似的事情,但从来没有用过数组,所以我遇到了这个错误 (如果我删除换行符,那么错误会从“[=38”指向“/” =]")
! Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character '\n' in string literal
on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition":
147: container_definitions = var.container_definitions
如果我删除“[”:
! Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character 't' after array element
on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition":
147: container_definitions = var.container_definitions
我不知道我还能尝试什么(我试过删除“[”,没有“DEFINITION”等...)
你们中有人尝试过声明一个数组并在 ECS 容器定义上使用它吗?
非常感谢。
您在
中使用引号
"command": "${local.prometheus_container_command}"
将导致 command
成为:
"command": "["cpmd1","cmd2", "cmd3"]"
请尝试:
"command": ${local.prometheus_container_command}
或
"command": ${jsonencode(local.prometheus_container_command})
我正在使用 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 上做过类似的事情,但从来没有用过数组,所以我遇到了这个错误 (如果我删除换行符,那么错误会从“[=38”指向“/” =]")
! Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character '\n' in string literal on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition": 147: container_definitions = var.container_definitions
如果我删除“[”:
! Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character 't' after array element on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition": 147: container_definitions = var.container_definitions
我不知道我还能尝试什么(我试过删除“[”,没有“DEFINITION”等...)
你们中有人尝试过声明一个数组并在 ECS 容器定义上使用它吗? 非常感谢。
您在
中使用引号"command": "${local.prometheus_container_command}"
将导致 command
成为:
"command": "["cpmd1","cmd2", "cmd3"]"
请尝试:
"command": ${local.prometheus_container_command}
或
"command": ${jsonencode(local.prometheus_container_command})