【问题标题】:Moving ECS containerDefinition command to locals - Terraform将 ECS containerDefinition 命令移动到本地 - Terraform
【发布时间】: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


    【解决方案1】:

    你对引号的使用

    "command": "${local.prometheus_container_command}"
    

    将导致command 成为:

    "command": "["cpmd1","cmd2", "cmd3"]"
    

    请尝试:

     "command": ${local.prometheus_container_command}
    

     "command": ${jsonencode(local.prometheus_container_command})
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 2023-02-04
      • 2020-12-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多