HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to find out the used Key pair name via AWS CLI?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
thehowusednameviaawsfindclioutpair

Problem

I'm aware of get-password-data command which can display PasswordData data like:

$ aws ec2 get-password-data --instance-id i-0123456789
{
    "InstanceId": "i-0123456789",
    "PasswordData": "\r\nAOh...xg==\r\n",
    "Timestamp": "2018-03-27T16:52:04.000Z"
}


however I'd like to know which Key pair name was used to launch the instance, so I can pass it using --priv-launch-key in order to decrypt the password.

Solution

For a given instance, you would first use aws ec2 describe-instances to get the information JSON for your instance.

The information also contains the keypair name used to create that instance.

E.g. for an instance i-0e2x8xd7xxx (Note: I use the awesome tool jq to do JSON parsing but you can use any other solution)

aws ec2 describe-instances --instance-ids i-0e2x8xd7xxx | jq '.Reservations[].Instances[].KeyName'


Output:

"my_key_name"


You can store that in a variable, say $keypair_name and then pass it into your aws ec2 get-password-data command.

You would also need to pass in the path on your machine where your keypairs are located e.g. $keypair_path.

For example:

aws ec2 get-password-data --priv-launch-key $keypair_path/$keypair_name .....

Code Snippets

aws ec2 describe-instances --instance-ids i-0e2x8xd7xxx | jq '.Reservations[].Instances[].KeyName'
"my_key_name"
aws ec2 get-password-data --priv-launch-key $keypair_path/$keypair_name .....

Context

StackExchange DevOps Q#3825, answer score: 7

Revisions (0)

No revisions yet.