Skip to content

InternetGatewayAttachment attachment state value mismatch with AttachmentStatus typeΒ #7011

@ndvrich

Description

@ndvrich

Checkboxes for prior research

Describe the bug

According to https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ec2/Interface/InternetGatewayAttachment/, the attachment "State" value should be "available" (string) when the Internet Gateway is attached to a VPC. It is otherwise not returned. However, it is typed to be "AttachmentStatus", and your documentation shows that it is defined to have the following properties:

AttachmentStatus: {
    readonly attached: "attached"
    readonly attaching: "attaching";
    readonly detached: "detached";
    readonly detaching: "detaching";
}

None of these states will match the string 'available' when the Internet Gateway is attached to a VPC, and if I understand the documentation properly, there are no instances where any of these statuses will be present or can be matched.

Using the following code snipped from the AWS Landing Zone Accelerator fails to match an attached InternetGateway when comparing the attachment.State against AttachmentStatus.attached:

      // Retrieve and detach, delete IGWs
      for (const vpcId of defaultVpcIds) {
        let nextToken: string | undefined = undefined;
        do {
          const page = await throttlingBackOff(() =>
            ec2Client.send(
              new DescribeInternetGatewaysCommand({
                Filters: [{ Name: 'attachment.vpc-id', Values: [vpcId] }],
                NextToken: nextToken,
              }),
            ),
          );
          for (const igw of page.InternetGateways ?? []) {
            for (const attachment of igw.Attachments ?? []) {
              console.log(`Current attachment state of Internet Gateway ${igw.InternetGatewayId}: ${attachment.State}`);
              if (attachment.State == AttachmentStatus.attached) {
                console.log(`Detaching ${igw.InternetGatewayId}`);
                await throttlingBackOff(() =>
                  ec2Client.send(
                    new DetachInternetGatewayCommand({ InternetGatewayId: igw.InternetGatewayId!, VpcId: vpcId }),
                  ),
                );
              }
              console.warn(`${igw.InternetGatewayId} is not attached. Proceeding to delete.`);
              await throttlingBackOff(() =>
                ec2Client.send(
                  new DeleteInternetGatewayCommand({
                    InternetGatewayId: igw.InternetGatewayId!,
                  }),
                ),
              );
            }
          }
          nextToken = page.NextToken;
        } while (nextToken);

In particular, this piece of code is the culprit:

if (attachment.State == AttachmentStatus.attached) {

The solution in my case was to replace this statement with:

if ((attachment.State as string) === 'available') {

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

18.14.0

Reproduction Steps

See https://github.com/awslabs/landing-zone-accelerator-on-aws/blob/release/v1.12.1/source/packages/%40aws-accelerator/constructs/lib/aws-ec2/delete-default-vpc/index.ts#L107

Observed Behavior

An attempt to delete the default VPC by iteratively identifying and removing default VPC resources, including identifying attached Internet Gateways, detaching them, and then deleting them fails due to resource dependencies. This is because the code is attempting to identify an attached Internet Gateway to detach it before deleting it, and the Internet Gateway is not matching the "attached" status, so the subsequent attempt to delete it fails because it is still attached.

Expected Behavior

AttachmentStatus.attached should match the Internet Gateway's Attachments.State value which happens to only be "available" when attached to a VPC.

Possible Solution

Update the "AttachmentStatus" types and values to align with the expected response from DescribeInternetGatewaysCommand for Attachments.State.

Additional Information/Context

No response

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.closed-for-stalenessp2This is a standard priority issueresponse-requestedWaiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.service-apiThis issue is due to a problem in a service API, not the SDK implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions