如何忽略 Terraform 中的嵌套字段?

Posted

技术标签:

【中文标题】如何忽略 Terraform 中的嵌套字段?【英文标题】:How do you ignore a nested field in 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]
  


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

【问题讨论】:

【参考方案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

以上是关于如何忽略 Terraform 中的嵌套字段?的主要内容,如果未能解决你的问题,请参考以下文章

限制 terraform 嵌套资源中的 AWS 安全组设置

Terraform - 迭代嵌套地图

如何使用 terraform 将多个 IP 动态添加到 azure servicebus 防火墙

如何忽略错误但不跳过 redshift 复制命令中的行

嵌套 Rails 模型 - 创建时忽略 child_index

Terraform:来自yaml的嵌套for循环