Reputation: 11662
Is it posssible to reference the PARAM1 / PARAM2 etc.. container environment properties from the .ebextensions config files. If so, how? I tried $PARAM1 but it seemed to be an empty value.
I want to set the hostname on startup to contain DEV, QA or PROD, which I pass to my container via the PARAM1 environment variable.
commands:
01-set-correct-hostname:
command: hostname myappname{$PARAM1}.com
Upvotes: 39
Views: 17019
Reputation: 664
Accepted answer is quite outdated.
Now you can use /opt/elasticbeanstalk/support/envvars
file which is already a shell script ready to be sourced:
commands:
01_update_composer:
command: |
. /opt/elasticbeanstalk/support/envvars
/usr/bin/composer.phar self-update
container_commands:
01_run_composer:
command: |
composer.phar install --no-scripts --no-dev # already has user-specified env variables
Update:
After some deeper investigation turns out that container_commands:
include your environment variables, but commands:
not.
Upvotes: 8
Reputation: 101
To make the environment variables available at the commands stage I parse them into a bash sourceable file.
000001.envvars.config
...
commands:
000001_envvars_to_bash_source_file:
command: |
# source our elastic beanstalk environment variables
/opt/elasticbeanstalk/bin/get-config --output YAML environment|perl -ne "/^\w/ or next; s/: /=/; print qq|\$_|" > /var/tmp/envvars
chmod 400 /var/tmp/envvars
...
Then I use:-
source /var/tmp/envvars
in subsequent commands.
Upvotes: 8
Reputation: 111
Here is what worked for me. I tried the accepted approach and it did not produce the desired result (curly braces were included in the output). Troubleshooting commands that are executed from a .config file when uploading to Elastic Beanstalk is also a bit of a challenge (or I just don't know exactly where to look).
AWS Environment:
Elastic Beanstalk Environment Properties (Configuration -> Software Configuration -> Environment Properties):
Sample .config File included in the .ebextensions folder in the deployment artifact:
container_commands:
0_test-variable:
cwd: /tmp
command: "touch ${HELLO_VARIABLE}_0_.txt"
1_test-variable:
cwd: /tmp
command: "touch {$HELLO_VARIABLE}_1_.txt"
2_test-variable:
cwd: /tmp
command: "touch $HELLO_VARIABLE_2_.txt"
After the artifact has been deployed using Elastic Beanstalk the /tmp directory within an EC2 instance will contain the following files (note curly braces and position of $):
Upvotes: 11
Reputation: 11662
It turns out you can only do this in the container_commands
section, not the commands
section.
This works:
container_commands:
01-set-correct-hostname:
command: "hostname myappname{$PARAM1}.com"
See http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands for more details.
Upvotes: 29