Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

AWS will not detach or delete an internet gateway

ErrorDetaching an internet gateway fails while the VPC still has public addresses in use

Short answer

An internet gateway cannot be deleted while it is attached to a VPC, and it cannot be detached while the VPC still has resources with public IP addresses or Elastic IP addresses. Find those addresses, release or remove them, detach the gateway, then delete it.

  • Applies to AWS CLI 2, Amazon VPC
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether the gateway is attached, and to which VPC.

    aws ec2 describe-internet-gateways \
      --internet-gateway-ids igw-0d0fb496b3EXAMPLE \
      --query 'InternetGateways[0].Attachments'
  2. 2Match what you see, then jump to its fix

    1. 1Delete is refused and the attachment list shows a VPC id in state availableThe gateway is still attached to the VPCFix
    2. 2Delete works only after detach, and detach itself is refused while addresses remainResources in the VPC still have public IP or Elastic IP addressesFix

What does the error look like?

  • Detaching an internet gateway fails while the VPC still has public addresses in use
  • Deleting an internet gateway fails while it is attached to a VPC

Cause 1 of 2

The gateway is still attached to the VPC

1Check

Delete only works on a detached gateway. The first-step command lists any attachment.

aws ec2 describe-internet-gateways --query 'InternetGateways[].[InternetGatewayId,Attachments[0].VpcId]' --output table

2Fix

Detach it from its VPC first, then delete it.

aws ec2 detach-internet-gateway \
  --internet-gateway-id igw-0d0fb496b3EXAMPLE \
  --vpc-id vpc-0a60eb65b4EXAMPLE
aws ec2 delete-internet-gateway \
  --internet-gateway-id igw-0d0fb496b3EXAMPLE

3Verify

The gateway no longer appears in the listing.

aws ec2 describe-internet-gateways --internet-gateway-ids igw-0d0fb496b3EXAMPLE

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 2

Resources in the VPC still have public IP or Elastic IP addresses

1Check

AWS refuses to detach while the VPC contains resources with associated public IP or Elastic IP addresses; a running instance with one is enough. In the EC2 console, check Elastic IPs and the instances in that VPC that show a public IPv4 address.

aws ec2 describe-internet-gateways --internet-gateway-ids igw-0d0fb496b3EXAMPLE

2Fix

Release the Elastic IPs and stop or terminate the instances that hold public addresses, or the other resources that use them, then detach the gateway.

# EC2 console: release the Elastic IPs, stop or terminate instances that hold
# a public address, then detach the gateway with:
aws ec2 detach-internet-gateway --internet-gateway-id igw-0d0fb496b3EXAMPLE --vpc-id vpc-0a60eb65b4EXAMPLE

3Verify

Run the detach command again; it returns no output on success.

aws ec2 detach-internet-gateway \
  --internet-gateway-id igw-0d0fb496b3EXAMPLE \
  --vpc-id vpc-0a60eb65b4EXAMPLE

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Delete the VPC's resources (instances, NAT gateways, load balancers) before its internet gateway.
  • Tag Elastic IPs with their owner so you can tell which ones are safe to release.
  • Use infrastructure as code so teardown follows dependency order.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

Apache has a certificate, but https:// does not open

No line with :443 in the output of ss -ltnp

HTTPS needs four things at once: mod_ssl loaded, Apache listening on 443, a virtual host on 443 with SSLEngine on, and a firewall that lets 443 in.

4 causes, each with a fix

Servers

AWS CLI says you must specify a region

You must specify a region. You can also configure your region by running "aws configure".

The AWS CLI sends every request to one region and has none to use.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.