Now, there are 2 ways of doing this: through the AWS Console and through the Elastic Beanstalk config files.
Using AWS Console
Go to the Elastic Beanstalk environment where you want to set environment variables. From the left pane, select ‘Configuration’ and click on ‘Edit’ next to the ‘Software’ option.
Scroll to the bottom. You will see the ‘Environment Properties’ section. You can enter the key value pairs for your environment variables in this section.
When you are done, click on ‘Apply’ and the Environment will be rebuilt with the changes applied.
Using Config Files
If you don’t want to use the console, you can pass in Environment variables in a config file. These need to be passed in option_settings, under the aws:elasticbeanstalk:application:environment namespace. For example, the same settings above can be applied using the config file as follows:
option_settings: aws:elasticbeanstalk:application:environment: GOOGLE_APPLICATION_CREDENTIALS: serviceAccount.json NODE_ENV: dev
The config file needs to be within the .ebextensions directory. You can have multiple files, and they are parsed in the lexicographic order.
Once the config file is created and the code is deployed (either through eb-cli or through Code Pipeline), you will be able to see the environment variables in the ‘Software’ section in the configuration tab.
Reading environment variables
For reading environment variables in your application code, you need to follow the platform specific syntax. For example, on Nodejs, you would read the NODE_ENV environment variable as follows:
var node_env = process.env.NODE_ENV
Similarly, in Python, you would access the variable as follows:
import os node_env = os.environ.get('NODE_ENV')
I hope you liked this article. For more tutorials on AWS, check out https://iotespresso.com/category/aws/. Also, if you are planning to become a certified AWS Solutions Architect, I’d recommend that you check out this course on Udemy. I took this course and found the lectures to be lucid, to-the-point, and fun. I hope they will help you as well.