Adaptateur AWS Lambda + API Gateway
Cette page a été traduite par PageTurner AI (bêta). Non approuvée officiellement par le projet. Vous avez trouvé une erreur ? Signaler un problème →
Adaptateur AWS Lambda
L'adaptateur AWS Lambda est pris en charge pour les cas d'utilisation de l'API Gateway : l'API REST (v1) et l'API HTTP (v2).
Exemple d'application
| Description | Links |
|---|---|
| API Gateway with NodeJS client. |
Comment ajouter tRPC
1. Installer les dépendances
bashyarn add @trpc/server
bashyarn add @trpc/server
2. Créer un routeur tRPC
Implémentez votre routeur tRPC. Un exemple de routeur est fourni ci-dessous :
server.tstsimport { initTRPC } from '@trpc/server';import { z } from 'zod';export const t = initTRPC.create();const appRouter = t.router({getUser: t.procedure.input(z.string()).query((opts) => {opts.input; // stringreturn { id: opts.input, name: 'Bilbo' };}),});// export type definition of APIexport type AppRouter = typeof appRouter;
server.tstsimport { initTRPC } from '@trpc/server';import { z } from 'zod';export const t = initTRPC.create();const appRouter = t.router({getUser: t.procedure.input(z.string()).query((opts) => {opts.input; // stringreturn { id: opts.input, name: 'Bilbo' };}),});// export type definition of APIexport type AppRouter = typeof appRouter;
3. Utiliser l'adaptateur Amazon API Gateway
tRPC inclut un adaptateur prêt à l'emploi pour API Gateway. Cet adaptateur permet d'exécuter vos routes via le handler API Gateway.
server.tstsimport { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';const appRouter = /* ... */;// created for each requestconst createContext = ({event,context,}: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => ({}) // no contexttype Context = Awaited<ReturnType<typeof createContext>>;export const handler = awsLambdaRequestHandler({router: appRouter,createContext,})
server.tstsimport { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';const appRouter = /* ... */;// created for each requestconst createContext = ({event,context,}: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => ({}) // no contexttype Context = Awaited<ReturnType<typeof createContext>>;export const handler = awsLambdaRequestHandler({router: appRouter,createContext,})
Construisez et déployez votre code, puis utilisez l'URL de votre API Gateway pour appeler votre fonction.
| Endpoint | HTTP URI |
|---|---|
getUser | GET https://<execution-api-link>/getUser?input=INPUT where INPUT is a URI-encoded JSON string. |
Note sur les versions de format de payload
API Gateway utilise deux formats différents pour les événements lors de l'invocation d'une Lambda. Pour les API REST, le format doit être la version "1.0" (APIGatewayProxyEvent), tandis que pour les API HTTP vous pouvez choisir entre les versions "1.0" ou "2.0".
-
Version 1.0 :
APIGatewayProxyEvent -
Version 2.0 :
APIGatewayProxyEventV2
Pour déterminer la version utilisée, fournissez le contexte comme suit :
tsfunction createContext({event,context,}: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) {...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
tsfunction createContext({event,context,}: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) {...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>