Created
October 23, 2019 14:15
-
-
Save bateller/a14a7cd4b86c7cd45522cd6f31e08c6c to your computer and use it in GitHub Desktop.
Create AMIs + Launch Configurations for non aws-cli experts for use in AutoScaling Groups
This file contains hidden or 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
#!/bin/sh | |
# | |
# Copyright: 2017-2019 - B. Teller | |
# Created by: Brian Teller | |
# Description: Help non-shell or AWS experts create easy + quick AMIs for use within AutoScaling Groups | |
# | |
upSeconds="$(tail /proc/uptime | grep -o '^[0-9]\+')" | |
upMins=$((upSeconds / 60)) | |
if [ "${upMins}" -lt "2" ] | |
then | |
upMins2=$(awk '{print int(($1%3600)/60)" min(s) "int($1%60)" sec(s)"}' /proc/uptime) | |
echo "Please wait until system is up for at least 2 minutes, only up for ${upMins2} so far" | |
exit | |
fi | |
# Always start with '001' | |
# Edit this via daily crontab: 01 0 * * * reset_ami_number.sh | |
increment_number="001" | |
# Verify Launch Configurations exist for latest AMI first, if they dont... exit | |
aws_name=$(tail /root/aws/current_name.txt | tr -d '"') | |
lc_checker=$(if aws autoscaling describe-launch-configurations --launch-configuration-names "$aws_name"-micro | grep -q "\"LaunchConfigurations\": \[\]"; then echo "0"; fi) | |
if [ "$lc_checker" = "0" ]; then | |
echo "Cannot Continue - Launch Configuration (Micro) for previous AMI not found! Are you sure you didn't run 1.sh or create_ami.sh previously and forgot to run 2.sh or create_launch_configs.sh after?" | |
exit | |
else | |
echo "Launch Configuration (Micro), found for previous AMI (this is a good thing), Continuing..." | |
fi | |
lc_checker2=$(if aws autoscaling describe-launch-configurations --launch-configuration-names "$aws_name"-small | grep -q "\"LaunchConfigurations\": \[\]"; then echo "0"; fi) | |
if [ "$lc_checker2" = "0" ]; then | |
echo "Cannot Continue - Launch Configuration (Small) for previous AMI not found! Are you sure you didn't run 1.sh or create_ami.sh previously and forgot to run 2.sh or create_launch_configs.sh after?" | |
exit | |
else | |
echo "Launch Configuration (Small), found for previous AMI (this is a good thing), Continuing..." | |
fi | |
# Create new AMI | |
todays_date=$(date +%m-%d-%Y) | |
prefix_name="ACME-Main" | |
aws_name2=$prefix_name-$todays_date-$increment_number | |
dev_id=$(tail /root/aws/this_instance_id.txt | tr -d '"') | |
echo "$aws_name2" > /root/aws/current_name.txt | |
aws ec2 create-image \ | |
--no-dry-run \ | |
--instance-id "$dev_id" \ | |
--name "$aws_name2" \ | |
--description "$aws_name2" \ | |
--reboot \ | |
--block-device-mappings "[{\"DeviceName\": \"/dev/sda1\",\"Ebs\":{\"DeleteOnTermination\":true, \"VolumeType\": \"gp2\"}}]" \ | |
> /root/aws/current_ami.txt 2>&1 | |
echo "Done. Server Will Now Reboot. When it's back online, log back in and run create_launch_configs.sh (DO NOT FORGET THIS STEP!)" |
This file contains hidden or 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
# Can start blank |
This file contains hidden or 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
#!/bin/sh | |
sed -i '/increment_number="/c\increment_number="001"' /root/aws/create_ami.sh |
This file contains hidden or 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
# Should be a 1 line file containing ONLY your EC2 Instance's ID (eg. 'i-0ed5854a0be1c57ae' without quotes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not complete yet.
TODO: