有没有办法将Docker命令作为Terraform变量传递给在Terraform中定义的ECS任务定义?
你可以试试下面的方法,把 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 = ""
}
根据 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
}