Aller au contenu principal
Version : 10.x

Adaptateur AWS Lambda + API Gateway

Traduction Bêta Non Officielle

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

DescriptionLinks
API Gateway with NodeJS client.

Comment ajouter tRPC

1. Installer les dépendances

bash
yarn add @trpc/server
bash
yarn add @trpc/server

2. Créer un routeur tRPC

Implémentez votre routeur tRPC. Un exemple de routeur est fourni ci-dessous :

server.ts
ts
import { 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; // string
return { id: opts.input, name: 'Bilbo' };
}),
});
// export type definition of API
export type AppRouter = typeof appRouter;
server.ts
ts
import { 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; // string
return { id: opts.input, name: 'Bilbo' };
}),
});
// export type definition of API
export 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.ts
ts
import { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';
const appRouter = /* ... */;
// created for each request
const createContext = ({
event,
context,
}: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => ({}) // no context
type Context = Awaited<ReturnType<typeof createContext>>;
export const handler = awsLambdaRequestHandler({
router: appRouter,
createContext,
})
server.ts
ts
import { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda';
const appRouter = /* ... */;
// created for each request
const createContext = ({
event,
context,
}: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => ({}) // no context
type 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.

EndpointHTTP URI
getUserGET 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 :

ts
function createContext({
event,
context,
}: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) {
...
}
// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
ts
function createContext({
event,
context,
}: CreateAWSLambdaContextOptions<APIGatewayProxyEvent>) {
...
}
// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>

En savoir plus sur les versions de format de payload