Skip to content

Instantly share code, notes, and snippets.

@o2346
Last active December 26, 2020 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save o2346/dfe0d49e267cff36a9fc13ee7dc928bb to your computer and use it in GitHub Desktop.
Save o2346/dfe0d49e267cff36a9fc13ee7dc928bb to your computer and use it in GitHub Desktop.
EC2 Fleet Demo
#!/bin/bash
# turbo-boost-ec2-fleet-with-spot
# script for executing https://github.com/awslabs/ec2-spot-labs/tree/master/ec2-fleet#turbo-boost-ec2-fleet-with-spot
#Nodejs required to handle json
#Obtain right credentials beforehand
cd $(dirname $(mktemp -u)) #should be /tmp on linux, a counterpart on darwin instead, as working dir
[ -d "ec2-spot-labs" ] || git clone https://github.com/awslabs/ec2-spot-labs/
cd ec2-spot-labs/ec2-fleet
pwd
#For testing next if condition
#aws ec2 delete-launch-template --launch-template-name ec2-fleet
#1. Create launch template
if ! aws ec2 describe-launch-templates | grep '"LaunchTemplateName": "ec2-fleet"'; then
echo "Creating launch template" >&2
#Obtain latest ami id
readonly latest_ami=`aws ssm get-parameters --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 --query 'Parameters[].Value' --output text`
echo "with latest ami $latest_ami" >&2
readonly launch_template_custom_json=`mktemp`
cat ./ec2-fleet-launch-template.json | node -e "const t = []; require( 'readline' ).createInterface( { input: process.stdin } ).on( 'line', ( l ) => { t.push( l ); } ).on( 'close', () => { const json = JSON.parse( t.join( '' ) ); json.LaunchTemplateData.ImageId = '$latest_ami'; process.stdout.write( JSON.stringify( json, null, ' ' ) ); } );" > $launch_template_custom_json
cat $launch_template_custom_json >&2
aws ec2 create-launch-template --cli-input-json file://$launch_template_custom_json
fi
#customize baseline to prevent error such as shown below for next step
#An error occurred (InvalidFleetConfig) when calling the CreateFleet operation: Unable to find Subnet(s): subnet-fa2653a3.
#undefined:1
readonly cli_input_json=`mktemp`
cat ec2-fleet-ec2-spot-turbo-boost.json | node -e "const t = []; require( 'readline' ).createInterface( { input: process.stdin } ).on( 'line', ( l ) => { t.push( l ); } ).on( 'close', () => { const json = JSON.parse( t.join( '' ) ); json.LaunchTemplateConfigs[ 0 ].Overrides = [ { \"InstanceType\": \"t2.micro\" } ]; process.stdout.write( JSON.stringify( json, null, ' ' ) ); } );" > $cli_input_json
#2. Create EC2 Fleet with baseline On-Demand
aws ec2 create-fleet --cli-input-json file://$cli_input_json | tee ./fleet.json
#if following error occurred, retry after a while
#An error occurred (ServiceLinkedRoleCreationInProgress) when calling the CreateFleet operation (reached max retries: 2): The Service-Linked Role for this EC2 Fleet is not yet ready for use.
#undefined:1
# obtain fleetid for following steps
readonly FLEETID=$(cat fleet.json | node -e "const t = []; require( 'readline' ).createInterface( { input: process.stdin } ).on( 'line', ( l ) => { t.push( l ); } ).on( 'close', () => { const json = JSON.parse( t.join( '' ) ); process.stdout.write( json.FleetId ); } );")
echo FLEETID=$FLEETID
#3. Describe Fleet
aws ec2 describe-fleets --fleet-id $FLEETID
# await until it's activated
while true; do
sleep 3
aws ec2 describe-fleets --fleet-id $FLEETID | grep '"FleetState": "active"' && break
done
#4. Describe Fleet history
aws ec2 describe-fleet-history --fleet-id $FLEETID --start-time "`date +'%Y-%m-%d'`"
#5. Turbo boost EC2 Fleet with Spot
aws ec2 modify-fleet --fleet-id $FLEETID --target-capacity-specification TotalTargetCapacity=20
#[OPTIONAL] do whatever here among the fleet if you prefer, like
#./exam_among_fleet.sh
echo "fleet deletion will start shortly.."
sleep 100
#6. Clean up
aws ec2 delete-fleets --fleet-id $FLEETID --terminate-instances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment