如何创建任务定义的新修订版本,而不是使用 Terraform 创建新的任务定义本身

问题描述 投票:0回答:1

我有一个正在运行多个任务的 AWS 集群,每个任务都是前一个任务的修订版,因为除了 docker 命令传递给它们的路径更改之外,这些修订版是相同的。我想使用 Terraform 自动化此过程。我想创建现有任务的新修订版,并在现有集群中运行新任务,该集群选择最新的任务修订版。但现在我只能在每次运行脚本时创建一个新的任务定义和一个新的 ECS 集群。我不确定我错过了什么。

这是我的脚本的相关部分

# main.tf

# Provider configuration
provider "aws" {
  region = "ap-northeast-1"
}

# Read input.yaml file
data "local_file" "input" {
  filename = "input.yaml"
}

# Parse input.yaml file
locals {
  input_data = yamldecode(data.local_file.input.content)
}

# Retrieve existing ECS task definition
data "aws_ecs_task_definition" "existing_task" {
  task_definition = "modbus-simulator-fargate-task"
}

# Create new ECS task definition based on existing one with modifications
resource "aws_ecs_task_definition" "modbus_simulator" {
  family                   = data.aws_ecs_task_definition.existing_task.family
  task_role_arn            = data.aws_ecs_task_definition.existing_task.task_role_arn
  execution_role_arn       = data.aws_ecs_task_definition.existing_task.execution_role_arn
  network_mode             = data.aws_ecs_task_definition.existing_task.network_mode
  requires_compatibilities = ["FARGATE"]  # Add FARGATE here
  cpu                      = 1024      # Specify CPU here
  memory                   = 3072       # Specify memory here

  container_definitions = jsonencode([
    {
      "name": "client-server-simulator",
      "image": "337039605624.dkr.ecr.ap-northeast-1.amazonaws.com/client-server-simulator:26-june-2024",
      "cpu": 0,
      "portMappings": [
        {
          "name": "client-server-simulator-80-tcp",
          "containerPort": 80,
          "hostPort": 80,
          "protocol": "tcp",
          "appProtocol": "http"
        }
      ],
      "essential": true,
      "command": [
        "--s3-path",
        local.input_data[0].s3_path,
        "--modbus-ip",
        local.input_data[0].modbus_ip,
        "--modbus-port",
        local.input_data[0].modbus_port,
        "--interval",
        local.input_data[0].interval,
        "--edge-id",
        local.input_data[0].edge_id,
        "--location",
        local.input_data[0].location,
        "--company",
        local.input_data[0].company
      ],
      "environment": [],
      "mountPoints": [],
      "volumesFrom": [],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/ecs/modbus-simulator-fargate-task",
          "awslogs-create-group": "true",
          "awslogs-region": "ap-northeast-1",
          "awslogs-stream-prefix": "ecs"
        }
      },
      "systemControls": []
    }
  ])
}

# Update ECS service to use the new task definition revision
resource "aws_ecs_service" "modbus_simulator_service" {
  name            = "modbus-simulator-service"
  cluster         = "modbus-simulator-cluster" # use the existing cluster
  task_definition = aws_ecs_task_definition.modbus_simulator.arn
  desired_count   = 1

  # Update only if there's a change in the task definition
  lifecycle {
    ignore_changes = [task_definition]
  }
}

[编辑]:一条评论要求我提及我存储状态的位置。如果您所说的状态是指

.tfstate
文件,那么它正在我的系统上本地创建并存储

我已尝试查看存储库中提供的示例。

terraform amazon-ecs terraform-provider-aws
1个回答
0
投票

aws_ecs_task_definition

的当前实现中,我注意到container_definitions
属性被标记为
ForceNew

该标志是 Terraform 的 SDK 提供的快捷方式,它使 SDK 逻辑(代表提供者工作)告诉 Terraform Core,如果不销毁当前对象并创建一个新对象来替换它,则无法更改此属性。您可以通过用

# forces replacement

 注释该属性的更改来识别 Terraform 计划中此行为的影响。

使用该标志后,提供者将强制 Terraform Core 将此更改视为两个单独的操作:销毁现有对象,创建一个新对象。这意味着提供者不可能将其表示为对现有对象的更新,并且我认为,为了让提供者将其建模为现有对象的新修订版而不是全新的修订版,这是必需的.

因此,不幸的是,我被迫得出结论,你想要实现的目标目前是不可能的。 AWS 提供商对此对象类型的建模是,其容器定义一旦创建就不可变。

我认为您想要的行为需要更改提供商的实现,因此如果尚未打开类似的功能请求,您可能会向提供商提出功能请求。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.