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.

질문됨 일 년 전703회 조회
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
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠