How can I pass variables from my Elastic Beanstalk environment to Linux and Windows instances?

Last updated: 2020-10-08

I want to pass variables from my AWS Elastic Beanstalk environment to Linux and Windows Amazon Elastic Compute Cloud (Amazon EC2) instances.

Short description

You can pass environment variables to Amazon EC2 instances by using the following:

Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.


Important: To pass confidential information (such as a database password) to the instance, follow the instructions at Storing private keys securely in Amazon S3.

Pass your environment variables

Choose one of the following ways to pass your environment variables.

Using the Elastic Beanstalk console:

1.    Open the Elastic Beanstalk console.

2.    Select your application, and then choose Configuration from the navigation pane.

3.    For the Software category, choose Modify.

4.    In the Environment properties section, enter the key-value pairs for the environment properties that you want to pass to your instances.

5.    Choose Apply.

Important: Be sure to consider environment property limits.

Using the EB CLI:

To set an environment variable in the EB CLI, run the following command:

eb setenv key=value

In the following example, the environment variable ExampleVar is set:

$ eb setenv ExampleVar=ExampleValue
2018-07-11 21:05:25    INFO: Environment update is starting.
2018-07-11 21:05:29    INFO: Updating environment tmp-dev's configuration settings.
2018-07-11 21:06:50    INFO: Successfully deployed new configuration to environment.
2018-07-11 21:06:51    INFO: Environment update completed successfully.

To set multiple environment properties, use the following command:

$ eb setenv foo=bar JDBC_CONNECTION_STRING=hello PARAM4= PARAM5=

Using option settings:

You can use Elastic Beanstalk configuration files to set environment properties and configuration options in your source code.

To define environment properties, use the aws:elasticbeanstalk:application:environment namespace.

See the following .ebextensions/options.config example:


Using the AWS CLI:

To set environment properties in the AWS CLI, run the following command:

$ aws elasticbeanstalk update-environment --environment-name my-env --option-settings file://options.json

For example:

    "Namespace": "aws:elasticbeanstalk:application:environment",
    "OptionName": "API_ENDPOINT",
    "Value": ""
    "Namespace": "aws:elasticbeanstalk:application:environment",
    "OptionName": "URL",
    "Value": ""

Access your environment variables

Note: Environment properties aren't automatically exported to the shell, even though they are present in the instance. Instead, environment properties are made available to the application through the stack that it runs in, based on which platform you're using.

To access your environment variables, see Accessing environment properties.

To access your environment variables outside of application code (for example, in a script that runs during deployment), use the get.config platform script or Fn::GetOptionSetting.

For Windows instances, environment properties are passed from C:\ProgramFiles\Amazon\ElasticBeanstalk\config\containerconfiguration to the Microsoft Internet Information Services (IIS) server.

Did this article help?

Do you need billing or technical support?