Skip to content

Instantly share code, notes, and snippets.

@dapi
Created May 13, 2024 08:13
Show Gist options
  • Save dapi/27b6ca097e02038a288a00c8edafa0df to your computer and use it in GitHub Desktop.
Save dapi/27b6ca097e02038a288a00c8edafa0df to your computer and use it in GitHub Desktop.
Copy ssh public key to all AWS instances
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