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

时间:2020-04-23 23:11:28

标签: amazon-web-services docker terraform amazon-ecs terraform-provider-aws

是否可以将Docker命令作为Terraform变量传递给Terraform中定义的ECS任务定义?

2 个答案:

答案 0 :(得分:2)

根据aws_ecs_task_definition documentationcontainer_definitions属性是未解析的JSON对象,它是container definitions的数组,您将直接传递给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
}

答案 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"]
...
}

variables.tf

variable "command" {
  default = ""
}
相关问题