使用.zip 文件存 TypeScript 档在 Lambda 中部署已转换的代码 - Amazon Lambda
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用.zip 文件存 TypeScript 档在 Lambda 中部署已转换的代码

在将 TypeScript 代码部署到之前Amazon Lambda,需要将其 JavaScript转换为。本页介绍使用.zip 文件存档构建 TypeScript 代码并将其部署到 Lambda 的三种方法:

Amazon SAM并Amazon CDK简化 TypeScript 功能的构建和部署。Amazon SAM 模板规范提供了一种简单而干净的语法,用于描述构成无服务器应用程序的 Lambda 函数、API、权限、配置和事件。Amazon CDK 使您能够借助编程语言的强大表达能力,在云中构建可靠、可扩展且成本高效的应用程序。Amazon CDK 适合中度到高度经验的 Amazon 用户。Amazon CDK和都Amazon SAM使用 esbuild 将 TypeScript 代码转换成。 JavaScript

使用Amazon SAM将 TypeScript 代码部署到 Lambda

按照以下步骤使用下载、构建和部署示例 Hello World TypeScript 应用程序Amazon SAM。此应用程序实现了基本的 API 后端。它由 Amazon API Gateway 端点和 Lambda 函数组成。在向 API Gateway 端点发送 GET 请求时,会调用 Lambda 函数。该函数将返回一条 hello world 消息。

注意

Amazon SAM使用 esbuild 根据代码创建 Node.js Lambda 函数 TypeScript 。esbuild 支持目前处于公开预览阶段。在公开预览期间,esbuild 支持可能会发生向后不兼容的变更。

先决条件

要完成本节中的步骤,您必须满足以下条件:

部署示例 Amazon SAM 应用程序
  1. 使用 Hello World TypeScript 模板初始化应用程序。

    sam init --app-template hello-world-typescript --name sam-app --package-type Zip --runtime nodejs18.x
  2. (可选)该示例应用程序包括常用工具的配置,例如用于代码检查的 esLlint 和用于单元测试的 Jest。要运行检查和测试命令,请执行以下操作:

    cd sam-app/hello-world npm install npm run lint npm run test
  3. 构建应用程序。

    cd sam-app sam build
  4. 部署应用程序。

    sam deploy --guided
  5. 按照屏幕上的提示操作。要接受交互式体验中提供的原定设置选项,请通过 Enter 进行响应。

  6. 输出将显示 REST API 的端点。在浏览器中打开该端点,以测试函数。您应该会看到以下响应:

    {"message":"hello world"}
  7. 这是一个可以通过互联网访问的公有 API 端点。我们建议您在测试后删除该端点。

    sam delete

使用将 TypeScript 代码部署Amazon CDK到 Lambda

按照以下步骤使用构建和部署示例 TypeScript 应用程序Amazon CDK。此应用程序实现了基本的 API 后端。它由 API Gateway 端点和 Lambda 函数组成。在向 API Gateway 端点发送 GET 请求时,会调用 Lambda 函数。该函数将返回一条 hello world 消息。

先决条件

要完成本节中的步骤,您必须满足以下条件:

部署示例 Amazon CDK 应用程序
  1. 为您的新应用程序创建一个项目目录。

    mkdir hello-world cd hello-world
  2. 初始化该应用程序。

    cdk init app --language typescript
  3. 添加 @types/aws-lambda 软件包作为开发依赖项。此程序包包含 Lambda 的类型定义。

    npm install -D @types/aws-lambda
  4. 打开 lib 目录。你应该会看到一个名为 hello-world-stack.ts 的文件。在此目录中创建两个新文件:hello-world.function.tshello-world.ts

  5. 打开 hello-world.function.ts,然后将以下代码添加到该文件。这是适用于 Lambda 函数的代码。

    注意

    import 语句从 @types/aws-lambda 中导入类型定义。它不导入 aws-lambda NPM 程序包,这是一个无关的第三方工具。有关更多信息,请参阅存储库中的 aws-lambda。 DefinitelyTyped GitHub

    import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda'; export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => { console.log(`Event: ${JSON.stringify(event, null, 2)}`); console.log(`Context: ${JSON.stringify(context, null, 2)}`); return { statusCode: 200, body: JSON.stringify({ message: 'hello world', }), }; };
  6. 打开 hello-world.ts,然后将以下代码添加到该文件。它包含创NodejsFunction 建 Lambda 函数的构造和创建 REST API 的LambdaRestApi 构造

    import { Construct } from 'constructs'; import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; import { LambdaRestApi } from 'aws-cdk-lib/aws-apigateway'; export class HelloWorld extends Construct { constructor(scope: Construct, id: string) { super(scope, id); const helloFunction = new NodejsFunction(this, 'function'); new LambdaRestApi(this, 'apigw', { handler: helloFunction, }); } }

    NodejsFunction 构造默认假设以下内容:

    • 您的函数处理程序称为 handler

    • 包含函数代码的 .ts 文件 (hello-world.function.ts) 与包含构造的 .ts 文件 (hello-world.ts) 位于相同的目录中。该构造使用构造的 ID(“hello-world”)和 Lambda 处理程序文件的名称(“function”)来查找函数代码。例如,如果您的函数代码位于名为 hello-world.my-function.ts 的文件中,则 hello-world.ts 文件必须按如下所示引用该函数代码:

      const helloFunction = new NodejsFunction(this, 'my-function');

    您可以更改此行为并配置其他 esbuild 参数。有关更多信息,请参阅 Amazon CDK API 参考中的配置 esbuild

  7. 打开 hello-world-stack.ts. 这是定义您的 Amazon CDK 堆栈的代码。使用以下代码替换该代码:

    import { Stack, StackProps } from 'aws-cdk-lib'; import { Construct } from 'constructs'; import { HelloWorld } from './hello-world'; export class HelloWorldStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); new HelloWorld(this, 'hello-world'); } }
  8. 从包含您的 cdk.json 文件的 hello-world 目录中,部署您的应用程序。

    cdk deploy
  9. Amazon CDK 使用 esbuild 构建和打包 Lambda 函数,然后将该函数部署到 Lambda 运行时。输出将显示 REST API 的端点。在浏览器中打开该端点,以测试函数。您应该会看到以下响应:

    {"message":"hello world"}

    这是一个可以通过互联网访问的公有 API 端点。我们建议您在测试后删除该端点。

使用Amazon CLI和 esbuild 将 TypeScript 代码部署到 Lambda

以下示例演示了如何使用 esbuild 将 TypeScript 代码转换和部署到 Lambda,.esbuild 会生成 JavaScript 一个包含所有依赖Amazon CLI项的文件。这是您需要添加到 .zip 归档中的唯一文件。

先决条件

要完成本节中的步骤,您必须满足以下条件:

部署示例函数
  1. 在本地计算机上,为新函数创建项目目录。

  2. 使用 npm 或您选择的软件包管理器创建一个新的 Node.js 项目。

    npm init
  3. 添加 @types/aws-lambdaesbuild 软件包作为开发依赖项。@types/aws-lambda 程序包包含 Lambda 的类型定义。

    npm install -D @types/aws-lambda esbuild
  4. 创建名为 index.ts 的新文件。将以下代码添加到该新文件中。这是适用于 Lambda 函数的代码。该函数将返回一条 hello world 消息。该函数不会创建任何 API Gateway 资源。

    注意

    import 语句从 @types/aws-lambda 中导入类型定义。它不导入 aws-lambda NPM 程序包,这是一个无关的第三方工具。有关更多信息,请参阅存储库中的 aws-lambda。 DefinitelyTyped GitHub

    import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda'; export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => { console.log(`Event: ${JSON.stringify(event, null, 2)}`); console.log(`Context: ${JSON.stringify(context, null, 2)}`); return { statusCode: 200, body: JSON.stringify({ message: 'hello world', }), }; };
  5. 将构建脚本添加到 package.json 文件。这会将 esbuild 配置为自动创建 .zip 部署软件包。有关更多信息,请参阅 esbuild 文档中的构建脚本

    Linux and MacOS
    "scripts": { "prebuild": "rm -rf dist", "build": "esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js", "postbuild": "cd dist && zip -r index.zip index.js*" },
    Windows

    在此示例中,"postbuild" 命令使用 7zip 实用程序创建您的 .zip 文件。使用您自己首选的 Windows zip 实用程序并根据需要修改该命令。

    "scripts": { "prebuild": "del /q dist", "build": "esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js", "postbuild": "cd dist && 7z a -tzip index.zip index.js*" },
  6. 构建软件包。

    npm run build
  7. 使用该 .zip 部署软件包创建 Lambda 函数。将突出显示的文本替换为您的执行角色的 Amazon 资源名称(ARN)。

    aws lambda create-function --function-name hello-world --runtime "nodejs18.x" --role arn:aws:iam::123456789012:role/lambda-ex --zip-file "fileb://dist/index.zip" --handler index.handler
  8. 运行测试事件,以确认该函数会返回以下响应。如果您想使用 API Gateway 调用此函数,请创建和配置 REST API

    { "statusCode": 200, "body": "{\"message\":\"hello world\"}" }