I want to pass properties from my AWS Elastic Beanstalk environment to Amazon Elastic Compute Cloud (Amazon EC2) Linux and Windows instances.
Resolution
Important: To pass confidential information to the instance, such as a database password, see Storing private keys securely in Amazon S3.
To pass environment properties to Amazon EC2 instances, use one of the following options:
- Elastic Beanstalk console
- Elastic Beanstalk Command Line Interface (EB CLI)
- Option settings
- AWS Command Line Interface (AWS CLI)
Pass your environment properties
Use the Elastic Beanstalk console
To use the Elastic Beanstalk console to pass your environment properties, see Configuring environment properties (environment variables).
Important: Make sure that you stay within the environment property limits.
Use the EB CLI
To use the Elastic Beanstalk CLI to set an environment property, run the following command:
eb setenv key=value
In the following example, the ExampleVar environment property 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, Run the following command:
$ eb setenv foo=bar JDBC_CONNECTION_STRING=hello PARAM4= PARAM5=
Use option settings
To set environment properties and configuration options in your source code, use Elastic Beanstalk configuration files.
To define environment properties, use the aws:elasticbeanstalk:application:environment namespace, such as in the following .ebextensions/options.config example:
option_settings:
aws:elasticbeanstalk:application:environment:
API_ENDPOINT: www.example.com/api
Use the AWS CLI
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
To use the AWS CLI to set environment properties, run the update-environment command:
$ aws elasticbeanstalk update-environment --environment-name my-env --option-settings file://options.json
Example output:
[
{
"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 properties
Even though they're in the instance, environment properties aren't automatically exported to the shell. Instead, environment properties are available to the application through the stack that the application runs in based on the platform that's used.
To access your environment properties, see Accessing environment properties.
To access your environment properties outside the application code, use Fn::GetOptionSetting or the get-config tool.
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 notification.