Skip to content

feat: Add a lifecycle rule to allow vpc replacement #37

@relsqui

Description

@relsqui

Feature Request

Describe the Feature Request

Add a lifecycle rule to aws_internet_gateway.agentless_scan_gateway such that replacing aws_vpc.agentless_scan_vpc forces it to be recreated.

Is your feature request related to a problem? Please describe

When I tried to re-apply this module after some changes, Terraform created a plan that included replacing the VPC and updating the internet gateway attached to it. The plan failed with an error from the provider (that I forgot to copy, sorry) about not being able to destroy the VPC because other resources still depended on it.

By looking in the console I found that the dependent resource was the internet gateway. Terraform knows the gateway depends on the VPC, but it thinks it can just update the ID. In practice, it can't delete the VPC at all (and get the new ID) without destroying the gateway first.

Describe Preferred Solution

Adding an explicit lifecycle rule to the gateway fixed it for us:

resource "aws_internet_gateway" "agentless_scan_gateway" {
  count  = var.regional ? 1 : 0
  vpc_id = aws_vpc.agentless_scan_vpc[0].id
  lifecycle {
    replace_triggered_by = [
      aws_vpc.agentless_scan_vpc[0]
    ]
  }

  tags = {
    Name                     = "${local.prefix}-gw"
    LWTAG_SIDEKICK           = "1"
    LWTAG_LACEWORK_AGENTLESS = "1"
  }
}

Specifically, with this change, the gateway was destroyed first, then the VPC, and then they were both recreated without problems.

It's worth noting that this will also mean that the gateway gets replaced if the VPC updates, not just when it's being destroyed or recreated. I haven't thought through the side effects or edge cases of that at all, which is why this is an issue and not a pull request.

Additional Context

This is really a bug in Terraform, which is why I called this workaround a feature request. The chain of issues marked as duplicate of an upstream bug starts here and ends at this one, which is still open.

AWS's docs for troubleshooting the initial error are here, but their suggested script didn't actually find the gateway that was causing the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions