Sub topic 16
Controlling EBS costs
This chapter assumes you can create and modify volumes and snapshots with the AWS CLI. It also assumes you know the volume types and the gp3 IOPS and throughput model from earlier chapters. Prices below are approximate us-east-1 list prices at the time of writing. Check the current EBS pricing page before you make decisions from them.
What you are billed for
EBS charges accrue whether or not the data is in use. Five line items account for most bills:
| Cost driver | How it is billed | Typical trap |
|---|---|---|
| Provisioned storage | Per GB-month of volume size, not data written | Oversized volumes that are 10% full |
| Provisioned performance | gp3 IOPS above 3,000 and throughput above 125 MiB/s; io1 and io2 IOPS | Performance set for a peak that never comes |
| Snapshot storage | Per GB-month of stored snapshot data, after deduplication | Old snapshots that nothing ever deletes |
| Fast snapshot restore | Per hour for each snapshot in each Availability Zone | Left enabled after a launch event |
| Unattached volumes | Same as attached volumes | Volumes left behind after instance termination |
A volume is billed from creation until deletion. A stopped instance still pays for its volumes. Data transfer between an instance and its volumes is free, so cost control comes down to size, performance settings and snapshot retention.
Find unattached volumes
A volume in the available state is not attached to any instance. These are the easiest savings, because nothing depends on them, in most cases.
aws ec2 describe-volumes \
--filters Name=status,Values=available \
--query 'Volumes[].[VolumeId,Size,VolumeType,CreateTime]' \
--output tableDo not delete on sight. First check the volume's tags and creation date. Then take a final snapshot, which costs far less than the volume, and delete the volume.
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'final copy before delete' \
--tag-specifications 'ResourceType=snapshot,Tags=[{Key=Name,Value=final-copy}]'
aws ec2 delete-volume --volume-id vol-0123456789abcdef0Wait until the snapshot reaches the completed state before you delete the volume. For the same reason, check DeleteOnTermination on non-root volumes at launch. Orphans usually appear when a data volume is attached with DeleteOnTermination set to false and the instance is later terminated.
Move gp2 volumes to gp3
For the same size, gp3 storage costs about 20% less than gp2 in us-east-1 (about $0.08 versus $0.10 per GB-month). gp3 also includes 3,000 IOPS and 125 MiB/s regardless of size. A gp2 volume gets 3 IOPS per GB, so small gp2 volumes only reach 3,000 IOPS through burst credits.
List your gp2 volumes, then change the type with Elastic Volumes. The volume stays attached and in use.
aws ec2 describe-volumes \
--filters Name=volume-type,Values=gp2 \
--query 'Volumes[].[VolumeId,Size,Iops]' \
--output table
aws ec2 modify-volume \
--volume-id vol-0123456789abcdef0 \
--volume-type gp3Check the IOPS column before converting. A gp2 volume larger than 1,000 GiB has a baseline above 3,000 IOPS (3 IOPS per GB). It can reach 16,000 at 5,334 GiB. When you convert, set --iops and --throughput to preserve that performance, for example --iops 6000 for a 2,000 GiB volume. Extra gp3 IOPS and throughput are billed separately, but they are cheap compared with the difference in storage price, and at most sizes gp3 still comes out ahead.
Right-size volumes and performance
Cost scales with provisioned size, so a volume at 15% usage wastes most of what you pay for it. EBS volumes cannot be shrunk. To reduce the size, create a smaller volume, copy the data across, and then switch the instance over to it:
- Create the new, smaller volume and attach it to the instance.
- Format it and mount it at a temporary path.
- Copy the data with
rsync -aHAX /data/ /mnt/new/. - Stop the application, run a final
rsync, and swap the mount points. - Detach and delete the old volume after you verify the copy.
For root volumes, the same procedure needs more care because of the boot partition. Often it is simpler to build a new AMI with a smaller root volume.
Performance settings need the same review. Compare the CloudWatch metrics VolumeReadOps and VolumeWriteOps (divide by the period in seconds to get IOPS) with what you provisioned. If the peak is 2,500 IOPS on a volume set to 10,000, lower it. For io1 and io2 volumes, ask whether the workload truly needs sub-millisecond latency and more than 16,000 IOPS. If not, gp3 at up to 16,000 IOPS is usually much cheaper. Provisioned IOPS volumes carry a per-IOPS charge on top of storage.
Throughput-oriented workloads with large sequential I/O can use st1 or sc1 at lower per-GB prices. Neither can be a boot volume, and their minimum size is 125 GiB. They suit logs, data warehouses and cold archives, not small random reads.
Control snapshot costs
Snapshots are incremental, so the first snapshot stores all written blocks and each later one stores only changed blocks. That does not make them free. Deleting an old snapshot does not remove data that a newer snapshot still references, so the savings from deleting one snapshot are often smaller than its apparent size. Deleting the whole chain is what brings the bill down.
Set a retention policy rather than relying on people to clean up. Amazon Data Lifecycle Manager can take snapshots on a schedule and delete them after a count or age limit. Common mistakes are to schedule snapshots with no retention rule, or to set the rule to keep far more than you would ever restore.
To find old snapshots you own, sort by start time:
aws ec2 describe-snapshots \
--owner-ids self \
--query 'sort_by(Snapshots,&StartTime)[:20].[SnapshotId,StartTime,VolumeSize,Description]' \
--output tableDelete a snapshot with aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef0. If the snapshot backs a registered AMI, the call fails. Deregister the AMI first with aws ec2 deregister-image, then delete its snapshots. Deregistering does not delete snapshots automatically.
For snapshots you must keep for months or years but rarely restore, use the archive tier. It costs about a quarter of the standard tier (about $0.0125 versus $0.05 per GB-month). It has a 90-day minimum storage period and a restore fee, and a restore takes hours.
aws ec2 modify-snapshot-tier \
--snapshot-id snap-0123456789abcdef0 \
--storage-tier archiveArchiving stores a full copy of the snapshot, so it saves money only when the snapshot is large or holds data that would otherwise stay in standard storage for a long time. A snapshot with a small incremental size can cost more in the archive tier than in the standard one. Check the incremental size before you archive.
Also review fast snapshot restore. It removes first-read latency on volumes restored from a snapshot, but it bills per hour for each snapshot in each Availability Zone it is enabled in. Enable it only for the window you need and disable it afterward.
Tag volumes and watch spend
You cannot optimize what you cannot attribute. Tag volumes and snapshots at creation, with keys such as Project, Owner and Environment. Activate those keys as cost allocation tags in the Billing console, and then group by tag in Cost Explorer. Tags also make cleanup safe, because you can ask the owner whether a volume is still needed.
Useful checks to schedule monthly:
- Volumes in the
availablestate for more than 14 days. - gp2 volumes that could be gp3.
- io1 or io2 volumes whose peak IOPS is far below what is provisioned.
- Snapshots with no matching volume or AMI.
- Fast snapshot restore enabled on old snapshots.
AWS Compute Optimizer produces volume recommendations from CloudWatch metrics, including a suggested type and IOPS setting. Treat them as a starting point and confirm against your own peak load.
Common errors
- Deleting a volume before its snapshot completes. The snapshot can fail or be unusable. Wait for
completed. - Converting a large gp2 volume to gp3 without setting IOPS. The volume falls back to 3,000 IOPS and a workload that depended on the gp2 baseline slows down.
- Assuming a deleted snapshot frees its full size. Blocks shared with newer snapshots stay.
- Trying to shrink a volume with
modify-volume. The API only accepts a size that is larger than or equal to the current one. - Forgetting regions. Volumes and snapshots are regional. A cleanup in us-east-1 does not touch eu-west-1, so repeat the checks in each Region you use.
Exercise
A 2,000 GiB gp2 volume is attached to a database server and has a baseline of 6,000 IOPS. You want to convert it to gp3 without losing performance. Which command do you run, and why does the IOPS value matter?
Show answer
Run aws ec2 modify-volume --volume-id <id> --volume-type gp3 --iops 6000. A gp3 volume defaults to 3,000 IOPS, so without --iops the volume would lose half of its baseline performance. Also check throughput. A gp2 volume of this size can deliver up to 250 MiB/s, while gp3 defaults to 125 MiB/s, so add --throughput if the workload needs it. The extra IOPS and throughput are billed separately, but gp3 storage is cheaper per GB.
Exercise
You find a volume that has been in the available state for 60 days. List the steps you take before the volume stops costing money, and name one thing to check first.
Show answer
First check the volume's tags and creation date, and ask the owner if it is still needed. Then create a snapshot and wait for it to reach completed, and delete the volume with aws ec2 delete-volume. Billing for the volume stops on deletion. The snapshot continues to cost a small amount, so add a retention date to it.