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.

已提问 1 年前699 查看次数
1 回答
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
已回答 1 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则