11
votes

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?

3
Why do you need 2>&1 ? Are you redirecting stderr to stdout? Why? Remove 2>&1 and it should fix your problem. - helloV
I did remove 2>&1 as you are right, it is not needed. However that still does not remove the trailing 2 in the end of the last line. So by adding the following did give me the desired output: sed 's/KEY-----.*/KEY-----/' - H RH

3 Answers

37
votes

You can obtain the value alone using the following command:

aws --region=us-east-1 ssm get-parameter --name "mykey" --with-decryption --output text --query Parameter.Value

i.e. by selecting the value using --query Parameter.Value

You can then pipe it directly to the file without using sed.

0
votes

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
0
votes

Additional option:

aws --region=us-east-1 ssm get-parameters --names "mykey" --with-decryption | jq -r '.Parameter.Value'