AWS Lambda
toLambdaHandler for API Gateway, ALB, function URLs and Lambda@Edge, with response streaming.
On this page
@trpc12/trpc/aws-lambda turns a fetch handler into a Lambda handler for API Gateway REST APIs (payload 1.0), HTTP APIs and function URLs (payload 2.0), Application Load Balancers and Lambda@Edge. The event becomes a web Request and the response becomes that event's result shape. { event, context } is the platform argument:
import { function toLambdaHandler(handler: LambdaFetchHandler, options?: LambdaHandlerOptions): (event: LambdaEvent, context?: LambdaContext) => Promise<LambdaResult>toLambdaHandler, type LambdaPlatform } from '@trpc12/trpc/aws-lambda';
const const handler: FetchHandler<LambdaPlatform>handler = createFetchHandler<{
health: BuildProcedure<RootDef<{
requestId: string | undefined;
}, {}>, "query", {
output: string;
errors: never;
services: never;
unmapped: never;
}>;
}, LambdaPlatform>(opts: FetchHandlerOptions<{
health: BuildProcedure<RootDef<{
requestId: string | undefined;
}, {}>, "query", {
output: string;
errors: never;
services: never;
unmapped: never;
}>;
}, LambdaPlatform>): FetchHandler<LambdaPlatform>
createFetchHandler<type AppRouter = {
health: BuildProcedure<RootDef<{
requestId: string | undefined;
}, {}>, "query", {
output: string;
errors: never;
services: never;
unmapped: never;
}>;
}
AppRouter, LambdaPlatform>({
BaseHandlerOptions<{ health: BuildProcedure<RootDef<{ requestId: string | undefined; }, {}>, "query", { output: string; errors: never; services: never; unmapped: never; }>; }>.router: {
health: BuildProcedure<RootDef<{
requestId: string | undefined;
}, {}>, "query", {
output: string;
errors: never;
services: never;
unmapped: never;
}>;
}
router,
createContext: (opts: CreateContextOpts<LambdaPlatform>) => MaybePromise<{
requestId: string | undefined;
}>
createContext: (opts: CreateContextOpts<LambdaPlatform>opts) => ({
requestId: string | undefinedrequestId: opts: CreateContextOpts<LambdaPlatform>opts.CreateContextOpts<LambdaPlatform>.platform: LambdaPlatformplatform.LambdaPlatform.context: LambdaContextcontext.LambdaContext.awsRequestId?: string | undefinedawsRequestId,
}),
});
export const const main: (event: LambdaEvent, context?: LambdaContext) => Promise<LambdaResult>main = function toLambdaHandler(handler: LambdaFetchHandler, options?: LambdaHandlerOptions): (event: LambdaEvent, context?: LambdaContext) => Promise<LambdaResult>toLambdaHandler(const handler: FetchHandler<LambdaPlatform>handler);- Text bodies (JSON, JSONL, forms,
text/*) stay text. Others, such as aFileoutput, are base64 withisBase64Encoded. - Set-cookie headers go where each event type expects them:
multiValueHeaders(REST, ALB),cookies(HTTP API) or CloudFront headers (Edge). - The call's
signalabortstimeoutMarginMs(default 1000) before Lambda's deadline (context.getRemainingTimeInMillis()), so a slow call ends with an error response instead of a Lambda timeout.
Response streaming
A buffered result only arrives once the response ends, so subscriptions and streamed outputs reach the client all at once. With response streaming (function URLs with InvokeMode: RESPONSE_STREAM, or API Gateway REST streaming), toLambdaStreamingHandler writes the status and headers first and then the body as it is produced:
import { function toLambdaStreamingHandler(handler: LambdaFetchHandler, options?: LambdaHandlerOptions): (event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>toLambdaStreamingHandler } from '@trpc12/trpc/aws-lambda';
export const const main: (event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>main = awslambda.function awslambda.streamifyResponse<(event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>>(handler: (event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>): (event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>streamifyResponse(
function toLambdaStreamingHandler(handler: LambdaFetchHandler, options?: LambdaHandlerOptions): (event: LambdaEvent, stream: LambdaResponseStream, context?: LambdaContext) => Promise<void>toLambdaStreamingHandler(const handler: FetchHandler<undefined>handler),
);