FastAPI
Deploy a Python FastAPI app as a Lambda function with a linked value.
const linkableValue = new sst.Linkable("MyLinkableValue", { properties: { foo: "Hello World", },});
const fastapi = new sst.aws.Function("FastAPI", { handler: "functions/src/functions/api.handler", runtime: "python3.11", url: true, link: [linkableValue],});from fastapi import FastAPIfrom mangum import Mangumfrom sst import Resourcefrom functions.shared import foofrom core.db import get_userfrom core.ping import ping
# Create FastAPI app with JSON configurationapp = FastAPI( title="SST FastAPI App", json_encoder=None # Use FastAPI's default JSON encoder)
# Create a route@app.get("/")async def root(): print("Function invoked from Python")
# Share code within the same workspace package result = foo() print(result)
# Share code between workspace packages res = ping() user = get_user("1234") print(user) print("Ping result:", res)
# Use the SST SDK to access resources resource_value = Resource.MyLinkableValue.foo print(f"Resource.MyLinkableValue.foo: {resource_value}")
return { "message": f"{resource_value} from Python!",
}
# Create handler for AWS Lambda with specific configurationhandler = Mangum(app, api_gateway_base_path=None, lifespan="off")View the full example.