Poetry Plugin: make AWS SAM work with python’s poetry

Poetry Plugin make AWS SAM work with python's poetry

AWS creates good tools to work with its resources. The tool SAM, Serverless Application Model, takes your python code, packages it up and deploys to AWS lambda. It is similar to other tools like Serverless Framework. It simplifies the process to get your code into a lambda and also manages the configuration. There are limitations, however, to how a project’s dependencies are defined using SAM. SAM makes use of a requirements.txt file for the definition of what needs to be installed and deployed with the lambda.

Poetry has a plugin framework to add functionality to the poetry cli. The package poetry-aws-sam is a poetry plugin that works with SAM without the need of a requirements.txt file.

Reference pages:
Serverless Framework
AWS SAM Webpage

Scenario

One of Pinnacle Solutions Group’s clients decided corporately to manage its lambdas through SAM. This worked well because the group was making use of a requirements.txt file to manage project dependencies. Also, SAM made the deployment process more simple. However, when the team later implemented poetry for managing dependencies, we needed to build a poetry plugin that would work with the SAM cli without the team managing a requirements file.

Solution

The solution that the team implemented was to make use of poetry’s plugin framework. It allowed the team to gain the benefits of using poetry (for example a lock file) without adding more steps to the deployment and more importantly without needing to manage a requirements.txt file.

The plugin, poetry-aws-sam, replaces the build command in SAM cli. Running poetry sam will build the files locally that can then be deployed to AWS. After the poetry sam command is run, you can run sam deploy to push the build to your lambda.

Code

The package is on pypi at pypi url and the code is found on github at github repo

The readme of the repository explains how to install it and how to make use of the plugin.