# Packaging Python AWS Lambda Functions Locally with AWS SAM Build Images

> For the complete documentation index, see [llms.txt](https://iqbalaissaoui.com/llms.txt).

This is a simple guide to package Python AWS Lambda functions with dependencies using AWS SAM build images.

This method allows you to create a deployment package that includes all necessary dependencies for the same runtime as your Lambda function, ensuring compatibility and ease of deployment.

If your Python script is using imports that rely on C extensions, you will need to use the AWS SAM build image that matches the runtime of your Lambda function and basically build on the Docker image and not locally. This is because the C extensions need to be compiled for the specific environment in which the Lambda function will run.

This is a faster approach than containerizing your Lambda function with Docker, at least development-wise, as you can use the AWS SAM CLI to build and package your Lambda function with dependencies in a single command.

## Prerequisites

Let's demo this with any library that has C extensions, such as `regex`, which is a common library used for regular expressions in Python.

```PYTHON
import regex

# simple example that finds all numbers in a string

def lambda_handler(event, context):
    pattern = r'\d+'
    text = '123 abc 456'
    matches = regex.findall(pattern, text)
    return {
        'statusCode': 200,
        'body': matches
    }
```

## Dry Run

If you zip and run this function locally from within the same directory, where the above code is saved as `lambda_function.py`, you will get an error like this:

```BASH
pip install regex -t .
zip -r lambda_function.zip .
```

Once you upload to AWS Lambda and run a test, you'll get an error like this during runtime:

```LOG
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'regex._regex'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "",
  "stackTrace": []
}
```

## Packaging with AWS SAM Build Image

Let's package now with the right runtime that matches the AWS Lambda Python runtime:

```BASH
# run from the folder that contains lambda_function.py
docker run --rm --platform linux/amd64 \
  -v "$PWD":/var/task \
  public.ecr.aws/sam/build-python3.13 \
  /bin/sh -c "pip3.13 install \
                regex \
                -t /var/task && \
              find /var/task -name '*.pyc' -delete"

zip -r lambda.zip . -x '*.DS_Store'
```

This command uses the AWS SAM build image for Python 3.13 to install the `regex` library into the current directory (`$PWD`), ensuring that the library is compatible with the Lambda runtime. The `find` command is used to remove any compiled Python files (`*.pyc`) to keep the package clean.

We then zip the contents of the current directory into `lambda.zip`, excluding any `.DS_Store` files that might be present on macOS.

> **Tip:**
> References
>
>
>
> [AWS SAM Serverless Image Repositories](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-image-repositories.html)

> **Tip:**
> Note:
>
>
> Make sure you run against the same Python version, here using Python 3.13 as runtime and in the SAM image: `public.ecr.aws/sam/build-python3.13`

## Deploying the Lambda Function

Rerun with the new `lambda.zip` file uploaded to AWS Lambda:

```LOG
{
  "statusCode": 200,
  "body": [
    "123",
    "456"
  ]
}
```

