1

I updated AWS platform from "Python 3.6 running on 64bit Amazon Linux" to "Python 3.7 running on 64bit Amazon Linux 2" and I'm not able to execute crons because I don't know how to add reference for Django environment properties there. Could you help me?

On the old platform everything works fine with this ".ebextensions/cron.config" setup:

files:
"/etc/cron.d/mycron":
    mode: "000644"
    owner: root
    group: root
    content: |
        3 * * * * root /usr/local/bin/script.sh > /home/ec2-user/script.log 2>&1

"/usr/local/bin/script.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
        #!/bin/bash

        date > /tmp/date
        # actual script content
        source /opt/python/run/venv/bin/activate
        source /opt/python/current/env
        cd /opt/python/current/app/
        python manage.py send_notification
        exit 0

I know that I have to make changes on Linux 2 platform and I have to run activate -script from different location and this is what I have now:

files:
"/etc/cron.d/mycron":
    mode: "000644"
    owner: root
    group: root
    content: |
        3 * * * * root /usr/local/bin/script.sh > /home/ec2-user/script.log 2>&1

"/usr/local/bin/script.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
        #!/bin/bash

        date > /tmp/date
        # actual script content
        source /var/app/venv/*/bin/activate
        cd /var/app/current/
        python manage.py send_notification
        exit 0

I have also noticed that everything works fine when I try to do the same from .platform/hooks/predeploy and executing python from /var/app/staging, but that path is not available later on. The problem is that os.Environment - variables were not loaded from the EB configuration and I got KeyError from os.Environment['SOME_ENVIRONMENT_KEY'] reference.

2 Answers2

1

If someone else is looking for resolution, I already resolved the problem with help of this document: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/

0

Here is what worked for me:

files:
    /usr/local/bin/my_cmd.sh:
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash
            source /var/app/venv/*/bin/activate
            cd /var/app/current
            envvars="$(/opt/elasticbeanstalk/bin/get-config environment)"
            # export env vars so they are available for the python management command
            for s in $(echo $envvars | jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" ); do
                export $s
            done
            # check env var is there
            env | grep RDS_HOSTNAME
            python manage.py my_cmd

    /etc/cron.d/my_cmd_cron:
        mode: "000644"
        owner: root
        group: root
        content: |
            */5 * * * * root /usr/local/bin/my_cmd.sh >> /home/ec2-user/logs/my_cmd.log 2>&1

commands:
    rm_old_cron:
        # remove old versions of the cron job on deloyment
        command: "rm -fr /etc/cron.d/*.bak"
        ignoreErrors: true
    yum:
        jq: []

Background:

jimmybutton
  • 143
  • 1
  • 9