Zappa, the packager

Are you having problems with packaging your dependencies for a Lambda? You've done the AWS lambda python tutorials but now need to deal with something more complex? Do you just want to build a package to upload in the console or with terraform?

Zappa is a great tool to handle everything from publishing your code to have it integrated with other AWS services with the minimum modifications possible. That results in sometimes ignoring that you can use for some smaller roles, like just packaging a lambda in a zip file that you can then use on the console and/or terraform.

So, let's imagine you have your code in a file called myfunction.py doing something like:

def lambda_handler(event, context):
    print('Hello World!')

To package it do the following:

  1. Create a virtual environment for your function: python3 -m venv ve for python 3.6.

  2. Activate the virtual environment: . ve/bin/activate (on Mac OS X or GNU/Linux) or ve/Scripts/Activate (on Windows).

  3. Install zappa and other dependencies you need with pip install <dependency>.

  4. Create a zappa_settings.json with the following content:

    {
        "dev": {
            "lambda_handler": "myfunction.lambda_handler",
            "runtime": "python3.6"
        }
    }
    
  5. Run zappa package dev.

  6. And that's it, with that zappa has created a zip file that you can now upload to AWS.

links

social