Forked from almerindo/read_dotenv_and_create_parameter_store_on_aws.sh
Created
July 31, 2023 11:08
-
-
Save petronetto/57203ebb8b1b423576b4426e0a3d72ad to your computer and use it in GitHub Desktop.
Bash script to read .env and creates parameter store on aws.
This file contains 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/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