How to Set Custom Environment Variables on an AWS Elastic Beanstalk App?

The easiest way to set environment variables on Beanstalk apps is from the console:

But if you need your variables as a part of your deployment package, maybe because you want them under version control or because you want they to be applied to any new environments you create, simple create a .ebextensions/*.config file:

option_settings:
- option_name: ENV_KEY
  value: ENV_VAL

After deployment, these will become available in the console, so you can modify them there too if required.

If the variable’s value is a secret, you can put a placeholder in the .config file & change it manually in the console after the deployment. Don’t worry, the value once set won’t change in subsequent deployments.

NOTE β€” For Java apps, these aren’t set as environment variables, but as system properties. That’s the kind you set using the java -D command when running JARs locally. So don’t do System.getenv() in your code; do System.getProperty() instead!

For .NET apps, these are inserted into your web.config, not environment.

Environment properties are passed only to the application and can’t be viewed by connecting an instance in your environment and running env.

β€” Accessing environment properties

See here for code samples for how to access environment variables in every language.

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

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

You can also use the EB CLI β€” eb setenv FOO=bar & eb printenv.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.