AWS SAM + Python + Pytest

Update 07.09.2020: Added an AWS DynamoDB.

In this article we will build, test and deploy an example lambda using SAM, Python3 and Pytest, while maintaining maximum code coverage, development velocity and simplicity.

Why SAM?

While there are other Infrastructure as Code tools out there, I found SAM to be the easiest and most convenient tool for packaging and deploying functions and their resources on AWS.

Part 1: code

Part 2: template

Part 3: test file

Part 4: deployment script

Deployment gates:

  1. unit tests:
    Let’s make sure that an invalid url indeed generates an error in the test:
    In line 42 we expect the application to raise an exception, so in line 41 we’ll switch the test names (change it to “works properly”) and make our test expect an exception, but it will never happen.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store