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
.