37

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
Kevin
  • 11,181
  • 22
  • 78
  • 97
  • Why is there still no answer for this?!?!? Below only tells you how to do it with `contaienr_comands` not `commands`. – ThomasReggi Mar 09 '16 at 00:05

5 Answers5

28

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.

Kevin
  • 11,181
  • 22
  • 78
  • 97
  • That link doesn't mention anything about using environment variables in container commands. – Nate Sep 12 '14 at 17:51
  • 1
    @Nate Read the Container_Commands section, where it says "They also have access to environment variables such as your AWS security credentials." – Kevin Sep 12 '14 at 19:11
  • Won't you run into hostname problems if your beanstalk configuration is spawning multiple ec2 instances? – srirachapills Oct 28 '14 at 04:27
  • does this work for regular `commands:`? My testing indicates not. – Peter Ehrlich Apr 16 '15 at 23:51
  • @PeterEhrlich, per the answer, this *only* works on container_commands. – Kevin Apr 18 '15 at 21:26
  • 2
    I don't think this works anymore since these params are no longer environment variables but application variables instead. – GuiSim Jun 07 '16 at 12:51
11

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:

  • Type - Elastic Beanstalk
  • Platform - 64bit Amazon Linux 2015.09 v2.0.4 running PHP 5.6

Elastic Beanstalk Environment Properties (Configuration -> Software Configuration -> Environment Properties):

  • Property Name - HELLO_VARIABLE
  • Property Value - test

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 $):

  • touch ${HELLO_VARIABLE}_0_.txt creates /tmp/test_0_.txt
  • touch {$HELLO_VARIABLE}_1_.txt creates /tmp/{test}_1_.txt
  • touch $HELLO_VARIABLE_2_.txt creates /tmp/.txt
Maciej
  • 111
  • 1
  • 3
  • The reason you don't see your command output in the log files is because EB is only including specific log files. But, you can add the cfn-init-cmd.log to the bundle. I explain it here for Windows, but you should be able to figure out how to change it for Linux. http://stackoverflow.com/a/37189606/674488 – tayl0rs May 12 '16 at 14:56
  • for troubleshooting look in /var/log/eb-activity.log – denov Jul 16 '16 at 05:14
8

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.

gilesw
  • 101
  • 1
  • 4
7

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.

Roman Zhuzha
  • 504
  • 5
  • 7
0

This blog describes in detail various options on how you can achieve this.

https://www.onica.com/blog/how-to-call-and-export-variables-in-elastic-beanstalk/

Chacko Mathew
  • 1,301
  • 1
  • 17
  • 39