Environment Variables in CodeDeploy


I have a Django application that uses Gunicorn and NGINX.

The application is using CodeDeploy to launch onto an EC2 Instance running Ubuntu. It needs to access the Django SECRET_KEY variable from the AWS Parameter Store. The problem I'm having is figuring out how to access the environment variables from the Store in my gunicorn.service file.

My settings.py file is set to run "os.environ.get('SECRET_KEY')" in order to get the value. But, I get the error when running "sudo systemctl status gunicorn": "The SECRET_KEY setting must not be empty."

I'm stuck on figuring out how to have these environment variables accessible to the Gunicorn service.

asked 13 days ago26 views
1 Answer

OK. You want to start gunicorn with environment variable SECRET_KEY stored in AWS Parameter Store. If you can customize service file, write service shell file and change service file.

Service shell file(For example, locate at /opt/bin/django.sh. Do not forget to set execute permission.)

    export SECRET_KEY=`aws ssm get-parameter --name [parameter name] | jq -r '.Parameter.Value'`
    /path/to/project/bin/gunicorn --access-logfile - --workers 3 --bind unix:/var/run/gunicorn.sock site_mod.wsgi

Service file(For example, locate at /etc/systemd/system/django.service)

    Description=gunicorn daemon

I run this service, and it can run. I do not test on CodeDeploy, but this solution would help you.

answered 13 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions