AWS Lambda Runtime Interface Client for Python

4.0.0 · active · verified Fri Apr 10

The AWS Lambda Runtime Interface Client (RIC) for Python is a lightweight library that implements the Lambda Runtime API. It enables you to use alternative base images or build custom runtimes for your Python Lambda functions, making them compatible with the Lambda service. It manages the interaction between the Lambda execution environment and your function code, handling invocation events and responses. The library is actively maintained by AWS and is currently at version 4.0.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Python Lambda function using a custom container image with `awslambdaric`. You define your Lambda handler in `app.py` and then create a `Dockerfile` that installs `awslambdaric` and sets it as the container's entrypoint, with your function handler as the command. This allows the Lambda service to properly invoke your Python code within the custom runtime environment.

# app.py
def handler(event, context):
    print(f"Received event: {event}")
    print(f"Context: {context.aws_request_id}")
    return {"statusCode": 200, "body": "Hello from Lambda!"}

# Dockerfile
FROM python:3.9-slim-buster

# Set working directory
WORKDIR /var/task

# Copy function code
COPY app.py .

# Install awslambdaric and any other dependencies
RUN pip install --no-cache-dir awslambdaric

# Set the ENTRYPOINT to the AWS Lambda Runtime Interface Client
ENTRYPOINT ["/usr/local/bin/python", "-m", "awslambdaric"]

# Set the CMD to your Lambda handler
CMD ["app.handler"]

view raw JSON →