3

Terraform新手在这里。我这里有一个 ECS 计划任务的代码。每当我更改并应用更改时,都会在 ECS 任务中设置任务定义的第一个版本。所以我尝试向它添加生命周期方法。

resource "aws_cloudwatch_event_target" "sqs" {
  rule      = aws_cloudwatch_event_rule.sqs.name
  target_id = local.namespace
  arn       = aws_ecs_cluster.app.arn
  role_arn  = aws_iam_role.ecsRole.arn
  input     = "{}"

  ecs_target {
    task_count          = 1
    task_definition_arn = aws_ecs_task_definition.sqs.arn
    launch_type         = "FARGATE"
    platform_version    = "LATEST"

    network_configuration {
      security_groups = [aws_security_group.nsg_task.id]
      subnets         = split(",", var.private_subnets)
    }
  }
}

试过:

    resource "aws_cloudwatch_event_target" "sqs" {
      rule      = aws_cloudwatch_event_rule.sqs.name
      target_id = local.namespace
      arn       = aws_ecs_cluster.app.arn
      role_arn  = aws_iam_role.ecsRole.arn
      input     = "{}"

      ecs_target {
        task_count          = 1
        task_definition_arn = aws_ecs_task_definition.sqs.arn
        launch_type         = "FARGATE"
        platform_version    = "LATEST"

        network_configuration {
          security_groups = [aws_security_group.nsg_task.id]
          subnets         = split(",", var.private_subnets)
        }

        lifecycle {
          ignore_changes = [task_definition_arn]
        }
      }
    }

resource "aws_cloudwatch_event_target" "sqs" {
  rule      = aws_cloudwatch_event_rule.sqs.name
  target_id = local.namespace
  arn       = aws_ecs_cluster.app.arn
  role_arn  = aws_iam_role.ecsRole.arn
  input     = "{}"

  ecs_target {
    task_count          = 1
    task_definition_arn = aws_ecs_task_definition.sqs.arn
    launch_type         = "FARGATE"
    platform_version    = "LATEST"

    network_configuration {
      security_groups = [aws_security_group.nsg_task.id]
      subnets         = split(",", var.private_subnets)
    }
  }

  lifecycle {
    ignore_changes = [ecs_target.task_definition_arn]
  }

}

您如何通过生命周期忽略嵌套字段?

4

1 回答 1

3

找到了解决方案。这有效

resource "aws_cloudwatch_event_target" "sqs" {
  rule      = aws_cloudwatch_event_rule.sqs.name
  target_id = local.namespace
  arn       = aws_ecs_cluster.app.arn
  role_arn  = aws_iam_role.ecsRole.arn
  input     = "{}"

  ecs_target {
    task_count          = 1
    task_definition_arn = aws_ecs_task_definition.sqs.arn
    launch_type         = "FARGATE"
    platform_version    = "LATEST"

    network_configuration {
      security_groups = [aws_security_group.nsg_task.id]
      subnets         = split(",", var.private_subnets)
    }
  }

  lifecycle {
    ignore_changes = [ecs_target.0.task_definition_arn]
  }
}

对我来说不寻常的语法,但这就是它的样子:)。

于 2020-04-29T21:46:43.830 回答