Search code examples
grepaws-secrets-manager

How to get variable to value format out of AWS secret manager?


So we have our secrets stored in AWS Secret Manager, i can retrieve the secret using awscli

aws secretsmanager get-secret-value --secret-id test | grep SecretString

but the output is in this format:

 "SecretString": "{\"REACT_APP_API_URL\":\"https://example.com/api\"}",

How can i get output like in this format

REACT_APP_API_URL=https://example.com/api

I'm really new to this so was thinking maybe its possible using grep? Or if there's any way to retrieve from AWS in the above way?

Thanks


Solution

  • aws secretsmanager get-secret-value --secret-id XXXXX | jq --raw-output '.SecretString' | jq '.' | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]"

    that will do it

    Broken down:

    aws secretsmanager get-secret-value --secret-id XXXXX retrieves specific secret

    jq --raw-output '.SecretString' filters out only secret value

    jq '.' formats is as json again (might not be necessary strictly speaking)

    jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" remaps the json format to key=value