Cron Job not picking up environment variables

0

am deploying a FastAPI application using ElasticBeanstalk. Part of my what I would like to do, is to have a script to collect some data and post it on an RDS postgres database using a cron job. After some digging, I managed to get the cron job to work, but not fully.

The script to post data uses the RDS_USERNAME... environment variables. I have the following configs files:

option_settings:
  aws:elasticbeanstalk:application:environment:
    PYTHONPATH: "/var/app/current:$PYTHONPATH"
  aws:elasticbeanstalk:container:python:
    WSGIPath: "main:app"
  commands:
    setvars:
        command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/local.sh
packages:
    yum:
        jq: []
container_commands:
  01_initdb:
    command: "source /var/app/venv/*/bin/activate && python3 init_db.py"
    leader_only: true
  02_some_cron_job:
    command: "cat .ebextensions/cron_job.txt > /etc/cron.d/cron_job && chmod 644 /etc/cron.d/cron_job"
    leader_only: true

I have a command that reads the AWS enviroment variables and sets them to the session.

The cron as I have said is running. This is the error I get from the /var/log/cron:

CMDOUT (KeyError: 'RDS_USERNAME')

The python file that is connecting to the database:

DATABASE_URL = \
        'postgresql://{username}:{password}@{host}:{port}/{database}'.format(
            username=os.environ['RDS_USERNAME'],
            password=os.environ['RDS_PASSWORD'],
            host=os.environ['RDS_HOSTNAME'],
            port=os.environ['RDS_PORT'],
            database=os.environ['RDS_DB_NAME'],
        )

And this is the cron_job.txt file:

* * * * * ec2-user /var/app/venv/*/bin/python3 /var/app/current/cron.py

1 Answer
0

To run a cron job that connects to an RDS database from an Elastic Beanstalk application, you can use a .ebextensions configuration file.

Place a file such as 01_cronjob.config in a .ebextensions directory at the root of your application source code.

The file should contain:

files:
  "/etc/cron.d/cronjob" :
    mode: "000644"
    owner: root
    group: root
    content: |
      */5 * * * * root cd /var/app/current && ./script.sh >> /var/log/cronjob.log 2>&1

This will run the script.sh file inside your application directory every 5 minutes.

The script can then connect to the RDS database using the RDS_HOSTNAME, RDS_USERNAME, RDS_PASSWORD environment variables provided by Elastic Beanstalk.

Make sure to grant access to the security group of your EC2 instances to connect to the RDS port from its IP range.

profile picture
EXPERT
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions