How do you ignore a nested field in Terraform?
T

1

5

Terraform newbie here. I have a code here for the ECS schedule task. Whenever I change this and apply the change, the first version of task definition is getting set in the ECS task. So I tried adding the lifecycle method to it.

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

Tried:

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

and

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

}

How do you ignore a nested field via lifecycle?

Thereabouts answered 29/4, 2020 at 19:12 Comment(0)
T
6

Found the solution. This works

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

The unusual syntax for me but this is how it is :).

Thereabouts answered 29/4, 2020 at 21:46 Comment(1)
That is the 0.11-style syntax that is still supported for some backward compatibility. You can also write ignore_changes = [ecs_target[0].task_definition_arn] if the new-style index syntax feels less unusual. :DLashondalashonde

© 2022 - 2024 — McMap. All rights reserved.