Environment Variables in CodeDeploy

0

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.

preguntada hace 10 meses647 visualizaciones
1 Respuesta
0

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.)

    #!/bin/bash
    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)

    [Unit]
    Description=gunicorn daemon
    Requires=gunicorn.sock
    After=network.target
    [Service]
    User=service-user
    Group=service-group
    WorkingDirectory=/path/to/project
    ExecStart=/opt/bin/django.sh
    [Install]
    WantedBy=multi-user.target

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

MH35
respondido hace 10 meses

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas