Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save almerindo/45e1bb186c48c48b7e964c6a3ba0bc0e to your computer and use it in GitHub Desktop.
Save almerindo/45e1bb186c48c48b7e964c6a3ba0bc0e to your computer and use it in GitHub Desktop.
Bash script to read .env and creates parameter store on aws.
#! /bin/bash
ENVS=$(egrep -v '^#' .env | xargs)
ARRAY_ENVS=($(echo $ENVS | tr ' ' ' '))
prop="name"
APP_NAME="$(node -pe "require('./package.json')['$prop']")"
TOTAL="${#ARRAY_ENVS[@]}"
TOTAL=$((TOTAL - 1))
function findNodeEnvAndSetPrefix() {
for i in $(seq 0 $TOTAL); do
echo "ele: ${ARRAY_ENVS[i]}"
property=($(echo ${ARRAY_ENVS[i]} | tr '=' ' '))
key=${property[0]}
value=${property[1]}
echo "key: ${key}"
echo "value: ${value}"
echo $NODE_ENV
if [ "$key" = "NODE_ENV" ]; then
if [ "$value" = "DEV" ]; then
echo "DEVELOPER ENVIROMENT"
PREFIX="/staging/${APP_NAME}"
break;
else
echo "ATTENTION! PRODUCTION ENVIROMENT! "
PREFIX="/prod/${APP_NAME}"
break;
fi
fi
done
}
function findAndCreateParameters() {
for i in $(seq 0 $TOTAL); do
property=($(echo ${ARRAY_ENVS[i]} | tr '=' ' '))
key=${property[0]}
value=${property[1]}
echo "Creating property: ${PREFIX}/${key} -> ${value}"
aws --region us-east-1 --profile default ssm put-parameter --name "${PREFIX}/${key}" --value "$value" --type "String"
done
}
findNodeEnvAndSetPrefix
findAndCreateParameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment