terraform 应用传递变量来部署特定的 ECS

terraform apply pass variable to deploy specific ECS

我有多个ECS。是否可以将变量传递给 ecs-container-definition.json 以部署到特定的 ECS?

例如地形应用-var 'deploy=aws-ecs-backend'

首先,这里有一些重叠的概念。您没有指定您使用的是哪个版本的 Terraform,所以我假设它是相对较新的。

您可能指的 Terraform 资源是 ecs_task_definition。此资源采用以下格式(来自文档):

resource "aws_ecs_task_definition" "service" {   
    family = "service"
    container_definitions = jsonencode([

     //ommitted for brevity

    ])

}

您似乎正在使用 file or templatefile 函数将名为 ecs-container-definition.json 的文件的内容嵌入到您的资源中。从 Terraform 的角度来看,您不会对此文件应用更改,而是将更改应用于引用它的资源(aws_ecs_task_definition)。

您尝试进行的过程是resource targeting,它是通过在terraform apply命令中指定目标资源来调用的(例如)。这是一个示例,如果您的 ecs_task_definition 资源被称为 myservice 您将拥有以下内容:

resource "aws_ecs_task_definition" "myservice" {   
    family = "service"
    container_definitions = file('./templates/aws_ecs_task_definition')
    .....
}

然后,您可以使用以下命令(运行 来自与包含 .tf 文件的相同目录:

将更改应用到此资源

terraform plan -target="aws_ecs_task_definition.myservice"(查看计划的更改)

terraform apply -target="aws_ecs_task_definition.myservice"(应用更改)

请注意,这是一种 Terraform 反模式,如文档所述 in the opening paragraph:

Occasionally you may want to only apply part of a plan, such as situations where Terraform's state has become out of sync with your resources due to a network failure, a problem with the upstream cloud platform, or a bug in Terraform or its providers.

通常最佳做法是让 Terraform“干净地应用”,因为所有状态都与您的存储库内容同步。