Skip to main content
Version: 10.x

AWS Lambda + API 网关适配器

AWS Lambda 适配器

¥AWS Lambda adapter

API Gateway Rest API(v1) 和 HTTP API(v2) 使用案例支持 AWS Lambda 适配器。

¥The AWS Lambda adapter is supported for API Gateway Rest API(v1) and HTTP API(v2) use cases.

示例应用

¥Example app

描述链接
带有 NodeJS 客户端的 API 网关。

如何添加 tRPC

¥How to add tRPC

1. 安装依赖

¥ Install deps

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

2. 创建 tRPC 路由

¥ Create a tRPC router

实现你的 tRPC 路由。下面给出了一个示例路由:

¥Implement your tRPC router. A sample router is given below:

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. 使用 Amazon API Gateway 适配器

¥ Use the Amazon API Gateway adapter

tRPC 包含一个开箱即用的 API 网关适配器。该适配器允许你通过 API 网关处理程序运行路由。

¥tRPC includes an adapter for API Gateway out of the box. This adapter lets you run your routes through the API Gateway handler.

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,
})

构建并部署你的代码,现在使用你的 API 网关 URL 来调用你的函数。

¥Build & deploy your code, now use your API Gateway URL to call your function.

端点HTTP URI
getUserGET https://<execution-api-link>/getUser?input=INPUT

其中 INPUT 是 URI 编码的 JSON 字符串。

关于有效负载格式版本的说明

¥A word about payload format version

API Gateway 在调用 Lambda 时有两种不同的事件数据格式。对于 REST API,它们应该是版本 "1.0"(APIGatewayProxyEvent),但你可以通过声明版本 "1.0" 或 "2.0" 来选择对于 HTTP API 的版本。

¥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".

  • 1.0 版本:APIGatewayProxyEvent

    ¥Version 1.0: APIGatewayProxyEvent

  • 2.0 版本:APIGatewayProxyEventV2

    ¥Version 2.0: APIGatewayProxyEventV2

要推断你可能拥有的版本,请提供以下上下文:

¥To infer what version you might have, supply the context as following:

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

在此处阅读有关有效负载格式版本的更多信息

¥Read more here about payload format version