AWS Lambda + API Gateway Adapter
AWS Lambda adapter
The AWS Lambda adapter is supported for API Gateway REST API(v1) and HTTP API(v2), and Lambda Function URL use cases.
httpBatchLinkrequires the router to work on a single API Gateway Resource (as shown in the example). If you'd like to have a Resource per procedure, you can use thehttpLinkinstead (more info).
Example apps
| Description | Links |
|---|---|
| API Gateway with NodeJS client. | |
| API Gateway REST API with response streaming. |
How to add tRPC
1. Install deps
bashyarn add @trpc/server
bashyarn add @trpc/server
2. Create a tRPC router
Implement your tRPC router. A sample router is given below:
server.tstsimport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();constappRouter =t .router ({getUser :t .procedure .input (z .string ()).query ((opts ) => {opts .input ; // stringreturn {id :opts .input ,name : 'Bilbo' };}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
server.tstsimport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();constappRouter =t .router ({getUser :t .procedure .input (z .string ()).query ((opts ) => {opts .input ; // stringreturn {id :opts .input ,name : 'Bilbo' };}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
3. Use the Amazon API Gateway adapter
tRPC includes an adapter for API Gateway out of the box. This adapter lets you run your routes through the API Gateway handler.
server.tstsimport type {APIGatewayProxyEventV2 } from 'aws-lambda';import type {CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import {awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';import {appRouter } from './router';// created for each requestconstcreateContext = ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEventV2 >) => ({}); // no contexttypeContext =Awaited <ReturnType <typeofcreateContext >>;export consthandler =awsLambdaRequestHandler ({router :appRouter ,createContext ,})
server.tstsimport type {APIGatewayProxyEventV2 } from 'aws-lambda';import type {CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import {awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';import {appRouter } from './router';// created for each requestconstcreateContext = ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEventV2 >) => ({}); // no contexttypeContext =Awaited <ReturnType <typeofcreateContext >>;export consthandler =awsLambdaRequestHandler ({router :appRouter ,createContext ,})
Build & deploy your code, now use your API Gateway URL to call your function.
| Endpoint | HTTP URI |
|---|---|
getUser | GET https://<execution-api-link>/getUser?input=INPUT where INPUT is a URI-encoded JSON string. |
A word about payload format version
API Gateway has two different event data formats when it invokes a Lambda. For REST APIs they should be version "1.0"(APIGatewayProxyEvent), but you can choose which for HTTP APIs by stating either version "1.0" or "2.0".
- Version 1.0:
APIGatewayProxyEvent - Version 2.0:
APIGatewayProxyEventV2
To infer what version you might have, supply the context as following:
tsfunctioncreateContext ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEvent >) {// ...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
tsfunctioncreateContext ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEvent >) {// ...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
Read more here about payload format version
AWS Lambda Response Streaming Adapter
AWS Lambda supports streaming responses to clients with both Lambda Function URLs and API Gateway REST APIs.
Response streaming is supported for Lambda Function URLs and API Gateway REST APIs. For API Gateway REST APIs, you need to configure the integration with
responseTransferMode: STREAM. Read more about Lambda response streaming and API Gateway response streaming.
Response Streaming
The signature of a streaming handler is different from the default handler. The streaming handler additionally receives a writable stream parameter, responseStream, besides the default node handler parameters, event and context. To indicate that Lambda should stream your responses, you must wrap your function handler with the awslambda.streamifyResponse() decorator.
Note that the
awslambdanamespace is automatically provided by the Lambda execution environment. You can import the types from@types/aws-lambdato augment the global namespace with theawslambdanamespace.
server.tsts/// <reference types="aws-lambda" />import type {APIGatewayProxyEventV2 } from 'aws-lambda';import type {CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import {awsLambdaStreamingRequestHandler } from '@trpc/server/adapters/aws-lambda';import {appRouter } from './router';// created for each requestconstcreateContext = ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEventV2 >) => ({// your context});typeContext =Awaited <ReturnType <typeofcreateContext >>;export consthandler =awslambda .streamifyResponse (awsLambdaStreamingRequestHandler ({router :appRouter ,createContext ,}),);
server.tsts/// <reference types="aws-lambda" />import type {APIGatewayProxyEventV2 } from 'aws-lambda';import type {CreateAWSLambdaContextOptions } from '@trpc/server/adapters/aws-lambda';import {awsLambdaStreamingRequestHandler } from '@trpc/server/adapters/aws-lambda';import {appRouter } from './router';// created for each requestconstcreateContext = ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEventV2 >) => ({// your context});typeContext =Awaited <ReturnType <typeofcreateContext >>;export consthandler =awslambda .streamifyResponse (awsLambdaStreamingRequestHandler ({router :appRouter ,createContext ,}),);