将命令作为变量传递给 ECS 任务定义
Pass command as variable to ECS task definition
有没有办法将 Docker 命令作为 Terraform 变量传递给在 Terraform 中定义的 ECS 任务定义?
根据 aws_ecs_task_definition
documentation, the container_definitions
property is an unparsed JSON object that's an array of 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
}
如果没有从根模块传递任何内容,您可以尝试以下方法将 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 = ""
}
有没有办法将 Docker 命令作为 Terraform 变量传递给在 Terraform 中定义的 ECS 任务定义?
根据 aws_ecs_task_definition
documentation, the container_definitions
property is an unparsed JSON object that's an array of 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
}
如果没有从根模块传递任何内容,您可以尝试以下方法将 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 = ""
}