有没有办法将Docker命令作为Terraform变量传递给Terraform中定义的ECS任务定义?
根据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
}
如果根模块没有传递任何内容,您可以尝试以下方法将command
作为具有模板条件的变量。service.json
[
{
...
],
%{ if command != "" }
"command" : [${command}],
%{ endif ~}
...
}
]
集装箱.tf
data "template_file" "container_def" {
count = 1
template = file("${path.module}/service.json")
vars = {
command = var.command != "" ? join(",", formatlist(""%s"", var.command)) : ""
}
}
主.tf
module "example" {
...
command = ["httpd", "-f", "-p", "8080"]
...
}
变量.tf
variable "command" {
default = ""
}