Skip to content

Instantly share code, notes, and snippets.

@josh-padnick
Last active June 15, 2022 04:48
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save josh-padnick/8619a785663153564fec to your computer and use it in GitHub Desktop.
Save josh-padnick/8619a785663153564fec to your computer and use it in GitHub Desktop.
Create EC2 AMI from Bash Script; Good for cron jobs
PATH=/bin:/usr/local/bin
# Put this in your crontab file to run the script every day at 01:30 (1:30am). Note the PATH variable above; required for this script.
# m h dom mon dow command
30 01 * * * /bin/bash /home/ubuntu/scripts/ec2-create-image.sh i-8a915682 >> /home/ubuntu/logs/crontab.log 2>&1
#!/usr/bin/env bash
#
# @Purpose Creates an image (AMI) of the given EC2 instance
# @Background Meant to be run as a cronjob. Requires that awscli is installed. Assumes that the
# instance running this command has the permission ec2:CreateImage assigned via IAM.
#
# @Usage: ec2-create-image <instance-id>
#
DATE=$(date +%Y-%m-%d_%H-%M)
AMI_NAME="Wordpress Backup - $DATE"
AMI_DESCRIPTION="Wordpress Backup - $DATE"
INSTANCE_ID=$1
printf "Requesting AMI for instance $1...\n"
aws ec2 create-image --instance-id $1 --name "$AMI_NAME" --description "$AMI_DESCRIPTION" --no-reboot
if [ $? -eq 0 ]; then
printf "AMI request complete!\n"
fi
@euphy92
Copy link

euphy92 commented Mar 29, 2016

so this script is working for me when i run it manually but not as a cronjob. Any idea why? when its gets to line 21 in the script which is the aws command it says command not found.

@euphy92
Copy link

euphy92 commented Mar 29, 2016

i took PATH=/bin:/usr/local/bin out of crontab and the script and cronjob work fine now. Thank You

@akolkarsohan
Copy link

Thanks for the script! I just had to add "--region us-east-1" attribute to the "aws ec2 create-image" command.

@tln8679
Copy link

tln8679 commented Sep 18, 2018

Sorry if this is off-topic, but does anyone know of a script to deregister the AMI's and delete the associated snapshots/volumes after a given period of time (say 30 days)? It appears there is a snapshot life cycle policy on the console, but you cannot delete a snapshot until the ami is deregistered.

The only solution I can think of is, naming each image chronologically 1 though N and deregistering all images that are named greater than N. Any thoughts?

@chrisarrow
Copy link

Sorry if this is off-topic, but does anyone know of a script to deregister the AMI's and delete the associated snapshots/volumes after a given period of time (say 30 days)? It appears there is a snapshot life cycle policy on the console, but you cannot delete a snapshot until the ami is deregistered.

The only solution I can think of is, naming each image chronologically 1 though N and deregistering all images that are named greater than N. Any thoughts?

Hi tln8679, I came across this article that might help you out:

https://n2ws.com/blog/how-to-guides/how-to-delete-unutilized-ebs-based-amis-and-corresponding-snapshots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment