Via G. Falcone 5, Pollenza (MC), Italy
+39 0733 203595

Lambda function manager

When we started developing Lambda functions to automate processes, or simply to delegate various jobs, compared to our infrastructure, we faced the problem of how to handle the deploy of functions and maintain the versions of this code.

There are many frameworks, more or less useful and well done, but none had everything we needed. First of all we wanted to keep a single versioning and profiling system. Generally we use Ansible and Git to manage our infrastructure. This allows us to have a simple and powerful versioning and provisioning.

We have therefore decided, at least for the simplest projects, to manage the deploy of Lambda functions in the same way.

We have created a directory in our github repository:

Within lambda_functions we find a structure of this type:

files
It contains the files with the python code to be inserted in the Lambda function. So we need to create a file containing function name.
Here is the code of an example function hello_world.py:

libs
Contains modules, libraries or classes called by Lambda functions.

tasks
It contains additional tasks to include in the main.yml. By default the file hook_ [pre | post]_function_name.yml is included to insert any customizations or additional tasks that Ansible can execute. Pre and post indicate that the script is executed before or after the creation of the function and the installation of any additional packages with pip.
Below is an example of pre and post task. In the first case variables passed to the Lambda function are encrypted. This operation must be done before generating the function (hook_pre_):

Below is a post-task example for creating a cloudwatch alarm metric, which is created after (hook_post_) that we have defined the Lambda function:

vars
It contains the variables needed to play the playbook. Specifically, vars_function_name.yml contains the variables to be modified to customize the Lambda function.

main.yml
It contains instructions for downloading any dependencies specified in the vars file (install_libs), creating the Lambda package, uploading it online, deleting the newly created package locally and finally creating the cloudwatch rule (if requested).

make_function.sh
It contains the executable that calls the playbook Ansible.

At this point it is possible to launch the creation with:

inside

The function, assuming that there are no mistakes, should already be operational on AWS Lambda.

With this system we have collected in one single point our lambda functions in a centralized and controlled way with the github versioning. It is very easy, thanks to Ansible, to make the deploy of new versions.

Good fun.

We’re always looking for talented developers… find out all open job positions!

Related Posts

Leave a reply