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.

demandé il y a 10 mois648 vues
1 réponse
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
répondu il y a 10 mois

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions