The AWS Elastic Beanstalk documentation does not list exactly how environment variables are exported (or just simply set for top level shell commands). Since we use variables for all kinds of things and we needed a definitive guide of how environment variables are used, I decided to create a how-to guide for calling and viewing environment variables in ebextension config files.
In this post we’ll dive into a number of different ways to call them and see how they show up under different circumstances:
Environment Variables in ebextension config files: How to call and export in Elastic Beanstalk
Lets take the following as our .ebextensions/00-variables.config
file. This will be the basis for all of our tests:
option_settings:
|
commands
and container_commands
echo
output
The first test is to see how these two can be used in the commands and container_commands sections of an ebextensions config file:
commands: a00_command_line_test: command: “echo $envvar1 > /tmp/a00_command_line_test” container_commands: c00_command_line_test: command: “echo $envvar1 > /tmp/c00_command_line_test” |
Here are the results:
|
The/tmp/a00_command_line_test
file ends up being empty while the latter/tmp/c00_command_line_test
file has thevalue1
value in it as expected.
Theenvvar1
variable is not even seen when used incommands
!
To force this you can use theenv:
option along with thecommand:
option:
|
get-config
output
get-config
The next test I conducted is simply to use the get-config
command included in all Elastic Beanstalk AMIs:
|
|
Both files have the variable in it, but there’s no newline. This is due to the echo
command above natively adding a newline. I like this command in shell scripts like so:
somevar=$(/opt/elasticbeanstalk/bin/get-config environment -k envvar1)
files
The next tests I did were around the files directive. I created two files – a bash script and a python script. Each one tests the ability to:
1. Simply just call an environment variable when ran
2. Use the backtick + curly brace + Fn::GetOptionSetting
command embedded in the file
3. Use the get-config
command
|
The result of the files
directive are these files:
|
|
It is very important to notice how this affects scripts
In the second test on both the shell script and the python script, the variable is already embedded into the script! You can run either script at any time under any user and it’ll always have the right variable there. The variable is ‘set’ when the file is created (at deploy time) rather than evaluating the variable when it is ran.
If the environment variable changes and this file is not updated (say it is removed from the ebextentions config file but still called by another script/program), it will then have the wrong variable in it.
The other two tests (env_method_1
and env_method_3
) still gather the environment variable when they’re ran. Lets run them under commands
and container_commands
and check out the results:
|
|
As we can see from these results, the commands
doesnt seem to see the $envvar1
variable in env_method_1
of either script. Otherwise, all scripts pick up all other variables.
Next, I’ll run these as standard ec2-user
via ssh:
|
As you can see, sudo
is required for the get-config
command. Lets try that:
|
Running them as root
(via sudo) yields effectively the same results as running under commands
. However, if we run them under the webserver user (via the actual webserver):
|
|
As you can see, the webserver has the environment variables set but the get-config
command is still limited to the root
user, therefore it fails.
All done!
Hopefully the above experiments shed some light on how environment variables are used in Elastic Beanstalk. I see all tools as having their place and I believe each one of the methods above have appropriate use cases – it’ll be up to you to determine which method(s) you want to use and in which situations.
Happy scripting and be sure to contact us if you need any help with Elastic Beanstalk.