Skip to content

Instantly share code, notes, and snippets.

@navoshta navoshta/
Last active May 2, 2017

What would you like to do?
Starts your AWS instance. Check out my post about usage of this script:
# Starts your AWS instance, connects via SSH and launches Chrome with the remote Jupyter Notebook page open.
# Usage is as follows:
# 1. Run this script, so that Chrome has launched and SSH connection is established.
# 2. Execute 'jupyter notebook' on the AWS instance.
# 3. Reload the page in Chrome and log in to Jupyter Notebook.
# Note: we use Chrome, as there's a known issue with Safari that won't let Jupyter Notebook connect to a remote kernel.
# Script configuration:
# ID of your AWS instance.
AWS_INSTANCE_ID="<Your instance ID here>"
# Port that you used in your Jupyter Notebook configuration.
# Port that you configured in the security group of your AWS instance.
# Browser path to run Jupyter Notebook.
echo "Starting..."
# Get the instance state.
AWS_STATE=$(aws ec2 describe-instances --instance-ids $AWS_INSTANCE_ID --query "Reservations[*].Instances[*].State.Name" --output text)
if [ "$AWS_STATE" != "stopped" ] && [ "$AWS_STATE" != "running" ]; then
echo "...Instance is not available, try again later."
if [ "$AWS_STATE" == "running" ]; then
echo -n "...AWS instance is already running. Initialising..."
elif [ "$AWS_STATE" == "stopped" ]; then
# If the state is 'stopped', start it.
aws ec2 start-instances --instance-ids $AWS_INSTANCE_ID >/dev/null
echo -n "...AWS instance started. Initialising..."
# Wait till the instance has started.
while AWS_STATE=$(aws ec2 describe-instances --instance-ids $AWS_INSTANCE_ID --query "Reservations[*].Instances[*].State.Name" --output text); test "$AWS_STATE" != "running"; do
sleep 1; echo -n '.'
echo " Ready."
# Get the instance public IP address.
AWS_IP=$(aws ec2 describe-instances --instance-ids $AWS_INSTANCE_ID --query "Reservations[*].Instances[*].PublicIpAddress" --output text)
echo "AWS instance IP: $AWS_IP."
# Launch Chrome with the Jupyter Notebook URL. The URL will fail, since we haven't started it yet.
/usr/bin/open -a "$BROWSER_PATH" $NOTEBOOK_URL
# When the AWS instance starts there is still a bit of a delay till its network interface is initialised, we will wait till it is available.
echo -n "Waiting for AWS instance network interface..."
nc -z -w 5 $AWS_IP $AWS_SSH_PORT 1>/dev/null 2>&1
while [ $? != 0 ]; do
sleep 1; echo -n '.'
nc -z -w 5 $AWS_IP $AWS_SSH_PORT 1>/dev/null 2>&1
echo " Ready."
# Add instance IP to known hosts to avoid a security warning dialog.
ssh-keyscan -H $AWS_IP >> ~/.ssh/known_hosts
# Connect to the AWS instance.
ssh -i ~/.aws/my_aws_key.pem ubuntu@$AWS_IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.