Skip to content

Instantly share code, notes, and snippets.

@channainfo
Last active July 8, 2019 11:31
Show Gist options
  • Save channainfo/7228074be64b55f0485f78c09585bd5e to your computer and use it in GitHub Desktop.
Save channainfo/7228074be64b55f0485f78c09585bd5e to your computer and use it in GitHub Desktop.
bash script to read aws parameter store to update elastic beanstalk env variable
#!/usr/bin/env bash
## to update the main process with >> . file_name
##function definition
process_json(){
#$1: json string
#$2: indicee
#$3: env name
json=$1
i=$2
app_env=$3
element_name=$(echo $json | jq -r ".Parameters[$i].Name")
element_value=$(echo $json | jq -r ".Parameters[$i].Value")
env_var_name=$(variable_name "$element_name" "$app_env")
command="export $env_var_name=$element_value"
echo "$command"
export "$env_var_name"="$element_value"
}
variable_name(){
var_name=$1 # "/app_env/name" come with double quote
app_env=$2
app_env_path="/$app_env/"
app_env_path_length=${#app_env_path}
env_var_name="${var_name:($app_env_path_length)}"
echo $env_var_name
}
console_log(){
display_log=1
if [ $display_log -eq 1 ]
then
echo $@
fi
}
fetch_config(){
app_env=$1
json_str="
{
\"Parameters\": [
{
\"Name\": \"/$app_env/APP_NAME\",
\"Type\": \"String\",
\"Value\": \"BookMeBus\"
},
{
\"Name\": \"/$app_env/APP_VERSION\",
\"Type\": \"String\",
\"Value\": \"StgVersion\"
},
{
\"Name\": \"/$app_env/COMPANY_NAME\",
\"Type\": \"String\",
\"Value\": \"System Manager SM\"
},
{
\"Name\": \"/$app_env/ERROR_NOTIFICATION\",
\"Type\": \"String\",
\"Value\": \"no\"
},
{
\"Name\": \"/$app_env/SLACK_CHANNEL\",
\"Type\": \"String\",
\"Value\": \"#error-staging\"
},
{
\"Name\": \"/$app_env/SLACK_WEB_HOOK\",
\"Type\": \"String\",
\"Value\": \"https://hooks.slack.com/services/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\"
}
]
}
"
# command_load_params="aws --region=ap-southeast-1 ssm get-parameters-by-path --path /$app_env"
# echo $command_load_params
# json_str=$($command_load_params)
echo $json_str
}
if [[ -z "${APP_ENV}" ]]; then
error="ENV['APP_ENV'] must be set "
echo $error
exit 1
elif [[ $APP_ENV != "staging" && $APP_ENV != "production" ]]; then
error="ENV['APP_ENV'] must be staging or production"
exit 1
fi
app_env=$APP_ENV
json=$(fetch_config "$app_env")
# console_log "json is: $json"
# echo requires
# console_log $json | jq '.Parameters'
# console_log $json | jq '[.Parameters[] | {Name: .Name, Value: .Value}] '
length=$(echo $json | jq '.Parameters | length')
# console_log "length=$length"
i=0
while [ $i -lt $length ]
do
# Passing variables to a function quote is required surround variable name
process_json "$json" "$i" "$app_env"
((i++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment