-
-
Save meabed/40c34d06a35005ffa141 to your computer and use it in GitHub Desktop.
Create EC2 AMI from Bash Script; Good for cron jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment