1

I have done some research on this and feel as if I'm about 80% there but struggling to adjust the jq output as required due to splitting one of the strings.

I'm trying to convert the JSON output from AWS SSM to environment variables.

AWS command

aws ssm get-parameters-by-path \
--path /qa/es \
--with-decryption \
--query 'Parameters[*].{Name:Name,Value:Value}' \

Output

[
    {
        "Name": "/qa/es/AWS_ACCESS_KEY_ID",
        "Value": "ABC123"
    },
    {
        "Name": "/qa/es/AWS_SECRET_ACCESS_KEY",
        "Value": "abcdefghijkl"
    },
    {
        "Name": "/qa/es/ENDPOINT",
        "Value": "https://amazonaws.com"
    }
]

My required output from jq, note I'm only after the environment variable AFTER the last /. There may be cases where this could be /qa/es/something/nested/ENV_VAR

AWS_ACCESS_KEY_ID=ABC123
AWS_SECRET_ACCESS_KEY=abcdefghijkl
ENDPOINT=https://amazonaws.com

Once I have this I can utilise the answer here to set the environment variables. Exporting JSON to environment variables

The closest I have got is

jq -r "map(\"\(try(.Name |= split(\"/\")))=\(.Value|tostring)\")|.[]" params.json

Which gives me

{"Name":["","qa","es","AWS_ACCESS_KEY_ID"],"Value":"ABC123"}=ABC123
{"Name":["","qa","es","AWS_SECRET_ACCESS_KEY"],"Value":"abcdefghijkl"}=abcdefghijkl
{"Name":["","qa","es","ENDPOINT"],"Value":"https://amazonaws.com"}=https://amazonaws.com

Close, but not close enough! Can anyone point me in the right direction here?

peak
  • 105,803
  • 17
  • 152
  • 177
Nick
  • 1,219
  • 1
  • 13
  • 24

2 Answers2

5

With the -r command-line option,

.[]
| "\(.Name|split("/")|.[-1])=\(.Value)"

yields:

AWS_ACCESS_KEY_ID=ABC123
AWS_SECRET_ACCESS_KEY=abcdefghijkl
ENDPOINT=https://amazonaws.com

This seems to correspond to what you've asked for, but this approach has the potential disadvantage that it assumes something about "=", so please be careful!

peak
  • 105,803
  • 17
  • 152
  • 177
1

As the previous comment but use '@sh' to escape the value

| "\(.Name|split("/")|.[-1])=\(.Value | @sh)"
Anonymous
  • 11
  • 1