Last active
April 8, 2025 01:59
-
-
Save neilkuan/fd3f4dbbc0ed1596f3db84df7fb2bce9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
AWS_PROFILE=<yourprofile-name> | |
instanceId=$1 | |
pubkey="$HOME/.ssh/id_rsa.pub" | |
sshUser='ec2-user' | |
az=$(aws ec2 describe-instances --instance-id ${instanceId} --region ap-northeast-1 --query 'Reservations[0].Instances[0].Placement.AvailabilityZone' --output text) | |
privateIp=$(aws ec2 describe-instances --instance-id ${instanceId} --region ap-northeast-1 --query 'Reservations[0].Instances[0].PrivateIpAddress' --output text) | |
echo "sending public key to ${instanceId}" | |
aws ec2-instance-connect send-ssh-public-key --instance-id ${instanceId} --instance-os-user ${sshUser} \ | |
--ssh-public-key file://${pubkey} --availability-zone $az > /dev/null | |
if [[ $2 != '--send-key-only' ]]; then | |
echo "connecting to ${publicIp}" | |
ssh -i id_rsa ${sshUser}@$privateIp | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment