Python Functional Testing for AWS Lambda

In this post I will explain and go over how I personally write functional tests for my AWS Lambda functions.

For our example use case, we will assume that our Lambda function is quite complex, as in that it involves downloading a file, processing it, and uploading a result. Additionally it will be called by an SQS event, and it make use of other non-Python related elements, as we will see later.

Let’s get started!

python pytest aws lambda serverless tdd

Pytest Tricks for Better Python Tests

Pytest is my Python testing framework of choice. Very easy to use, and makes tests look much better.

In this article I’ll show you some cool tricks I have incorporated into my test suites using Pytest.

Environment Variables

When your application must work with defined environment variables, the testing environment must have these variables defined as well, even if the values are not real.

With Pytest we can easily configure any necessary environment variables in our test environment. We simply create a fixture in that will be loaded automatically:


import pytest

def env_setup(monkeypatch):
    monkeypatch.setenv('MY_SETTING', 'some-value')
    monkeypatch.setenv('ANOTHER_SETTING', 'some-value')

monkeypatch is a built-in pytest fixture that allows us to set environment variables in the test runs. By enabling the autouse option, our custom environment setup fixture will be automatically called in every test without having to include it explicitly using the usual dependency injection mechanism.

python pytestб tdd

CloudFront CORS Font Issues

I was experiencing an issue with CloudFront and some static assets of a Django application deployed to ElasticBeanstalk. The issue was happening when trying to retrieve some fonts that are stored in S3 and served by CloudFront, the browser dev tools would show some errors similar to the following:

Access to Font at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

A CORS issue. And apparently, very common when trying to load fonts.

To fix this, there are a few possible things that you might have to try. I will list some of the fixes I found.

cloudfront cors

Cancellable Tasks in Netbeans Platform

I was trying to implement cancellable tasks in Netbeans Platform using this blog post as reference.

However I never managed to make it work. The tasks’s thread would never get interrupted even after the confirmation dialog, the task would just keep running.

After some digging on the Netbeans Platform and IDE source code, I think I managed to find a way to properly and easily implement cancellable tasks.

netbeans platform

Recursive Python AWS Lambda Functions

There are times where some processing task cannot be completed under the AWS Lambda timeout limit (a maximum of 5 minutes as of this writing). A possible solution for these kind of situations is to implement a recursive approach to perform the processing task.

Basically, if you are able to separate the task into multiple chunks or batches, then you could make each batch to be processed by a different Lambda function. The amount of Lambda functions necessary to finish the task will scale accordingly.

aws lambda python serverless
← Newer Posts Older Posts →