Crontabless? Yes, it is possible with Lambda and Cloudwatch

In our daily work we often have the need to plan periodic or recurring activities. In the past this activity has always been managed using cron.
This obliges us, always, to have a server on which to schedule activities.
In recent years we have been using Serverless services with increasing frequency. We will do even more in the future. So we asked ourselves the question of how to transform our crontab into crontabless!

We immediately looked for the services offered by Amazon AWS, and we found what we needed.
Combining Cloudwatch Events Rules and the Lambda functions, we can schedule events at any time.

Cloudwatch Events Rules permits to create rules, which are very similar to Unix-like crontab. With appropriate permissions, when an event rule is executed, we can invoke a Lambda function.

The Lambda function performs the operations which we indicate, or can call other AWS services.

By our choice we decided to create the various events rules with Ansible, but it is possible to do the same operations as GUI. Let’s see a simple example, using the same procedure explained in this article.

The first file that has to be created is:

With a list of dictionaries we can create a lot of events rules at once.
To understand how it works read the article “Lambda function manager”.

In particular, in the main.yml file the task:

Then, it is a good idea to create the code needed to create the events rules on AWS. As mentioned, we use Ansible to do this:

Finally, the Lambda code to make our manage_cron.py operations:

We can, in this file, create the code that queues messages to a message broker (such as RabbitMQ or SQS).
Or we can connect our Lambda function to a server using the SSH protocol (with paramiko in python).
Or simply by recalling other Lambda functions in a recursive way.

We now have a completely Serverless tool to manage our cron or scheduled tasks.

Good fun.

Loreno Edelmondo
Loreno Edelmondo
Articoli: 24

Lascia una risposta

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.