Created
August 4, 2021 22:18
-
-
Save djeikyb/0dba919d768015747b5035d8c11a143c to your computer and use it in GitHub Desktop.
Launch an app with environment variables pulled from aws secrets manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SELF=${0##*/} | |
die() { | |
log "$@" | |
exit 1 | |
} | |
log() { | |
printf "$SELF: %s\n" "$@" >&2 | |
} | |
dieIfNonZeroStatus() { | |
estatus=$1 # some shells reserve $status | |
if [ $estatus -ne 0 ]; then | |
log "$*" | |
exit $estatus | |
fi | |
} | |
usage() { | |
cat <<EOF | |
usage: $SELF [command [<args>]] | |
Launch an app with env vars pulled from aws secrets. The expanded | |
execution looks something like: | |
env "some:port=5000" "some:env_name=stg" dotnet run myapp.exe | |
given invocation of: | |
$SELF dotnet run myapp.exe | |
Examples: | |
$SELF dotnet run --project SomeProject.csproj -- migrate-database | |
$SELF --help | |
Debugging: | |
A python session can help inspect the environment state: | |
$SELF python | |
In the repl, paste and run: | |
import os; os.environ.keys(); os.environ["ek_cr:db:Port"] | |
EOF | |
} | |
requireCommand() { | |
command -v $1 >/dev/null 2>&1 || die "'$1' is required, but not found" | |
} | |
requireCommand jq | |
[ $# -eq 0 ] && usage && exit 1; | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
"") | |
usage | |
die "unrecognized argument: $1" | |
exit 1 | |
;; | |
--help|help|-help|-h) | |
usage | |
exit 0 | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
secret_name="${AWS_SECRET_NAME:?missing aws secret name}" | |
aws_secret="$(aws secretsmanager get-secret-value --secret-id ${secret_name})" | |
dieIfNonZeroStatus $? "failed to fetch from aws secrets manager" | |
properties=$(printf "%s\n" "$aws_secret" \ | |
| jq '.SecretString | |
| fromjson | |
| to_entries[] | |
| "\(.key)=\(.value)" | |
' \ | |
| perl -wp -e 's/^"(.*)"$/$1/' \ | |
| paste -sd\ - \ | |
) | |
env $properties "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment