Terraform AWS:ECS DOSN' t部署最新的任务定义

发布于 2025-02-13 17:59:15 字数 2514 浏览 1 评论 0原文

我在Terraform部署方面面临一个小问题。

在部署期间创建了一个新的任务定义。然后,我也在Web控制台中看到了这一点。 ECS现在实际上应该使用此新任务定义并进行更新。相反,最新的修订再次部署。

我的期望是,如果我创建一个新的任务定义并将其传递给ECS,那将被部署。

不幸的是,我不知道该从这里去哪里。

我创建了一个具有以下配置的ECS服务:

resource "aws_ecs_service" "service" {
  name                               = "${var.appPrefix}-api-service-${var.envPrefix}"
  cluster                            = var.ecsClusterId
  task_definition                    = aws_ecs_task_definition.ecsTaskDefinition.arn
  platform_version                   = "1.3.0"
  desired_count                      = 1
  deployment_minimum_healthy_percent = 50
  deployment_maximum_percent         = 200
  launch_type                        = "FARGATE"
  scheduling_strategy                = "REPLICA"
  wait_for_steady_state              = true
  force_new_deployment               = true

  network_configuration {
    security_groups  = [var.ecsSecurityGroupId]
    subnets          = [var.privateSubnet1Id, var.privateSubnet2Id]
    assign_public_ip = false
  }

  load_balancer {
    target_group_arn = var.targetGroupId
    container_name   = var.containerName
    container_port   = var.httpPort
  }

  lifecycle {
    ignore_changes = [task_definition, desired_count]
  }
}

我的任务定义如下:

resource "aws_ecs_task_definition" "ecsTaskDefinition" {
  family                   = "${var.appPrefix}-api-ECSTaskDefinition"
  task_role_arn            = var.ecsTaskRoleArn
  execution_role_arn       = var.ecsTaskExecutionRoleArn
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "512"
  memory                   = "1024"
  container_definitions    = <<DEFINITION
[...]
DEFINITION
}

〜编辑: 这是Terraform日志:

# module.ecs_api_module.aws_ecs_task_definition.ecsTaskDefinition must be replaced
-/+ resource "aws_ecs_task_definition" "ecsTaskDefinition" {
~ arn                      = "arn:aws:ecs:*****:********:task-definition/project-name-api-ECSTaskDefinition:507" -> (known after apply)
~ container_definitions    = (sensitive) # forces replacement
~ id                       = "project-name-api-ECSTaskDefinition" -> (known after apply)
- ipc_mode                 = "" -> null
- pid_mode                 = "" -> null
~ revision                 = 507 -> (known after apply)
- tags                     = {} -> null
~ tags_all                 = {} -> (known after apply)
# (8 unchanged attributes hidden)
}

I'm facing a little problem with my terraform deployment.

A new task definition is created during deployment. I then also see this in the web console.
ecs should now actually use this new task definition and update it. Instead, the most recent revision is deployed again.

My expectation would be that if I create a new task definition and pass the arn from that to ecs, that it will be deployed.

Unfortunately, I have no idea where to go from here.

I have created an ecs service with the following config:

resource "aws_ecs_service" "service" {
  name                               = "${var.appPrefix}-api-service-${var.envPrefix}"
  cluster                            = var.ecsClusterId
  task_definition                    = aws_ecs_task_definition.ecsTaskDefinition.arn
  platform_version                   = "1.3.0"
  desired_count                      = 1
  deployment_minimum_healthy_percent = 50
  deployment_maximum_percent         = 200
  launch_type                        = "FARGATE"
  scheduling_strategy                = "REPLICA"
  wait_for_steady_state              = true
  force_new_deployment               = true

  network_configuration {
    security_groups  = [var.ecsSecurityGroupId]
    subnets          = [var.privateSubnet1Id, var.privateSubnet2Id]
    assign_public_ip = false
  }

  load_balancer {
    target_group_arn = var.targetGroupId
    container_name   = var.containerName
    container_port   = var.httpPort
  }

  lifecycle {
    ignore_changes = [task_definition, desired_count]
  }
}

My task definition looks like this:

resource "aws_ecs_task_definition" "ecsTaskDefinition" {
  family                   = "${var.appPrefix}-api-ECSTaskDefinition"
  task_role_arn            = var.ecsTaskRoleArn
  execution_role_arn       = var.ecsTaskExecutionRoleArn
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "512"
  memory                   = "1024"
  container_definitions    = <<DEFINITION
[...]
DEFINITION
}

~edit:
this is the terraform log:

# module.ecs_api_module.aws_ecs_task_definition.ecsTaskDefinition must be replaced
-/+ resource "aws_ecs_task_definition" "ecsTaskDefinition" {
~ arn                      = "arn:aws:ecs:*****:********:task-definition/project-name-api-ECSTaskDefinition:507" -> (known after apply)
~ container_definitions    = (sensitive) # forces replacement
~ id                       = "project-name-api-ECSTaskDefinition" -> (known after apply)
- ipc_mode                 = "" -> null
- pid_mode                 = "" -> null
~ revision                 = 507 -> (known after apply)
- tags                     = {} -> null
~ tags_all                 = {} -> (known after apply)
# (8 unchanged attributes hidden)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-02-20 17:59:17

ecs_service资源或最少删除task_definition中删除ignore_changes属性。

  lifecycle {
    ignore_changes = [desired_count]
  }

Remove the ignore_changes attribute from the ecs_service resource or least remove the task_definition.

  lifecycle {
    ignore_changes = [desired_count]
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文