Maaz Zindani

Loading the index…

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

EC2 refuses to detach a network interface

ErrorCan't detach network interfaces

Short answer

Three things stop a detach. The primary interface of an instance can never be detached. A requester-managed interface belongs to an AWS service such as RDS or a NAT gateway and can only go when that service's resource is deleted. And the CLI wants the attachment ID, not the interface ID.

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

Fix it fast

  1. 1Find out which cause you have

    Describe the interface. Its attachment, its device index and whether a service manages it tell you which cause applies.

    aws ec2 describe-network-interfaces --network-interface-ids <eni-id> \
      --query "NetworkInterfaces[0].[Attachment.AttachmentId,Attachment.DeviceIndex,RequesterManaged,Description]"
  2. 2Match what you see, then jump to its fix

    1. 1Device index 0, attached to the instance as its default interfaceIt is the primary network interface of the instanceFix
    2. 2RequesterManaged is true, and the description names a service, for example a VPC endpoint or a NAT gatewayAn AWS service manages the interface (requester-managed)Fix
    3. 3The command was run with an eni-... value after --attachment-idThe command got the interface ID instead of the attachment IDFix

What does the error look like?

  • Can't detach network interfaces
  • You can't detach a primary network interface from an instance

Cause 1 of 3

It is the primary network interface of the instance

1Check

Every instance has a default network interface called the primary network interface. AWS does not allow it to be detached.

aws ec2 describe-instances --instance-ids <instance-id> --query "Reservations[0].Instances[0].NetworkInterfaces[*].[NetworkInterfaceId,Attachment.DeviceIndex]"

2Fix

Keep the primary interface. Detach a secondary interface instead, or terminate the instance, or launch a replacement instance with the setup you want and move the workload.

# detach a secondary interface (device index 1 or higher)
aws ec2 detach-network-interface --attachment-id <eni-attach-id>

3Verify

Describe the interface again and see that it no longer has an attachment.

aws ec2 describe-network-interfaces --network-interface-ids <eni-id> --query "NetworkInterfaces[0].Attachment"

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

An AWS service manages the interface (requester-managed)

1Check

A requester-managed interface is created by a service in your VPC for its own resource, such as an RDS DB instance, a NAT gateway or an interface VPC endpoint. You can view it and edit its tags, but you cannot attach or detach it.

aws ec2 describe-network-interfaces \
  --filters "Name=requester-managed,Values=true" \
  --query "NetworkInterfaces[*].[NetworkInterfaceId,Description,InterfaceType]" --output table

2Fix

Delete the resource that owns the interface. The service then detaches and deletes the interface itself. If the service detached it but left it behind, you can delete the detached interface.

# after deleting the owning resource, if a detached interface remains
aws ec2 delete-network-interface --network-interface-id <eni-id>

3Verify

List requester-managed interfaces again and confirm yours is gone.

aws ec2 describe-network-interfaces --network-interface-ids <eni-id>

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

The command got the interface ID instead of the attachment ID

1Check

detach-network-interface requires --attachment-id, which looks like eni-attach-..., not the eni-... ID of the interface itself.

aws ec2 describe-network-interfaces --network-interface-ids <eni-id> --query "NetworkInterfaces[0].Attachment.AttachmentId" --output text

2Fix

Pass the attachment ID. Use --force only as a last resort for an interface on a failed instance.

aws ec2 detach-network-interface --attachment-id eni-attach-66c4350a

3Verify

The command prints no output, and the interface no longer has an attachment.

aws ec2 describe-network-interfaces --network-interface-ids <eni-id> --query "NetworkInterfaces[0].Attachment"

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Put a clear Name tag on every secondary interface you create, so it is not confused with a service's interface.
  • Delete service resources (endpoints, NAT gateways, DB instances) through their own service, never by hunting for their interfaces.
  • Keep infrastructure in code, so interfaces are created and removed together with what uses them.

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.