【问题标题】:How do you ignore a nested field in Terraform?如何忽略 Terraform 中的嵌套字段?
【发布时间】:2020-08-14 00:21:45
【问题描述】:

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]
  }

}

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

【问题讨论】:

    标签: amazon-web-services terraform amazon-ecs terraform-provider-aws infrastructure-as-code


    【解决方案1】:

    找到了解决办法。这行得通

    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]
      }
    }
    

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

    【讨论】:

    • 这是 0.11 样式的语法,它仍然支持一些向后兼容性。如果新式索引语法感觉不那么不寻常,您也可以写ignore_changes = [ecs_target[0].task_definition_arn]。 :D
    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2021-12-22
    • 1970-01-01
    • 2019-12-18
    • 2012-09-23
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    相关资源
    最近更新 更多