Created
May 13, 2024 08:13
-
-
Save dapi/27b6ca097e02038a288a00c8edafa0df to your computer and use it in GitHub Desktop.
Copy ssh public key to all AWS instances
This file contains 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
aws ec2 describe-instances --query 'Reservations[*].Instances[*].{Instance:InstanceId,AvailabilityZone:Placement.AvailabilityZone}' --region us-east-1 | \ | |
jq -r '.[] | map({Instance,AvailabilityZone}) | (first | keys_unsorted) as $$keys | map([to_entries[] | .value]) as $$rows | $$keys,$$rows[] | @tsv' | \ | |
grep -v Instance | \ | |
awk '{print "--instance-id", $1, "--availability-zone", $2}' | \ | |
xargs -I@ bash -c "AWS_PAGER= aws ec2-instance-connect send-ssh-public-key --region us-east-1 @ --instance-os-user ec2-user --ssh-public-key file://~/.ssh/id_rsa.pub" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment