Twilio environment variable error - python

Twilio-Python works fine if I place my account_sid and auth_token directly into the code but will not work when I set them to environment variables. I'm using PyCharm and set them by going to edit configurations > Environment variables, just as I have done with other variables in the past with no problems. I reference them in my code with:
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
Which throws the following error:
twilio.rest.exceptions.TwilioRestException: HTTP 404 error: The requested resource /2010-04-01/Accounts/'<my account_sid>'/Messages.json was not found
I tried exporting the variable on the CLI and running the code there but got the following error:
KeyError: 'twilio_account_sid'
I have no idea what I am overlooking. Any suggestions?

For OS X El Capitan, add environment variables:
open terminal, then
cd ~/
vi .bash_profile
once in vi editor editing the file, get into insert mode (press i) then add these two lines:
export TWILIO_ACCOUNT_SID="AC0123456789abcdefabcdefabcdefabcd"
export TWILIO_AUTH_TOKEN="0123456789abcdefabcdefabcdefabcd"
get into vi command mode (press escape) save and quit
:wq
reboot your computer
To check environment variables after reboot, open terminal, type printenv, you should see them on the list.
In your script, to use the environment variables, try this
import os
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
For configuring Windows environment variables take a look at my answer
Twilio Auth windows enviro variables

Related

ModuleNotFoundError: No module named 'hello'

I did something really bad. I don't know what I did. I created a test project with hello.py where I did some mistake when running with some command. Now, I have deleted that and back to the real project, and I got the following error:
File "/home/bhojendra/anaconda3/envs/myenv/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app
import(module_name)
ModuleNotFoundError: No module named 'hello'
I don't have even the word hello anywhere in the project.
I have removed all pycaches from the project, cleaned the conda conda clean -a, removed myenv environment and removed pip cache directory. Then, I re-created the environment and and re-installed the requirements and when launching the project with flask run, it throws that error again. Not sure what's happening.
It might be the issue with flask cache, I don't know how to overcome this issue.
In your environment, you likely left your FLASK_APP set to the file hello, so it was trying to run that, although it doesn't exist. You just have to export or set your flask app based on what machine you are using.
Unix Bash (Linux, Mac, etc.):
$ export FLASK_APP=hello
$ flask run
Windows CMD:
> set FLASK_APP=hello
> flask run
Windows PowerShell:
> $env:FLASK_APP = "hello"
> flask run
You could also unset the export:
unset FLASK_APP
And then set the flask app

Why can't I get Environ variables within a Flask script?

I have this script:
import os
secret = os.environ.get('MFS')
print(secret)
For some reason, it returns None. I set that environment variable to a secret key that I want to use to set app.config['SECRET_KEY']. But, if I run this command: python3 -c 'import os;print(os.environ.get("MFS"))', it returns the correct environment variable. It doesn't matter if I cd to the project folder and run that or not, it just works. Even though I am using a venv to run the Flask app, that command still works when I run the venv Python. Why is this happening?

Is there a way to not expose credentials in the code?

I've been using ipython notebook and there are 2 information (SNOW_USER and PASSWORD) that I need to pass so I can connect to the database. I don't want to expose it for security reasons.
I tried to set as ENV VAR (environmental variables) saving it on my .bash_profile and also on .profile using export SNOW_USER='abc' but it doesn't seem ipython can find it.
import os
print os.environ['SNOW_USER']
I also tried:
%env
But the variables are not showing there either.
Any thoughts on how to do it?
Try to create a file .env somewhere with in it :
export SNOW_USER="snow_user"
export PASSWORD="password"
and then source it :
source .env
Or just source you bash_profile file :
source ~/.bash_profile

Cannot get environmental variables in Python sometimes?

So I have a Django/Python 3.4.3 setup with nginx, gunicorn and postgres on Ubuntu Server 14.04. The server is blank and setup following this guide. I have set a few environmental variables in the /etc/environment as follows and rebooted:
DJANGO_DB_NAME="db"
DJANGO_DB_USER="username"
DJANGO_DB_PASSWORD="password"
DJANGO_SECRET_KEY="9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko#_yn6%h"
So from Django's settings.py file I try to access it in a variety of ways, but ran into unexpected behavior:
'NAME': os.getenv('DJANGO_DB_NAME') # this works correctly
'NAME': os.environ.get('DJANGO_DB_NAME') # this works correctly
'NAME': os.environ['DJANGO_DB_NAME'] # this does NOT work and yields 'key' does not exist
None of these works as it returns an empty string instead of the key value:
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
Django Error:
File "/webapps/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 120, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
From within Ubuntu, when I access the environment variables from the command line, I always get the correct result back:
root#ubuntu-512mb-sfo1-01:/webapps# echo $DJANGO_SECRET_KEY
9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko
root#ubuntu-512mb-sfo1-01:/webapps# echo $DJANGO_DB_USER
username
Yet, when I do this from command line it works!
root#ubuntu-512mb-sfo1-01:/webapps# python3 -c "import os; print(os.environ['DJANGO_SECRET_KEY'])"
9g2&ionu!4u#%#2f&(r0dpp_yplyukxde^*1+evf7ko
Now, I am really confused. Any expert know what is going on and how to solve this?
Update 1: per comments by m.wasowski, gunicorn is running as root and running manage.py runserver works just fine again as root. Gunicorn only complains when I run 'service gunicorn start'. Security issues as running as root, or storing key in environment is temporary until I just get it working first.

How to set an environment variable in Amazon Elastic Beanstalk (Python)

I have been working on a Django application lately, trying to get it to work with Amazon Elastic Beanstalk.
In my .ebextensions/python.config file, I have set the following:
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: ProductionBucket
value: s3-bucket-name
- namespace: aws:elasticbeanstalk:application:environment
option_name: ProductionCache
value: memcached-server.site.com:11211
However, whenever I look on the server, no such environment variables are set (and as such, aren't accessible when I try os.getenv('ProductionBucket')
I came across this this page which appears to attempt to document all the namespaces. I've also tried using PARAM1 as the option name, but have had similar results.
How can I set environment variables in Amazon Elastic Beanstalk?
EDIT:
I have also tried adding a command prior to all other commands which would just export an environment variable:
commands:
01_env_vars:
command: "source scripts/env_vars"
... This was also unsuccessful
I was having the same problem.
Believe it or not, you have to commit the .ebextensions directory and all *.config files to version control before you deploy in order for them to show up as environment variables on the server.
In order to keep sensitive information out of version control, you can use a config file like this:
option_settings:
- option_name: API_LOGIN
value: placeholder
- option_name: TRANS_KEY
value: placeholder
- option_name: PROVIDER_ID
value: placeholder
Then edit the configuration in the AWS admin panel (Configuration > Software Configuration > Environment Properties) and update the values there.
You may also find this answer helpful.
Option 1:
You can set environment variables using eb setenv FOO=bar
You can view the environment variables using eb printenv
Option 2:
You can create a config file in your .ebextensions directory, for example 00_environment.config. Then, add your environment variables like this:
option_settings:
- option_name: MY_FIRST_ENV_VAR
value: abc
- option_name: ANOTHER_ENV_VAR
value: 123
However, if you have multiple environments, I have found that it is more useful to set the environment variables directly, using option #1.
I also have found the eb config commands to be helpful: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-config.html
These commands allow you to get, put, list, or delete configuration files on your eb environment.
The command eb config get will save your config, including environment variables, to a local file in .elasticbeanstalk/saved_configs.
I did the following to also get my environment variables that I configure in cloudformation in the non-container phase, eg the regular commands
/opt/elasticbeanstalk/bin/get-config environment | python -c "import json,sys; obj=json.load(sys.stdin); f = open('/tmp/eb_env', 'w'); f.write('\n'.join(map(lambda x: 'export ' + x[0] + '=' + x[1], obj.iteritems())))"
Once you execute this command you will have a file in /tmp/eb_env with all your environment variables. Just execute the following before a command that needs the environment variables
source /tmp/eb_env
Example
source /tmp/eb_env && echo $MY_CUSTOM_ENV
In the config file of elastic beanstalk, it looks like this:
commands:
02-make-sure-we-can-get-our-env-in-the-instance-itself:
command: "/opt/elasticbeanstalk/bin/get-config environment | python -c 'import json,sys; obj=json.load(sys.stdin); f = open(\'/tmp/eb_env\', \'w\'); f.write(\'\n\'.join(map(lambda x: \'export \' + x[0] + \'=\' + x[1], obj.iteritems())))'"
I've checked using a modern (i.e., non legacy) container, and found it under /opt/elasticbeanstalk/deploy/configuration/containerconfiguration as a json file.
The Behaviour seems to be Platform-Dependent: I remember in PHP in particular, it also creates some shell scripts with the values.
Regardless of that, look into /opt/elasticbeanstalk/hooks/configdeploy.
Java case again, it runs this python script, which looks quite handy for you:
https://gist.github.com/19c1e4b718f9a70a4ce1
To set variables on a local run, you can do the following:
eb local setenv CONFIG=dev
eb local run
This also works with Docker MultiContainers, which otherwise will not see your environment.
I know this is an old question but for those who still have the same question like I did here is the solution from AWS documentation: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environments-cfg-softwaresettings.html
To configure environment properties in the Elastic Beanstalk console
Open the Elastic Beanstalk console, and then, in the regions drop-down
list, select your region.
In the navigation pane, choose Environments, and then choose your
environment's name on the list.
In the navigation pane, choose Configuration.
In the Software configuration category, choose Edit.
Under Environment properties, enter key-value pairs.
Choose Apply.

Categories

Resources