HenryM
HenryM

Reputation: 5793

How to check Elastic Beanstalk environment variable in Bash script

I am trying to access an Elastic Beanstalk environment variable from a bash script (within .platform/hooks/postdeploy/.

The script is something like:

if [[ -v "${WORKER_ENV}" ]]; then
    echo -e "Happy times - worker env\n"
    # Do stuff I want to do
else
    echo -e "No worker env variable"
fi

I have 2 instances of the same application. In 1, I have specified WORKER_ENV in the Configuration > Environment properties. In the other I haven't.

When I deploy the application in both environments, the log file shows "No worker env variable". How do I get the script to pick up an environment variable from the Configuration > Environment properties?

Upvotes: 0

Views: 812

Answers (1)

HenryM
HenryM

Reputation: 5793

For anyone else who comes across this, I solved as follows:

WORKER = 0
SUBSTRING="WORKER_ENV"
while read line; do  
    if [[ "$line" == *"$SUBSTRING"* ]]; then
        WORKER=1
        echo "Found WORKER_ENV"
    fi
done < /opt/elasticbeanstalk/deployment/custom_env_var

if [[ $WORKER == 1 ]]; then
    echo -e "Happy times - worker env\n"

In another script I had already copied the env file to custom_env_var because I previously had trouble accessing the env file once the application was deployed in cron jobs. This is working fine now

Upvotes: 1

Related Questions