How can I add periodic tasks to the worker tier of my Elastic Beanstalk environment and troubleshoot issues with periodic tasks that won't get added?

Last updated: 2022-06-24

I want to add periodic tasks to the worker tier of my AWS Elastic Beanstalk environment.

Resolution

Add periodic tasks to a worker tier

To add a periodic task to the worker tier of your Elastic Beanstalk environment, include a cron.yaml file at the root of your source code. For example:

version: 1
cron:
 - name: "schedule"
   url: "/schedule"
   schedule: "0 */12 * * *"

Note: In the preceding example, the schedule periodic task sends an Amazon Simple Queue Service (Amazon SQS) message describing the task to the head of the queue every 12th hour. After the message transverses the queue, the task is picked up by an instance to HTTP POST the "/schedule" endpoint.

If your periodic tasks still don't appear in the worker tier, then complete the steps in the following Troubleshoot issues with periodic tasks section.

Troubleshoot issues with periodic tasks

1.    In the event stream of your Elastic Beanstalk environment, check for an event similar to the following:

Successfully loaded 1 scheduled tasks from cron.yaml

2.    In your /var/log/eb-*.log platform file, check for a log similar to the following:

{"status":"SUCCESS","api_version":"1.0","results":[{"status":"SUCCESS","msg":"","returncode":0,"events":[{"msg":"Successfully loaded 1 scheduled tasks from cron.yaml.","severity":"INFO","timestamp":1562000593}]}]}

3.    In your /var/log/aws-sqsd/default.log file, confirm that the Amazon SQS daemon is running and polling the correct queue.

If the Amazon SQS daemon starts correctly, you will see logs similar to the following:

2019-07-04T14:11:21Z init: initializing aws-sqsd 2.4 (2018-09-06)
2019-07-04T14:11:21Z pollers: start initializting poller timer...
2019-07-04T14:11:21Z pollers: start auto running poller...
2019-07-04T14:11:21Z start: polling https://sqs.us-west-2.amazonaws.com/111122223333/yourWorkerQueue

4.    In your /var/log/aws-sqsd/default.log file, check for the HTTP POST request that the Amazon SQS daemon sends to http://localhost/ on port 80. The request is sent when the daemon polls a queue item.

You will see a message similar to the following:

2019-07-05T13:54:52Z message: sent to http://localhost:80/

5.    To confirm that messages are delivered to the Amazon SQS queue, check the metric NumberOfMessagesReceived for the queue associated with the worker environment.

6.    To invoke the scheduled task manually, run the following command:

curl -d param1=value1 -d param2=value2 -H "Content-Type: application/json" -X POST http://localhost/scheduled-task-path

The -d (--data) flag can hold the data that you're trying to pass to the periodic task.

Note: Periodic tasks don't work for worker environments that are configured with Amazon SQS FIFO (First-In-First-Out) queues.