-->

Referencing env variables from Elastic Beanstalk .

2019-01-16 14:05发布

问题:

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

回答1:

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.



回答2:

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


回答3:

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.



回答4:

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.



回答5:

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/