Skip to content

tRPC v12 isn’t on npm yet. The @trpc12/* packages on this site can’t be installed until it’s released. Use v11 today

tRPC

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: Procedure<{
        type: "query";
        ctx: {
            requestId: string | undefined;
        };
        meta: {};
        input: undefined;
        output: string;
        errors: never;
        services: never;
    }>;
}, LambdaPlatform>(opts: FetchHandlerOptions<{
    health: Procedure<{
        type: "query";
        ctx: {
            requestId: string | undefined;
        };
        meta: {};
        input: undefined;
        output: string;
        errors: never;
        services: never;
    }>;
}, LambdaPlatform>): FetchHandler<LambdaPlatform>
createFetchHandler
<
type AppRouter = {
    health: Procedure<{
        type: "query";
        ctx: {
            requestId: string | undefined;
        };
        meta: {};
        input: undefined;
        output: string;
        errors: never;
        services: never;
    }>;
}
AppRouter
, LambdaPlatform>({
BaseHandlerOptions<{ health: Procedure<{ type: "query"; ctx: { requestId: string | undefined; }; meta: {}; input: undefined; output: string; errors: never; services: never; }>; }>.router: {
    health: Procedure<{
        type: "query";
        ctx: {
            requestId: string | undefined;
        };
        meta: {};
        input: undefined;
        output: string;
        errors: never;
        services: 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 a File output, are base64 with isBase64Encoded.
  • Set-cookie headers go where each event type expects them: multiValueHeaders (REST, ALB), cookies (HTTP API) or CloudFront headers (Edge).
  • The call's signal aborts timeoutMarginMs (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),
);