Skip to content

Instantly share code, notes, and snippets.

@pjbull
Created October 27, 2015 16:08
Show Gist options
  • Save pjbull/a45e5ce43ffe4d096124 to your computer and use it in GitHub Desktop.
Save pjbull/a45e5ce43ffe4d096124 to your computer and use it in GitHub Desktop.
SSH to Running EC2 Ubuntu Instance
# I keep this in my .bashrc to get to a running EC2 instance if there is only one running with my current creds
export AWS_EC2_PEM=<PATH_TO_PEM>
function ec2me () {
running=$(ec2-describe-instances | grep running)
instances=$(wc -l <<< "$running")
if [ $instances = 1 ]; then
server=$(cut -f4 <<< "$running" )
echo "Connecting to $server"
ssh -i $AWS_EC2_PEM ubuntu@$server
else
echo "ERROR: CAN'T CHOSE SERVER: There are $instances instances running:"
echo "$running"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment