6

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.

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this is true. I just ran a test with a very simple Django website, adding an endpoint to dump out the entire environment. All of the aws:elasticbeanstalk:application:environment variables in my .ebextensions/foo.config are in the environment of the WSGI process (as well as the container_commands processes, which is indirectly how I ended up at this question).
2

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.

9 Comments

I was using the correct variable names. I changed it in the question to avoid future confusion.
Have you tried sshing into the server and checking if the env var contains the desired value? by using echo $TWITTER_ACCESS_TOKEN, for instance?
from eb ssh MY_ENVIRONMENT I ran printenv and couldn't see the Twitter API keys and echo $TWITTER_ACCESS_TOKEN did not work. Is there a specific place in the EC2 where I should be checking?
However if I run . /opt/python/current/env then I can access them. So they are making it to the server.
Speaking from my experience - in my company we keep these kind of secrets in an s3 bucket and download into the app as a part of the beanstalk deploy process.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.