Skip to content
25K
Console

AWS Lambda Python container

Python Lambda function that use large dependencies like numpy and pandas, can hit the 250MB Lambda package limit. To work around this, you can deploy them as a container image to Lambda.

In this example, we deploy two functions as container image.

sst.config.ts
const base = new sst.aws.Function("PythonFn", {
python: {
container: true,
},
handler: "./functions/src/functions/api.handler",
runtime: "python3.11",
link: [linkableValue],
url: true,
});

Now when you run sst deploy, it uses a built-in Dockerfile to build the image and deploy it. You’ll need to have the Docker daemon running.

To use a custom Dockerfile, you can place a Dockerfile in the root of the uv workspace for your function.

sst.config.ts
const custom = new sst.aws.Function("PythonFnCustom", {
python: {
container: true,
},
handler: "./custom_dockerfile/src/custom_dockerfile/api.handler",
runtime: "python3.11",
link: [linkableValue],
url: true,
});

Here we have a Dockerfile in the custom_dockerfile/ directory.

custom_dockerfile/Dockerfile
# The python version to use is supplied as an arg from SST
ARG PYTHON_VERSION=3.11
# Use an official AWS Lambda base image for Python
FROM public.ecr.aws/lambda/python:${PYTHON_VERSION}
# ...

The project structure looks something like this.

├── sst.config.ts
├── pyproject.toml
└── custom_dockerfile
├── pyproject.toml
├── Dockerfile
└── src
└── custom_dockerfile
└── api.py

Locally, you want to set the Python version in your pyproject.toml to make sure that sst dev uses the same version as sst deploy.

sst.config.ts
const linkableValue = new sst.Linkable("MyLinkableValue", {
properties: {
foo: "Hello World",
},
});
const base = new sst.aws.Function("PythonFn", {
python: {
container: true,
},
handler: "./functions/src/functions/api.handler",
runtime: "python3.11",
link: [linkableValue],
url: true,
});
const custom = new sst.aws.Function("PythonFnCustom", {
python: {
container: true,
},
handler: "./custom_dockerfile/src/custom_dockerfile/api.handler",
runtime: "python3.11",
link: [linkableValue],
url: true,
});
functions/src/functions/api.py
from core.ping import ping
from sst import Resource
def handler(event, context):
response_code = ping()
print(f"Response code: {response_code}")
return {
"statusCode": 200,
"body": f"Hello, World! - Linkable value: {Resource.MyLinkableValue.foo}",
}
custom_dockerfile/src/custom_dockerfile/api.py
from core.ping import ping
from sst import Resource
def handler(event, context):
response_code = ping()
print(f"Response code: {response_code}")
return {
"statusCode": 200,
"body": f"Hello, World! - Linkable value: {Resource.MyLinkableValue.foo}",
}

View the full example.