How do you pass custom environment variable on Amazon Elastic Beanstalk (AWS EBS)?

Amazon Web-ServicesApp ConfigAmazon Elastic-Beanstalk

Amazon Web-Services Problem Overview


The Amazon Elastic Beanstalk blurb says:

> Elastic Beanstalk lets you "open the hood" and retain full control ... even pass environment variables through the Elastic Beanstalk console.

http://aws.amazon.com/elasticbeanstalk/

How to pass other environment variables besides the one in the Elastic Beanstalk configuration?

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

As a heads up to anyone who uses the .ebextensions/*.config way: nowadays you can add, edit and remove environment variables in the Elastic Beanstalk web interface.

The variables are under Configuration → Software Configuration:

Environment Properties

Creating the vars in .ebextensions like in Onema's answer still works.

It can even be preferable, e.g. if you will deploy to another environment later and are afraid of forgetting to manually set them, or if you are ok with committing the values to source control. I use a mix of both.

Solution 2 - Amazon Web-Services

Only 5 values is limiting, or you may want to have a custom environment variable name. You can do this by using the configuration files. Create a directory at the root of your project called

.ebextensions/

Then create a file called environment.config (this file can be called anything but it must have the .config extension) and add the following values

option_settings:
  - option_name: CUSTOM_ENV
    value: staging

After you deploy your application you will see this new value under Environment Details -> Edit Configuration -> Container

for more information check the documentation here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-options

Update

To prevent committing to your repository values like API keys, secrets and so on, you can put a placeholder value.

option_settings:
  - option_name: SOME_API_KEY
    value: placeholder-value-change-me

Later you can go to the AWS admin panel (Environment Details -> Edit Configuration -> Container) and update the values there. In my experience these values do not change after subsequent deployments.

Update 2 As @Benjamin stated in his comment, since the new look and feel was rolled out July 18, 2013 it is possible to define any number of environment variables directly from the console:

Configuration > Software Configuration > Environment Properties

Solution 3 - Amazon Web-Services

In the 2016 Java8 Tomcat8 AMI, ElasticBeanstalk fails to set environment variables from the web configuration. They are really setting jvm -D properties instead.

-- "The following properties are passed into the application as environment variables. Learn more."

This statement is incorrect for the Java Tomcat ami. Amazon does not set these as environment variables. They are set as System properties passed on the command line to Tomcat as a -D property for jvm. The method in Java to get environment variables is not the same for getting a property. System.getenv vs System.getProperty

I ssh'd into the box and verified that the environment variable was never set. However, in the tomcat logs I can see the -D property is set.

I've changed my code to check for both locations now as a workaround.

Solution 4 - Amazon Web-Services

AWS will interpret CloudFormation template strings in your environment variables. You can use this to access information about your EB environment inside your application:

In the AWS web interface the following will be evaluated as the name of your environment (note the back ticks):

`{ "Ref" : "AWSEBEnvironmentName" }`

Or, you can use an .ebextensions/*.config and wrap the CloudFormation template in back ticks (`):

{
  "option_settings": [
    {
      "namespace": "aws:elasticbeanstalk:application:environment",
      "option_name": "ENVIRONMENT_NAME",
      "value": "`{ \"Ref\" : \"AWSEBEnvironmentName\" }`"
    }
  ]
}

Solution 5 - Amazon Web-Services

Alternatively, you could use the Elastic Beanstalk CLI to set environment variables.

To set an environment variable: eb setenv FOO=bar

To view the environment variables: eb printenv

Solution 6 - Amazon Web-Services

Environment Details -> Edit Configuration -> Container

enter image description here

Solution 7 - Amazon Web-Services

This seems to be the only way to set ENVs with dynamic values in beanstalk. I came up with a workaround that works for my multi-docker setup:

  1. Add this to your Dockerfile before building + uploading to your ECS repository:

    CMD eval cat /tmp/envs/env_file$; ;

  2. In your Dockerrun.aws.json file create a volume:

    { "name": "env-file", "host": { "sourcePath": "/var/app/current/envs" } }

  3. Mount volume to your container

    { "sourceVolume": "env-file", "containerPath": "/tmp/envs", "readOnly": true }

  4. In your .ebextensions/options.config file add a container_commands block like so:

    container_commands: 01_create_mount: command: "mkdir -p envs/" 02_create_env_file: command: { "Fn::Join" : [ "", [ 'echo "', "export ENVIRONMENT_NAME=" , { "Ref", "RESOURCE" }, ';" > envs/env_file;' ] ] }

  5. eb deploy and your ENVS should be available in your docker container

You can add more ENVs by adding more container_commands like:

  02_create_env_file_2:
    command: { "Fn::Join" : [ "", [ 'echo "', "export ENVIRONMENT_NAME_2=" , { "Ref", "RESOURCE2" }, ';" >> envs/env_file;' \] \] }

Hope this helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenView Question on Stackoverflow
Solution 1 - Amazon Web-ServiceslimeView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesOnemaView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesEd JView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesGeorge BrasseyView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicestrexView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesBenView Answer on Stackoverflow
Solution 7 - Amazon Web-ServicesdsorensenView Answer on Stackoverflow