Skip to content

Instantly share code, notes, and snippets.

@chales
Created May 28, 2014 16:36
Show Gist options
  • Save chales/38da36144101742690af to your computer and use it in GitHub Desktop.
Save chales/38da36144101742690af to your computer and use it in GitHub Desktop.
Use AWS CLI Tools to allow ssh into EC2 instance via their name tag. See http://blog.ryanparman.com/2014/01/29/easily-ssh-into-amazon-ec2-instances-using-the-name-tag/
# Install AWS CLI tools, https://github.com/aws/aws-cli/
# Add these functions to ~/.bash_profile or ~/.profile
# Modify to fit specific needs such as adding a bastion proxy option.
#
# Usage examples:
# $ hostname_from_instance <instance-name>
# $ ip_from_instance <instance-name>
# $ ssh-aws <instance-name>
#
function hostname_from_instance() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicDnsName' | tr -d '"')
}
function ip_from_instance() {
echo $(aws ec2 describe-instances --filters "{\"Name\":\"tag:Name\", \"Values\":[\"$1\"]}" --query='Reservations[0].Instances[0].PublicIpAddress' | tr -d '"')
}
function ssh-aws() {
ssh -i ~/.ssh/your-keypair.pem ec2-user@$(ip_from_instance "$1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment