Tuesday, October 25, 2022

[SOLVED] aws ssm get-parameter rsa key output to file

Issue

I have stored my private key file in AWS SSM Parameter store. I want to retrieve just the private key value from the parameter store and save it as an id_rsa file locally using aws cli.

This article: https://github.com/aws/aws-cli/issues/2742 shows me exactly how i can do that using sed. however I still get a character returned after "-----END RSA PRIVATE KEY-----" which i want to remove using sed.

This is my command i run on command line:

aws --region=us-east-1 ssm get-parameters --names "mykey" --with-decryption --output text 2>&1 | sed 's/.*----BEGIN/----BEGIN/'

And the output is:

----BEGIN RSA PRIVATE KEY-----
some text here
-----END RSA PRIVATE KEY-----   2

Notice the 2 in the end of the last line. I want to get rid of anything after -----END RSA PRIVATE KEY----- as well.

What do i need to add to my sed command to achieve that?


Solution

This line fixed my problem:

aws --region=us-east-1 ssm get-parameters --names "mykey" --with-decryption --output text 2>&1 | sed 's/.*----BEGIN/----BEGIN/' | sed 's/KEY-----.*/KEY-----/' > id_rsa


Answered By - H RH
Answer Checked By - Gilberto Lyons (WPSolving Admin)