Wednesday, October 27, 2021

[SOLVED] How to generate pem file from create-key-pair command in windows?

Issue

I running this command to create key-pair in aws:

aws ec2 create-key-pair --key-name MyKeyPair

The command is works. but no pem file is generate. In the docs there is no reference for how to do that.

After I run the command I get the pem content file in json structure to the console.

{                                                                                                        
    "KeyMaterial": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAfe6quq7f2ikhT+wRV5pNzk9u3jqr3N
ta1tqaSoC3K5Tv4ZKeeT0WljQaNIQKV\nyg8WF8XtLKQS9PZ0mpPdkQbP7KSprwYusQTa0xldX8u423djFru36YQ71PDA6HdJl0r8h7RQ
sNKaXpStKq7\nHu4EzjHyQUVE9ffzDLwDMjNutAcm4bFhiNYGl9Twn+V0Jfq5wnnWluCG/sqMU6i/bWvb18iJ7dW2\n9Dl4czoSYp+kXB
/dT18bLWYlcOl9tNpBAECgYEA6dIIag6IFoSAP5/PstHk3IzT0YJ9sldRl7i/OJYsoaGs\nmoBtUgjjkvLWaEgV7pmeCGbmDPxmKtlKJf
ksMw+0NNivRnbhOeSBj1OwJCr67Uz0Mwr1AyhnkNlLWmD4dN1\npRQDy93l5/D0/l4A1muZWLVVboWvvg+KU0GVzx9WnIcXAStJGP4S3x
FDnZTl0ay8+dll3uN7Sm3/3qmDAor\nwSwsvUaOJy2dslDIZ+yaRQKBgQDL5/y/ORBlB4bgpxZ81CFZyM+536qr26Cp7YHBZYidpa/OXY
nD3R+Fgqs\nrig796uvJy9bsnBqrW9DDjeFDw6dnFU81MUNbjf4Fx5mPKoFcwSSxd7QaPePKCv+9RAZcWF3QeVa\nMtdNM0BemepdhHJ2
    "KeyName": "MyKeyPair",                                                                              
    "KeyFingerprint": "16:0b:c4:40:1e:e1:89:6a:75:d0:e1:aa:1a:92:38"                      
}                                                                                                        

How to have pem file after running this command in windows?


Solution

Try this:

aws ec2 create-key-pair --key-name <name> --query "KeyMaterial" --output text > key.pem


Answered By - Shlomi Levi