Skip to content

Instantly share code, notes, and snippets.

@zdk
Last active January 3, 2024 20:30
Show Gist options
  • Save zdk/11101f1a4172487b2ae5841500ae32e9 to your computer and use it in GitHub Desktop.
Save zdk/11101f1a4172487b2ae5841500ae32e9 to your computer and use it in GitHub Desktop.
#!/bin/bash
 set -e
 SECRETS_MANAGER="aws secretsmanager"
 REGION="ap-southeast-1"
 
 function get_secret {
$($SECRETS_MANAGER get-secret-value --secret-id $secret --query SecretString --output text --region $REGION)
 }
 
 function parse_secret {
  jq -r 'to_entries[] | "export \(.key)='\''\(.value)'\''"'
 }
 
 read -r -a secrets <<< "$SECRETS"
  for secret in "${secrets[@]}"
 do
  export_vars=$(get_secret | parse_secret)
  eval $export_vars
 done
 exec "$@"
@charltonstanley
Copy link

charltonstanley commented Jul 21, 2023

line 7 has some odd uicode characters present when viewing the gist Raw, causing the script to error.

Here is the clean version of line 7 with the unicode characters replaced:

    $($SECRETS_MANAGER get-secret-value --secret-id $secret --query SecretString --output text --region $REGION)

@charltonstanley
Copy link

Also, line 17 should have no spaces before/after the =, or it will produce an error. It should read:

    export_vars=$(get_secret | parse_secret)

@zdk
Copy link
Author

zdk commented Jul 21, 2023

@charltonstanley Thanks! Updated.

@charltonstanley
Copy link

I had to put this down for a few days, but the last thing I had to do to get it working was put echo in front of the whole command on line 7. Otherwise, it would return with {"KEY":"value"}: command not found. After that last change the script works perfectly. 🎉
Thank you @zdk for posting this. 😄

@zdk
Copy link
Author

zdk commented Jul 27, 2023

@charltonstanley Congrats! Happy you got it working, lol. great stuff 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment