I already provided one answer which uses EBS volumes to get at the ssh public key, but here's another way you can get at it by starting a temporary EC2 instance with a user-data script that sends the public key to the console output. Here are the steps:
Save the following code to a file named output-ssh-key.userdata
on your local computer. DO NOT RUN THESE COMMANDS LOCALLY!
#!/bin/bash -ex
exec> >(tee /var/log/user-data.log|logger -t user -s 2>/dev/console) 2>&1
adminkey=$(GET instance-data/latest/meta-data/public-keys/ |
perl -ne 'print $1 if /^0=[^a-z0-9]*([-.@\w]*)/i')
cat <<EOF
SSHKEY:========================================================================
SSHKEY:HERE IS YOUR PUBLIC SSH KEY FOR KEYPAIR "$adminkey":
SSHKEY:$(cat /home/ubuntu/.ssh/authorized_keys)
SSHKEY:========================================================================
SSHKEY:Halting in 50min ($(date --date='+50 minutes' +"%Y-%m-%d %H:%M UTC"))
EOF
sleep 3000
halt
Run a stock Ubuntu 10.04 LTS instance with the above file as a user-data script. Specify the keypair for which you want to retrieve the public ssh key:
ec2-run-instances \
--key YOURKEYPAIRHERE \
--instance-type t1.micro \
--instance-initiated-shutdown-behavior terminate \
--user-data-file output-ssh-key.userdata \
ami-ab36fbc2
Keep requesting the console output from the instance until it shows your public ssh key. Specify the instance id returned from the run-instances command:
ec2-get-console-output YOURINSTANCEID | grep SSHKEY: | cut -f3- -d:
Within 2-10 minutes you will get output like this:
========================================================================
HERE IS YOUR PUBLIC SSH KEY FOR KEYPAIR "erich":
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6rn8cl41CkzaH4ZBhczOJZaR4xBBDI1Kelc2ivzVvCB
THcdJRWpDd5I5hY5W9qke9Tm4fH3KaUVndlcP0ORGvS3PAL4lTpkS4D4goMEFrwMO8BG0NoE8sf2U/7g
aUkdcrDC7jzKYdwleRCI3uibNXiSdeG6RotClAAp7pMflDVp5WjjECDZ+8Jzs2wasdTwQYPhiWSiNcfb
fS97QdtROf0AcoPWElZAgmabaDFBlvvzcqxQRjNp/zbpkFHZBSKp+Sm4+WsRuLu6TDe9lb2Ps0xvBp1F
THlJRUVKP2yeZbVioKnOsXcjLfoJ9TEL7EMnPYinBMIE3kAYw3FzZZFeX3Q== erich
========================================================================
Halting in 50min (2011-12-20 05:58 UTC)
The temporary instance will automatically terminate itself in under an hour, but you can terminate it yourself if you'd like to make sure that you aren't charged more than the two cents this will cost to run.