#!/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!)"