AWS Lambda + API Gateway 适配器
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
AWS Lambda 适配器
该适配器支持 API Gateway REST API(v1)、HTTP API(v2) 和 Lambda Function URL 使用场景。
httpBatchLink要求路由器在单个 API Gateway 资源上运行(如示例所示)。 若需为每个过程创建独立资源,可使用httpLink(更多信息)。
示例应用
| Description | Links |
|---|---|
| API Gateway with NodeJS client. | |
| API Gateway REST API with response streaming. |
如何添加tRPC
1. 安装依赖
bashyarn add @trpc/server
bashyarn add @trpc/server
若您使用 AI 编程代理,请安装 tRPC 技能以优化代码生成效果:
bashnpx @tanstack/intent@latest install
bashnpx @tanstack/intent@latest install
2. 创建 tRPC 路由
实现你的 tRPC 路由。下方提供示例路由:
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. 使用Amazon API网关适配器
tRPC开箱即用地包含了一个API网关适配器。此适配器允许你通过API网关处理程序运行你的路由。
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 ,})
构建并部署你的代码,现在使用你的API网关URL来调用你的函数。
| Endpoint | HTTP URI |
|---|---|
getUser | GET https://<execution-api-link>/getUser?input=INPUT where INPUT is a URI-encoded JSON string. |
关于负载格式版本的说明
API Gateway 在调用 Lambda 时有两种不同的事件数据格式。REST API 应使用 "1.0" 版本(APIGatewayProxyEvent),而 HTTP API 可选择声明 "1.0" 或 "2.0" 版本。
-
版本1.0:
APIGatewayProxyEvent -
版本2.0:
APIGatewayProxyEventV2
要推断你可能使用的版本,请按如下方式提供上下文:
tsfunctioncreateContext ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEvent >) {// ...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
tsfunctioncreateContext ({event ,context ,}:CreateAWSLambdaContextOptions <APIGatewayProxyEvent >) {// ...}// CreateAWSLambdaContextOptions<APIGatewayProxyEvent> or CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>
AWS Lambda 响应流适配器
AWS Lambda 支持通过 Lambda Function URL 和 API Gateway REST API 向客户端流式传输响应。
响应流式传输支持 Lambda Function URL 和 API Gateway REST API。对于 API Gateway REST API,需配置集成设置
responseTransferMode: STREAM。阅读有关 Lambda 响应流式传输的更多信息以及API Gateway 响应流式传输。
响应流
流式处理函数的签名与默认处理函数不同。除默认的节点处理函数参数 event 和 context 外,流式处理函数还额外接收一个可写流参数 responseStream。要指示 Lambda 流式传输响应,必须用 awslambda.streamifyResponse() 装饰器包装你的函数处理程序。
注意:
awslambda命名空间由 Lambda 执行环境自动提供,可通过@types/aws-lambda导入类型,从而使用awslambda命名空间扩展全局命名空间。
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 ,}),);