Sub topic 18
Cleaning up to avoid charges
This chapter assumes you have launched an instance with the AWS CLI, attached an EBS volume, and know what a security group and an Elastic IP are. Cleanup is the last step of any lab. If you skip it, resources you forgot keep billing you.
What keeps costing money
Each resource type bills differently, so stopping an instance is not the same as cleaning up.
| Resource | Billed while | Stopped instance |
|---|---|---|
| Instance compute | State is running | No compute charge |
| EBS volume | It exists, attached or not | Still billed |
| EBS snapshot | It exists | Still billed |
| Public IPv4 address | It is allocated to you, in use or not | An Elastic IP is still billed |
A stopped instance stops compute charges, but its EBS volumes, snapshots, and Elastic IPs keep billing. Only termination ends the instance. Terminated instances remain visible in listings for about an hour and cost nothing.
Order of cleanup
Work from the instance outward. The diagram shows a safe order.
Step 1: Terminate the instance and wait
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0
aws ec2 wait instance-terminated --instance-ids i-0123456789abcdef0The wait command returns when the state is terminated. Do this before deleting the security group, because a group still used by an instance cannot be deleted.
Step 2: Release the Elastic IP
aws ec2 release-address --allocation-id eipalloc-0123456789abcdef0An Elastic IP that is allocated but not attached still incurs a public IPv4 charge. Releasing it returns the address to AWS, and you cannot get that same address back.
Step 3: Find volumes and snapshots that remain
Root volumes usually disappear with the instance. Extra data volumes you attached do not, unless you set delete-on-termination. Volumes in the available state are not attached to anything. List them:
aws ec2 describe-volumes \
--filters Name=status,Values=available \
--query 'Volumes[].[VolumeId,Size,CreateTime]' \
--output table
aws ec2 delete-volume --volume-id vol-0123456789abcdef0Snapshots are not removed with the volume. List your own and delete the ones you no longer need:
aws ec2 describe-snapshots --owner-ids self \
--query 'Snapshots[].[SnapshotId,VolumeSize,Description]' \
--output table
aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0Step 4: Remove the security group and key pair
aws ec2 delete-security-group --group-id sg-0123456789abcdef0
aws ec2 delete-key-pair --key-name my-keyDeleting a key pair removes only the public key stored in AWS. Delete your local .pem file yourself.
Common errors
DependencyViolationwhen deleting a security group: an instance or network interface still uses it. Wait for termination to finish, or check that no other group references it in a rule.InvalidVolume.InUse: the volume is still attached. Terminate or detach first.- Charges continue after cleanup: check that you worked in the correct Region. Resources are per Region, so pass
--regionor confirm your CLI default.
Exercise
You stopped an instance with one 20 GiB data volume and an Elastic IP attached, then closed your laptop. Which charges continue, and what is the complete way to end all of them?
Show answer
The EBS volume and the Elastic IP continue to bill, and any snapshots do too. Compute does not bill while the instance is stopped. To end all charges, snapshot anything you need, terminate the instance, delete the data volume if it remains, release the Elastic IP, and delete any snapshots you no longer want.