Skip to content

Instantly share code, notes, and snippets.

@redjoker011
Last active November 25, 2020 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redjoker011/8c5ace3059b2b5a755506b9d2924cdf6 to your computer and use it in GitHub Desktop.
Save redjoker011/8c5ace3059b2b5a755506b9d2924cdf6 to your computer and use it in GitHub Desktop.
Copy Public Key into AWS EC2 Server via AWS CLI ec2-instance-connect
#!/bin/bash
# Copy ~/.ssh/id_rsa.pub into Host Server
# Note: By default it will use `ubuntu` as os user, feel free to change this
#
# Suppyly instance id and availability zone as script argument
# e.g ./copy-pub-key.sh instance-1234 ap-southeast-1
current_dir=pwd
instance_id=$1
az=$2
echo "Copying Pub Key into Instance: $instance_id with AZ: $az"
cd ~/.ssh
aws ec2-instance-connect send-ssh-public-key \
--instance-id $instance_id \
--instance-os-user ubuntu \
--availability-zone $az \
--ssh-public-key file://id_rsa.pub \
--profile cg-manager-ec2
cd $CURRENT_DIR
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment