Skip to content

Instantly share code, notes, and snippets.

@chales
Last active July 25, 2021 05:38
Show Gist options
  • Save chales/c68702a725ba0d5d9c8c to your computer and use it in GitHub Desktop.
Save chales/c68702a725ba0d5d9c8c to your computer and use it in GitHub Desktop.
Helper bash functions to retrieve info and login to AWS EC2 instances.

You must install the AWS CLI to use these functions. You also have to have access keys to AWS and permission to access instance information.

The AWS CLI install is usually painless, see http://docs.aws.amazon.com/cli/latest/userguide/installing.html

sudo easy_install pip

sudo pip install awscli

After install run the setup to set your configuration options which includes the default region and your access keys.

aws configure

Possible issues with Mac not having the proper python path noted here, http://stackoverflow.com/questions/29858184/aws-cli-not-working-on-mac-osx-yosemite

You can the aws_login_helpers.sh functions to your bash_profile/bash_rc or use them in a script.

You will likely only need one of the helper functions to use the assh function but I've added a few as examples. I use the ec2_priv_ip_from_tag helper function in the assh function below since I'm logging in through a bastion instance and need the internal IP for my destination. Simply change "ubuntu@$(ec2_priv_ip_from_tag "$1")" to the helper function you need, e.g. ec2_pub_ip_from_tag to get the public IP.

The 'assh' function assumes you are using a bastion/jump server to connect through and that those settings are set in your ssh config file. Remove the proxy command option if this is not the case or specify the user@host in the place of the "bastion" alias in that command.

The host entry is written to /dev/null on each login so it's not stored and thus you will not fill up your knownhosts file with these ephemeral entries.

#### AWS cli helper functions that can be used for SSH'ing into EC2 instances
# Return public DNS name from instance name tag
function ec2_hostname_from_tag() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"')
}
# Return InstanceId from instance name tag
function ec2_id_from_tag() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].InstanceId' | tr -d '"')
}
# Return private IP address from instance name tag
function ec2_pub_ip_from_tag() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicIp' | tr -d '"')
}
# Return private IP address from instance name tag
function ec2_priv_ip_from_tag() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PrivateIpAddress' | tr -d '"')
}
# ssh into an amazon instance using it's name tag.
# Specify your ssh key name.
# I am using options from my ssh config for the "bastion" hop. Specify the user/IP if needed.
function assh() {
ssh -A -i ~/.ssh/<your key here> \
-o ForwardAgent=yes \
-o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null \
-o "ProxyCommand ssh -W %h:%p bastion" \
ubuntu@$(ec2_priv_ip_from_tag "$1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment