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

Last updated: 2022-09-16

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.

Resolution

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.

Use the Elastic Beanstalk console

To pass your environment variables using the console, follow the instructions in Configuring environment properties.

Important: Be sure to consider environment property limits.

Use 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=

Use 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

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": "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 the platform that 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. Warning: This file location is subject to the Elastic Beanstalk framework and can be changed without warning.


Did this article help?


Do you need billing or technical support?