将命令作为变量传递给ECS任务定义

问题描述 投票:0回答:1

有没有办法将Docker命令作为Terraform变量传递给在Terraform中定义的ECS任务定义?

amazon-web-services docker terraform amazon-ecs terraform-provider-aws
1个回答
0
投票

你可以试试下面的方法,把 command 作为一个带有模板条件的变量,如果没有从根模块传来任何东西。 service.json

[
  {
    ...
    ],
    %{ if command != "" }
    "command"  : [${command}],
    %{ endif ~}
    ...
  }
]

container.tf

data "template_file" "container_def" {
  count    = 1
  template = file("${path.module}/service.json")
  vars = {
    command        = var.command != "" ? join(",", formatlist("\"%s\"", var.command)) : ""
  }
}

main.tf

module "example" {
...
     command                 = ["httpd", "-f", "-p", "8080"]
...
}

变量.tf

variable "command" {
  default = ""
}

2
投票

根据 aws_ecs_task_definition 文件జజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజజ container_definitions 属性是一个未解析的JSON对象,它是一个数组的 容器定义 就像你直接传递给AWS API一样。 该对象的一个属性是 command.

在一定程度上解读了文档,你会得到一个类似于任务定义的示例。

resource "aws_ecs_task_definition" "service" {
  family                = "service"
  container_definitions = <<DEFINITIONS
[
  {
    "name": "first",
    "image": "service-first",
    "command": ["httpd", "-f", "-p", "8080"],
    "cpu": 10,
    "memory": 512,
    "essential": true
  }
]
DEFINITIONS
}
© www.soinside.com 2019 - 2024. All rights reserved.