Skip to content

Instantly share code, notes, and snippets.

@giantryansaul
Last active July 20, 2016 17:57
Show Gist options
  • Save giantryansaul/6f92ebfa0736ee31aeaf to your computer and use it in GitHub Desktop.
Save giantryansaul/6f92ebfa0736ee31aeaf to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Change xxxxx sections to your personal AWS settings or set them to environment variables
export AWS_ACCESS_KEY_ID=xxxxxxx
export AWS_SECRET_ACCESS_KEY=xxxxxxx
export AWS_DEFAULT_REGION=xxxxxxx
# Get the subnet ID for the lowest availability zone in the VPC section of AWS console
export AWS_SUBNET_ID=xxxxxxx
export EC2_SECURITY_GROUP_ID=xxxxxxx
# Get the current lowest price for the GPU machine we want (we'll be bidding a cent above)
echo -n "Getting lowest g2.2xlarge bid... "
PRICE=$( aws ec2 describe-spot-price-history --instance-types g2.2xlarge --product-descriptions "Windows" --start-time `date +%s` | jq --raw-output '.SpotPriceHistory[].SpotPrice' | sort | head -1 )
echo $PRICE
echo -n "Looking for the ec2-gaming AMI... "
AMI_SEARCH=$( aws ec2 describe-images --owner self --filters Name=name,Values=ec2-gaming )
if [ $( echo "$AMI_SEARCH" | jq '.Images | length' ) -eq "0" ]; then
echo "not found. You must use gaming-down.sh after your machine is in a good state."
exit 1
fi
AMI_ID=$( echo $AMI_SEARCH | jq --raw-output '.Images[0].ImageId' )
echo $AMI_ID
echo -n "Creating spot instance request... "
SPOT_INSTANCE_ID=$( aws ec2 request-spot-instances --spot-price $( bc <<< "$PRICE + 0.01" ) --launch-specification "
{
\"SecurityGroupIds\": [\"$EC2_SECURITY_GROUP_ID\"],
\"ImageId\": \"$AMI_ID\",
\"InstanceType\": \"g2.2xlarge\",
\"SubnetId\": \"$AWS_SUBNET_ID\"
}" | jq --raw-output '.SpotInstanceRequests[0].SpotInstanceRequestId' )
echo $SPOT_INSTANCE_ID
echo -n "Waiting for instance to be launched... "
aws ec2 wait spot-instance-request-fulfilled --spot-instance-request-ids "$SPOT_INSTANCE_ID"
INSTANCE_ID=$( aws ec2 describe-spot-instance-requests --spot-instance-request-ids "$SPOT_INSTANCE_ID" | jq --raw-output '.SpotInstanceRequests[0].InstanceId' )
echo "$INSTANCE_ID"
echo "Removing the spot instance request..."
aws ec2 cancel-spot-instance-requests --spot-instance-request-ids "$SPOT_INSTANCE_ID" > /dev/null
echo -n "Getting ip address... "
IP=$( aws ec2 describe-instances --instance-ids "$INSTANCE_ID" | jq --raw-output '.Reservations[0].Instances[0].PublicIpAddress' )
echo "$IP"
echo "Waiting for server to become available..."
while ! ping -c1 $IP &>/dev/null; do sleep 5; done
echo "All done!"
@runningman84
Copy link

I am also improving this script... how do does the setup work for you? I made some tests and I never go a machine running for more than 15 minutes because of pricing changes... Furthermore the steam library folder does not get created at startup...

@giantryansaul
Copy link
Author

When I create directly through the console with the provided AMI I can play for awhile, but pricing already seems much higher than when I tried this initially. Just started tinkering with this again, so maybe I can update later.

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