aws-lambda-typescript-integration 是做什麼的?
提供 AWS Lambda 與 TypeScript 的整合模式,並包含冷啟動最佳化。用於建立或部署 TypeScript Lambda 函數,選擇 NestJS 框架或原生 TypeScript 方法,最佳化冷啟動,設定 API Gateway 或 ALB 整合,或實作無伺服器 TypeScript 應用程式。觸發詞包括 "create lambda typescript"、"deploy typescript lambda"、"nestjs lambda aws"、"raw typescript lambda"、"aws lambda typescript performance"。
功能
- NestJS 框架整合:提供完整的 NestJS 設定,包括模組、控制器、服務、以及與 Lambda 的適配器。
- 原生 TypeScript 整合:提供輕量級的 TypeScript Lambda 處理器,無需額外框架。
- 冷啟動最佳化:包括減少部署包大小、使用
node:16或更新版本、最佳化記憶體設定、以及使用aws-lambda-fastify或@vendia/serverless-express等工具。 - API Gateway 與 ALB 整合:設定 HTTP API、REST API 或 ALB 作為 Lambda 觸發器,並處理請求/回應格式。
- 效能最佳化:提供程式碼分割、並行執行、以及快取策略的範例。
使用方式
此技能會根據觸發詞自動提供對應的程式碼範例和設定指南。以下是常見情境:
建立新的 TypeScript Lambda 函數
觸發詞:create lambda typescript
// 基本處理器結構
import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda';
export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello from TypeScript Lambda!' })
};
};
NestJS Lambda 設定
觸發詞:nestjs lambda aws
// main.ts
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import serverlessExpress from '@codegenie/serverless-express';
import { AppModule } from './app.module';
let cachedServer: any;
export const handler = async (event: any, context: any) => {
if (!cachedServer) {
const expressApp = require('express')();
const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp));
await app.init();
cachedServer = serverlessExpress({ app: expressApp });
}
return cachedServer(event, context);
};
冷啟動最佳化
觸發詞:aws lambda typescript performance
- 使用
esbuild做為打包工具以縮小部署包。 - 在
tsconfig.json中設定 `