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.
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.
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.
# The python version to use is supplied as an arg from SSTARG PYTHON_VERSION=3.11
# Use an official AWS Lambda base image for PythonFROM 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.pyLocally, you want to set the Python version in your pyproject.toml to make sure
that sst dev uses the same version as sst deploy.
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,});from core.ping import pingfrom 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}", }from core.ping import pingfrom 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.