Skip to content

Instantly share code, notes, and snippets.

@glittershark
Created June 7, 2017 22:25
Show Gist options
  • Save glittershark/4cc347094e61394f638d9b1175234c47 to your computer and use it in GitHub Desktop.
Save glittershark/4cc347094e61394f638d9b1175234c47 to your computer and use it in GitHub Desktop.
SSH to an AWS server by name
function instance_by_name() {
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$1" \
| jq -r '.Reservations | map(.Instances) | flatten | map(.NetworkInterfaces) | flatten | map(.Association.PublicIp) | first'
}
SSH_KEY="/path/to/ssh/key"
alias staging='ssh -i $SSH_KEY ec2-user@$(instance_by_name staging)'
alias production='ssh -i $SSH_KEY ec2-user@$(instance_by_name production)'
@daemianmack
Copy link

May want to add a comment distinguishing, e.g., "the production server" from "a production server", which hopefully won't matter much, but could cause some confusion if someone were expecting to find specific log output, etc.

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