-
Notifications
You must be signed in to change notification settings - Fork 634
Description
Checkboxes for prior research
- I've gone through Developer Guide and API reference
- I've checked AWS Forums and StackOverflow.
- I've searched for previous similar issues and didn't find any solution.
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
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