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

Last updated: 2019-11-20

How can I 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:

Resolution

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:

option_settings:
  aws:elasticbeanstalk:application:environment:
    API_ENDPOINT: www.example.com/api

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

See the following example:

[
  {
    "Namespace": "aws:elasticbeanstalk:application:environment",
    "OptionName": "API_ENDPOINT",
    "Value": "www.example.com/api"
  },
  {
    "Namespace": "aws:elasticbeanstalk:application:environment",
    "OptionName": "URL",
    "Value": "http://myurl.com"
  }
]

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.

Note: To pass confidential information (such as a database password) to the instance, follow the instructions at Storing Private Keys Securely in Amazon S3.


Did this article help you?

Anything we could improve?


Need more help?