App code packages

Describes how to package reusable code

Code Packages use AWS Lambda Layers to make your reusable code available in all your scripts across a single app. Layers allows us to support faster uploads when you make changes to your scripts.

You can write your own reusable code, and/or use Python's pip system to install requirements from Python's rich community library.

Zip up a root "python" folder

Anything you upload needs to be zipped.

This zipped file must to have a root "python" folder in the compressed zip file.

Dependencies must be placed in that folder.

x86 and Amazon Linux

Your scripts run as Python 3.13 using AWS Lambda's x86 architecture, running on a version of Linux specific to AWS Lambda.

This means that packages you pip install on any other system (like your laptop) probably won't work, if the installed packages includes binary code. The solution in these cases is to build packages from a Docker image AWS has made available. See "building from docker" below for how to do that.

However, if your code doesn't have any binary and are just simple Python source code, you can just provide as many .py files as you like, put them in a folder called python , zip it up and call it a day.

Building from Docker

This section describes how to pip install dependencies with binaries that will work on Middle's execution environment (AWS Lambda on x86).

First, ensure you have an up-to-date version of Docker.

  1. Make a new folder titled anything you like. You probably want to pick something relevant to whatever you're doing. Like, let's say you want to talk to MySQL in your code. Maybe call the folder middle-mysql

  2. Within this folder, create a new file titled Dockerfile with the following body.

FROM public.ecr.aws/lambda/python:3.13
  1. Within this folder, make a folder titled python, to which we will pip install our dependencies. We will share this folder between the Docker environment and our host environment.

  2. Come up with a tag that makes sense for whatever you're creating, then build the dockerfile. Continuing on the example of making a code package to talk to MySQL, we might tag it middle-mysql .

  1. Now, we can run the docker container and share the python folder we created earlier.

  1. Within this environment, install requirements with pip into the "python" directory. The -t parameter can be used to do this.

  1. Now, in your host environment, zip up the python directory. This can be uploaded as a code package archive. It's important that the directory python be zipped! AWS Lambda requires dependencies to be packaged in a zip with a python directory at root.

Tutorial

This tutorial shows how to make a simple utilities file to be used across all your scripts in an app.

Let's say you'd like a small utilities file that you can use across your app in Middle.

  1. Create a new folder called "python"

  2. Within that folder, create a file called "utils.py" and include the following code snippet.

  1. Zip up the folder. It's important that you zip up the folder "python", because all code needs to be defined within a folder called "python." Only code within the folder "python" will be accessible to your scripts.

  2. Create a new code package in Middle by navigating to your app, then clicking the "create" link next to code packages in the left panel.

  3. Follow the steps to upload your archive, then deploy it as a layer. Middle supports only four code packages at once.

  4. Toggle the code packages as "live".

  5. Create a test action by clicking "create" next to "actions" on the left panel.

  6. In the script import "utils" and call "hello_world"

  1. Test the script by clicking the "Run test" button.

  2. Find that "hello world" is printed in the log output.

Last updated

Was this helpful?