Amazon Machine Images (AMIs) are a copy of the boot volume for an Amazon EC2 instance, and optionally additional volumes. They can be created from an existing instance via the Create Image command.
AMIs are stored as Elastic Block Store (EBS) Snapshots, with some additional AMI metadata. EBS Snapshots are incremental backups, which means that only the blocks on the device that have changed after your most recent snapshot are saved. Thus, AMIs are also incremental in nature.
Creation of an AMI via the AWS Command-Line Interface (CLI) is quite simple:
aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My server" --description "An AMI for my server"
This could be run as a Scheduled Task (Windows) or a cron job (Linux).
Keeping the last n
AMIs, or AMIs created within the past n
days, is just a matter of writing a script to retrieve AMIs with the same description, sorting by date and deleting older ones. For example: Script to create daily AMI of AWS server and delete AMI older than n no of days (but it would a simpler script in something like Python).
Alternatively, you could just take snapshots. The difference is that a snapshot is a backup of the Volume, while an AMI can be used to directly launch a new instance.
Here is a script that takes a snapshot of all volumes in a region (you could modify it to only snapshot volumes for a given instance) and keeps the last n
snapshots:
#!/usr/bin/env python
import boto.ec2
MAX_SNAPSHOTS = 2 # Number of snapshots to keep
# Connect to EC2 in this region
connection = boto.ec2.connect_to_region('YOUR-REGION')
# Get a list of all volumes
volumes = connection.get_all_volumes()
# Create a snapshot of each volume
for v in volumes:
connection.create_snapshot(v.id)
# Too many snapshots?
snapshots = v.snapshots()
if len(snapshots) > MAX_SNAPSHOTS:
# Delete oldest snapshots, but keep MAX_SNAPSHOTS available
snap_sorted = sorted([(s.id, s.start_time) for s in snapshots], key=lambda k: k[1])
for s in snap_sorted[:-MAX_SNAPSHOTS]:
connection.delete_snapshot(s[0])
Finally, it's worth mentioning that Amazon CloudWatch Events can be scheduled to automatically create EBS Snapshots (but it won't delete old ones). See: Tutorial: Schedule EBS Snapshots Using CloudWatch Events