Maaz Zindani

Loading the index…

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

EC2 refuses to terminate an instance that has termination protection on

Your error

The instance i-1234567890abcdef0 may not be terminated. Modify its 'disableApiTermination' instance attribute1 more line it can show

Fix it fast

Most common cause, 1 of 3

Termination protection is turned on for this instance

Make sure this really is the instance to delete, then clear the attribute and terminate it.

Fix
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 \
  --no-disable-api-termination
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The message that an instance may not be terminated until you modify its 'disableApiTermination' attribute means termination protection is on. EC2 rejects TerminateInstances with OperationNotPermitted until you clear it: Actions, Instance settings, Change termination protection in the console, or modify-instance-attribute --no-disable-api-termination in the CLI. If a launch template or stack set it, change it there too.

  • Applies to Amazon EC2 (DisableApiTermination attribute), AWS CLI 2.42, AWS CloudFormation AWS::EC2::Instance
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the attribute on the instance that refused. A Value of true confirms termination protection; false means the request failed for another reason.

Run this first
aws ec2 describe-instance-attribute --instance-id i-1234567890abcdef0 \
  --attribute disableApiTermination

Then find the line you see in this list and open its fix:

  1. 1Termination protection is turned on for this instancedescribe-instance-attribute returns DisableApiTermination with Value true
  2. 2One protected instance blocks the rest of its Availability Zone in a batch requestYou terminated several instances in one call; those in one Availability Zone stayed running, including ones without protection
  3. 3A launch template or CloudFormation template sets DisableApiTerminationEvery new instance from the same launch template or stack arrives with protection on, so the error returns after each relaunch

What does the error look like?

  • The instance i-1234567890abcdef0 may not be terminated. Modify its 'disableApiTermination' instance attribute
  • OperationNotPermitted

Cause 1 of 3

Termination protection is turned on for this instance

You seedescribe-instance-attribute returns DisableApiTermination with Value true

Check

EC2 answers OperationNotPermitted and says the instance may not be terminated whenever the DisableApiTermination attribute is true. It blocks TerminateInstances from every interface, the console included. It is off by default, so it was switched on at launch or later to guard against an accidental delete.

Check
aws ec2 describe-instance-attribute --instance-id i-1234567890abcdef0 \
  --attribute disableApiTermination --query 'DisableApiTermination.Value'

Fix

Make sure this really is the instance to delete, then clear the attribute and terminate it. In the console: Instances, select it, Actions, Instance settings, Change termination protection, clear Enable, Save. The CLI flag reads backwards: --no-disable-api-termination turns protection off.

Fix
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 \
  --no-disable-api-termination
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

Verify

terminate-instances reports the instance moving to shutting-down, and it later shows as terminated.

Verify
aws ec2 describe-instances --instance-ids i-1234567890abcdef0 --query 'Reservations[].Instances[].State.Name'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

One protected instance blocks the rest of its Availability Zone in a batch request

You seeYou terminated several instances in one call; those in one Availability Zone stayed running, including ones without protection

Check

When a single TerminateInstances request covers instances in several Availability Zones and one of them is protected, the request fails for every listed instance in that protected instance's zone. Instances in other zones still terminate. So an unprotected instance can survive only because it shared a zone with a protected one.

Check
for id in i-0aaaaaaaaaaaaaaa1 i-0aaaaaaaaaaaaaaa2 i-0aaaaaaaaaaaaaaa3; do
  echo "$id $(aws ec2 describe-instance-attribute --instance-id "$id" \
    --attribute disableApiTermination --query 'DisableApiTermination.Value' --output text)"
done

Fix

Remove the protected instance from the list (or clear its protection if it should go too) and send the request again for the instances that remain.

Fix
aws ec2 terminate-instances --instance-ids i-0aaaaaaaaaaaaaaa2 i-0aaaaaaaaaaaaaaa3

Verify

Every instance in the new request reaches shutting-down.

Verify
aws ec2 describe-instances --instance-ids i-0aaaaaaaaaaaaaaa2 i-0aaaaaaaaaaaaaaa3 --query 'Reservations[].Instances[].[InstanceId,State.Name]'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

A launch template or CloudFormation template sets DisableApiTermination

You seeEvery new instance from the same launch template or stack arrives with protection on, so the error returns after each relaunch

Check

Launch templates and the AWS::EC2::Instance resource both carry a DisableApiTermination setting. Clearing the attribute on one instance does not change the template, so the next instance it launches is protected again, and a stack keeps declaring the value you cleared by hand.

Check
aws ec2 describe-launch-template-versions --launch-template-id lt-0123456789abcdef0 \
  --versions '$Default' --query 'LaunchTemplateVersions[].LaunchTemplateData.DisableApiTermination'
grep -n "DisableApiTermination" template.yaml

Fix

For a launch template, create a version with the setting off and make it the default. For CloudFormation, set DisableApiTermination to false in the template and update the stack first. The property updates with no interruption, and once it is false the instance is no longer protected when the stack deletes it.

Fix
aws ec2 create-launch-template-version --launch-template-id lt-0123456789abcdef0 \
  --source-version 1 --launch-template-data '{"DisableApiTermination":false}'
aws ec2 modify-launch-template --launch-template-id lt-0123456789abcdef0 --default-version 2

Verify

describe-launch-template-versions shows false for the default version, and an instance launched from it can be terminated directly.

Verify
aws ec2 describe-launch-template-versions --launch-template-id lt-0123456789abcdef0 --versions '$Default' --query 'LaunchTemplateVersions[].LaunchTemplateData.DisableApiTermination'

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Keep termination protection on only for named, long-lived servers, and record why in a tag on the instance so the next person knows it is deliberate.
  • Set DisableApiTermination in the template or launch template, never by hand, so the console and the code agree.
  • Terminate instances one Availability Zone at a time in cleanup scripts, or check the attribute first, so one protected server does not leave others running.

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 on the same stack: Dispatch Pro, Raheeb, Loade Driver.

Next step

Want this handled for good?

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