Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Elastic Beanstalk Environment Variables in Python

Recently I have been trying to deploy a django webapp to AWS Elastic Beanstalk and everything has been going fine. However part of my app uses that Twitter API so I need to import my API keys. My understanding is that I should use Configuration > Software Configurations > Environment Properties. I set this up inputting my keys but when I checked the site it still failed.

I have been using this to try and import the variables is that correct?

import os

os.enviorn.get('TWITTER_ACCESS_TOKEN')

I checked to see if the variables were making it to the server and when I ran eb printenv I was shown this:

 Environment Variables:
     TWITTER_ACCESS_TOKEN = XXXXX
     TWITTER_ACCESS_SECRET = XXXX
     TWITTER_CONSUMER_SECRET = XXXX
     TWITTER_CONSUMER_KEY = XXXXX

Any help would be greatly appreciated.

like image 916
David Lance Avatar asked Aug 04 '16 13:08

David Lance


People also ask

How do I deploy Python code in AWS Elastic Beanstalk?

Use the Elastic Beanstalk console to configure Python process settings, enable AWS X-Ray, enable log rotation to Amazon S3, and configure variables that your application can read from the environment. Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region.

What are the environment types on AWS Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment. The type of environment that you require depends on the application that you deploy.


2 Answers

In order to get system environment from AWS Elastic Beanstalk Properties (which is not OS environment variables) you need to "source" it to your environment. In case of Python, EB Properties are stored at /opt/python/current/env file. So simply run this command:

source /opt/python/current/env

Now you got your env variables updated.

like image 125
huy mai Avatar answered Oct 26 '22 23:10

huy mai


The key you are trying to get doesn't exist among your environment variables. Changing the code to - os.environ.get('TWITTER_ACCESS_TOKEN') or any other key among your env vars should do the trick.

like image 33
Yaron Idan Avatar answered Oct 27 '22 00:10

Yaron Idan