

# 什么是 Amazon API Gateway？
<a name="welcome"></a>

Amazon API Gateway 是一项AWS服务，用于创建、发布、维护、监控和保护任意规模的 REST、HTTP 和 WebSocket API。API 开发人员可以创建能够访问 AWS 或其他 Web 服务以及存储在 [AWS 云](https://aws.amazon.com/what-is-cloud-computing/)中的数据的 API。作为 API Gateway API 开发人员，您可以创建 API 以在您自己的客户端应用程序中使用。或者，您可以将您的 API 提供给第三方应用程序开发人员。有关更多信息，请参阅 [谁使用 API Gateway？](api-gateway-overview-developer-experience.md#apigateway-who-uses-api-gateway)。

API Gateway 创建符合下列条件的 RESTful API：
+ 基于 HTTP 的。
+ 启用无状态客户端-服务器通信。
+ 实施标准 HTTP 方法例，如 GET、POST、PUT、PATCH 和 DELETE。

有关 API Gateway REST API 和 HTTP API 的更多信息，请参阅 [在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)、[API Gateway HTTP API](http-api.md)、[使用 API Gateway 创建 REST API](api-gateway-overview-developer-experience.md#api-gateway-overview-rest) 和 [开发 API Gateway 中的 REST API](rest-api-develop.md)。

API Gateway 创建以下 WebSocket API：
+ 遵守 [WebSocket](https://datatracker.ietf.org/doc/html/rfc6455) 协议，从而支持客户端和服务器之间的有状态的全双工通信。
+ 基于消息内容路由传入的消息。

有关 API Gateway WebSocket API 的更多信息，请参阅 [使用 API Gateway 创建 WebSocket API](api-gateway-overview-developer-experience.md#api-gateway-overview-websocket)和 [API Gateway 中的 WebSocket API 概览](apigateway-websocket-api-overview.md)。

**Topics**
+ [API Gateway 的架构](#api-gateway-overview-aws-backbone)
+ [API Gateway 的功能](#api-gateway-overview-features)
+ [API Gateway 使用案例](api-gateway-overview-developer-experience.md)
+ [访问 API Gateway](#introduction-accessing-apigateway)
+ [AWS 无服务器基础设施的一部分](#api-gateway-overview-a-serverless-pillar)
+ [了解如何开始使用 Amazon API Gateway](#welcome-how-to-get-started)
+ [Amazon API Gateway 概念](api-gateway-basic-concept.md)
+ [在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)
+ [开始使用 REST API 控制台](getting-started-rest-new-console.md)

## API Gateway 的架构
<a name="api-gateway-overview-aws-backbone"></a>

下图显示 API Gateway 架构。

![\[API Gateway 架构图\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/Product-Page-Diagram_Amazon-API-Gateway-How-Works.png)


此图表说明您在 Amazon API Gateway 中构建的 API 如何为您或开发人员客户提供用于构建AWS无服务器应用程序的集成且一致的开发人员体验。API Gateway 将处理涉及接受和处理多达几十万个并发 API 调用的所有任务。这些任务包括流量管理、授权和访问控制、监控以及 API 版本管理。

API Gateway 充当应用程序从后端服务访问数据、业务逻辑或功能的“前门”，例如，在 Amazon Elastic Compute Cloud (Amazon EC2) 上运行的负载、在 AWS Lambda 上运行的代码、任何 Web 应用程序或实时通信应用程序。

## API Gateway 的功能
<a name="api-gateway-overview-features"></a>

Amazon API Gateway 提供如下功能：
+ 支持有状态 ([WebSocket](apigateway-websocket-api.md)) 和无状态（[HTTP](http-api.md) 和 [REST](apigateway-rest-api.md)）API。
+ 强大且灵活的[身份验证](apigateway-control-access-to-api.md)机制，如 AWS Identity and Access Management 策略、Lambda 授权方函数和 Amazon Cognito 用户池。
+ 用以安全地推出更改的[金丝雀版本部署](canary-release.md)。
+ [CloudTrail](cloudtrail.md) 记录和监控 API 使用情况和 API 更改。
+ CloudWatch 访问日志记录和执行日志记录，包括设置警报的能力。有关更多信息，请参阅[使用 Amazon CloudWatch 指标监控 REST API 执行](monitoring-cloudwatch.md) 和 [使用 CloudWatch 指标监控 WebSocket API 执行](apigateway-websocket-api-logging.md)。
+ 能够使用 CloudFormation 模板以支持创建 API 有关更多信息，请参阅 [Amazon API Gateway 资源类型参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_ApiGateway.html)和 [Amazon API Gateway V2 资源类型参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_ApiGatewayV2.html)。
+ 支持[自定义域名](how-to-custom-domains.md)。
+ 与 [AWS WAF](apigateway-control-access-aws-waf.md) 集成，以保护您的 API 免遭常见 Web 漏洞的攻击。
+ 与 [AWS X-Ray](apigateway-xray.md) 集成，以了解和分类性能延迟。

有关 API Gateway 功能版本的完整列表，请参阅[文档历史记录](history.md)。

# API Gateway 使用案例
<a name="api-gateway-overview-developer-experience"></a>

以下应用场景部分概述了不同类型的 API Gateway API，以及使用 API Gateway 的不同类型的开发人员。有关 REST API 与 HTTP API 之间的差异的更多详细信息，请参阅[在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)。

**Topics**
+ [使用 API Gateway 创建 REST API](#api-gateway-overview-rest)
+ [使用 API Gateway 创建 HTTP API](#api-gateway-overview-http)
+ [使用 API Gateway 创建 WebSocket API](#api-gateway-overview-websocket)
+ [谁使用 API Gateway？](#apigateway-who-uses-api-gateway)

## 使用 API Gateway 创建 REST API
<a name="api-gateway-overview-rest"></a>

API Gateway REST API 由资源和方法组成。资源是一种逻辑实体，应用程序可以通过资源路径来访问资源。方法与您的 API 用户提交的 REST API 请求以及返回给该用户的相应响应对应。

例如，`/incomes` 可以是代表应用程序用户收入的资源路径。一个资源可以包含一个或多个由适当的 HTTP 动词 (如 GET、POST、PUT、PATCH 和 DELETE) 定义的操作。资源路径和操作的组合构成 API 的方法。例如，`POST /incomes` 方法可以添加调用方获得的收入，`GET /expenses` 方法可以查询报告的调用方支出。

应用程序不需要知道在后端所请求数据的存储位置和提取位置。在 API Gateway REST API 中，前端由*方法请求* 和*方法响应* 封装。API 通过*集成请求*和*集成响应*与后端连接。

例如，使用 DynamoDB 作为后端，API 开发人员会设置集成请求以便将传入方法请求转发到所选的后端。该设置包括适当 DynamoDB 操作的规范、所需的 IAM 角色和策略以及所需的输入数据转换。后端将结果作为集成响应返回到 API Gateway。

要将与指定 HTTP 状态代码的适当方法响应对应的集成响应路由到客户端，您可以配置集成响应，将所需的响应参数从集成映射到方法。然后，您可以根据需要将后端的输出数据格式转换为前端的输出数据格式。API Gateway 让您能够为[负载](https://en.wikipedia.org/wiki/Payload_(computing))定义一个架构或模型，以方便设置正文映射模板。

API Gateway 提供 REST API 管理功能，如下所示：
+ 支持使用 API Gateway 对 OpenAPI 的扩展生成开发工具包和创建 API 文档
+ HTTP 请求的限制

## 使用 API Gateway 创建 HTTP API
<a name="api-gateway-overview-http"></a>

使用 HTTP API，您可以创建比 REST API 具有更低延迟和更低成本的 RESTful API。

您可以使用 HTTP API 将请求发送到 AWS Lambda 函数或任何可公开路由的 HTTP 端点。

例如，您可以创建一个与后端上的 Lambda 函数集成的 HTTP API。当客户端调用您的 API 时，API Gateway 将请求发送到 Lambda 函数并将该函数的响应返回给客户端。

HTTP API 支持 [OpenID Connect](https://openid.net/developers/how-connect-works/) 和 [OAuth 2.0](https://oauth.net/2/) 授权。它们内置了对跨域资源共享 (CORS) 和自动部署的支持。

要了解更多信息，请参阅“[在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)”。

## 使用 API Gateway 创建 WebSocket API
<a name="api-gateway-overview-websocket"></a>

在 WebSocket API 中，客户端和服务器都可以随时相互发送消息。后端服务器可以轻松地将数据推送到连接的用户和设备，从而无需实施复杂的轮询机制。

例如，您可以使用 API Gateway WebSocket API 和 AWS Lambda 来构建无服务器应用程序，以便在聊天室中向个人用户或用户组发送消息以及从个人用户或用户组接收消息。或者，您可以根据消息内容调用后端服务，例如 AWS Lambda、Amazon Kinesis 或 HTTP 端点。

您可以使用 API Gateway WebSocket API 来构建安全的实时通信应用程序，而无需预置或管理任何服务器来管理连接或大规模数据交换。目标使用案例包括实时应用程序，如下所示：
+ 聊天应用程序
+ 实时控制面板，如股票代码
+ 实时提醒和通知

API Gateway 提供 WebSocket API 管理功能，如下所示：
+ 连接和消息的监控和限制
+ 使用 AWS X-Ray 在消息经由 API 传递到后端服务时跟踪消息
+ 易于与 HTTP/HTTPS 端点集成

## 谁使用 API Gateway？
<a name="apigateway-who-uses-api-gateway"></a>

使用 API Gateway 的开发人员有两种：API 开发人员和应用程序开发人员。

API 开发人员创建和部署 API，以便启用 API Gateway 中所需的功能。API 开发人员必须是拥有 API 的 AWS 账户中的用户。

应用程序开发人员通过在 API Gateway 中调用由 API 开发人员创建的 WebSocket 或 REST API 来构建一个正常运行的应用程序以调用AWS服务。

应用程序开发人员是 API 开发人员的客户。应用程序开发人员不需要拥有 ‬AWS 账户，前提是 API 不需要 IAM 权限或通过 ‭[ Amazon Cognito 用户池联合身份‭ ](https://docs.aws.amazon.com/cognito/latest/developerguide/)所支持的第三方联合身份提供商来支持用户授权。此类身份提供商包括 Amazon、Amazon Cognito 用户池、Facebook 和 Google。

### 创建和管理 API Gateway API
<a name="api-gateway-overview-developer-experience-managing-api"></a>

API 开发人员使用名为 `apigateway` 的用于 API 管理的 API Gateway 服务组件来创建、配置和部署 API。

作为 API 开发人员，您可以按照[开始使用 API Gateway](getting-started.md)中所述使用 API Gateway 控制台或者通过调用 [API 参考](api-ref.md) 来创建和管理 API。这一 API 有若干种调用方式。这包括使用 AWS Command Line Interface (AWS CLI)，或使用 AWS 开发工具包。此外，您还可以使用 [AWS CloudFormation 模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-reference.html)或（如果是 REST API 和 HTTP API）[适用于 API Gateway 的 OpenAPI 扩展](api-gateway-swagger-extensions.md) 来支持创建 API。

有关提供 API Gateway 的区域以及相关控制服务端点的列表，请参阅 [Amazon API Gateway 端点和配额](https://docs.aws.amazon.com/general/latest/gr/apigateway.html)。

### 调用 API Gateway API
<a name="api-gateway-overview-developer-experience-invoking-api"></a>

应用程序开发人员使用名为 `execute-api` 的用于 API 执行的 API Gateway 服务组件来调用在 API Gateway 中创建或部署的 API。底层编程实体由创建的 API 公开。此类 API 有若干种调用方式。要了解更多信息，请参阅[调用 API Gateway 中的 REST API](how-to-call-api.md)和[调用 WebSocket API](apigateway-how-to-call-websocket-api.md)。

## 访问 API Gateway
<a name="introduction-accessing-apigateway"></a>

您可以通过以下方式访问 Amazon API Gateway：
+ **AWS 管理控制台** – AWS 管理控制台提供用于创建和管理 API 的 Web 界面。完成[设置为使用 API Gateway](setting-up.md)中的步骤后，您可以访问 API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。
+ **AWS开发工具包** – 如果您使用AWS为其提供开发工具包的编程语言，可以使用开发工具包访问 API Gateway。开发工具包可简化身份验证、与您的开发环境轻松集成，并有助于访问 API Gateway 命令。有关更多信息，请参阅[用于 Amazon Web Services 的工具](https://aws.amazon.com/developer/tools/)。
+ **API Gateway V1 和 V2 API** – 如果您使用的是开发工具包不适用的编程语言，请参阅 [Amazon API Gateway 版本 1 API 参考](https://docs.aws.amazon.com/apigateway/latest/api/API_Operations.html)和 [Amazon API Gateway 版本 2 API 参考](https://docs.aws.amazon.com/apigatewayv2/latest/api-reference/api-reference.html)。
+ **AWS Command Line Interface** – 有关更多信息，请参阅 *AWS Command Line Interface 用户指南*中的[开始设置 AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/)。
+ **AWS Tools for Windows PowerShell** – 有关更多信息，请参阅 *AWS Tools for PowerShell 用户指南*中的[设置 AWS Tools for Windows PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/)。

## AWS 无服务器基础设施的一部分
<a name="api-gateway-overview-a-serverless-pillar"></a>

API Gateway 与 [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/) 共同构成AWS无服务器基础设施中面向应用程序的部分。要了解有关无服务器入门的更多信息，请参阅[无服务器开发人员指南](https://docs.aws.amazon.com/serverless/latest/devguide/welcome.html)。

对于调用公开可用的AWS服务的应用程序，您可以使用 Lambda 与所需的服务进行交互，或在 API Gateway 中通过 API 方法公开 Lambda 函数。AWS Lambda 在高度可用的计算基础设施上运行您的代码。它会进行必要的计算资源执行和管理工作。为了启用无服务器应用程序，API Gateway 支持与 AWS Lambda 以及 HTTP 端点之间的[简化代理集成](api-gateway-set-up-simple-proxy.md)。

## 了解如何开始使用 Amazon API Gateway
<a name="welcome-how-to-get-started"></a>

有关 Amazon API Gateway 的简介，请参阅以下内容：
+ [开始使用 API Gateway](getting-started.md)，提供了创建 HTTP API 的演练步骤。
+ [无服务器 land](https://serverlessland.com/video?tag=Amazon%20API%20Gateway)，提供了教学视频。
+ [快乐的小 API 短视频](https://www.youtube.com/playlist?list=PLJo-rJlep0EDFw7t0-IBHffVYKcPMDXHY)，这是一系列简短的教学视频。

# Amazon API Gateway 概念
<a name="api-gateway-basic-concept"></a>

以下部分描述有关使用 API Gateway 的介绍性概念。

**API Gateway**  <a name="apigateway-definition-apigateway"></a>
API Gateway 是一项AWS服务，支持以下操作：  
+ 创建、部署和管理 [RESTful](https://en.wikipedia.org/wiki/Representational_state_transfer) 应用程序编程接口 (API) 以便公开后端 HTTP 终端节点、AWS Lambda 函数或其他 AWS 服务。
+ 创建、部署和管理 [WebSocket](https://datatracker.ietf.org/doc/html/rfc6455) API 以公开 AWS Lambda 函数或其他AWS服务。
+ 通过前端 HTTP 和 WebSocket 终端节点调用公开的 API 方法。

**API Gateway REST API**  <a name="apigateway-definition-apigateway-api"></a>
与后端 HTTP 终端节点、Lambda 函数或其他AWS服务集成的 HTTP 资源与方法的集合。您可以在一个或多个阶段部署中此集合。通常情况下，根据应用程序逻辑将 API 资源组织成资源树形式。每个 API 资源均可公开一个或多个 API 方法，这些方法具有受 API Gateway 支持的唯一 HTTP 命令动词。有关更多信息，请参阅 [在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)。

**API Gateway HTTP API**  <a name="apigateway-definition-http-api"></a>
与后端 HTTP 终端节点或 Lambda 函数集成的路由和方法的集合。您可以在一个或多个阶段部署中此集合。每个路由均可公开一个或多个 API 方法，这些方法具有受 API Gateway 支持的唯一 HTTP 命令动词。有关更多信息，请参阅 [在 REST API 和 HTTP API 之间进行选择](http-api-vs-rest.md)。

**API Gateway WebSocket API**  <a name="apigateway-definition-apigateway-websocket-api"></a>
与后端 HTTP 终端节点、Lambda 函数或其他AWS服务集成的 WebSocket 路由和路由键的集合。您可以在一个或多个阶段部署中此集合。API 方法通过可以与注册的自定义域名关联的前端 WebSocket 连接进行调用。

**API 部署**  <a name="apigateway-definition-api-deployment"></a>
API Gateway API 的时间点快照。要使客户端可用，必须将部署与一个或多个 API 阶段关联。

**API 开发人员**  <a name="apigateway-definition-api-developer"></a>
拥有 API Gateway 部署的AWS账户（例如，也支持编程访问的服务提供商。）

**API 终端节点**  <a name="apigateway-definition-api-endpoints"></a>
API Gateway 中部署到特定区域的 API 的主机名。主机名的格式是 `{api-id}.execute-api.{region}.amazonaws.com`。支持以下类型的 API 终端节点：  
+ [边缘优化的 API 终端节点](#apigateway-definition-edge-optimized-api-endpoint)
+ [私有 API 终端节点](#apigateway-definition-private-api-endpoint)
+ [区域 API 终端节点](#apigateway-definition-regional-api-endpoint)

**API 密钥**  <a name="apigateway-definition-api-key"></a>
API Gateway 用于识别使用 REST 或 WebSocket API 的应用程序开发人员的字母数字字符串。API Gateway 可以代表您生成 API 密钥，也可以从 CSV 文件中导入 API 密钥。您可以将 API 密钥与 [Lambda 授权方](apigateway-use-lambda-authorizer.md)或[使用计划](api-gateway-api-usage-plans.md)一起使用来控制对 API 的访问。  
请参阅 [API 终端节点](#apigateway-definition-api-endpoints)。

**API 所有者**  <a name="apigateway-definition-api-owner"></a>
请参阅 [API 开发人员](#apigateway-definition-api-developer)。

**API 阶段**  <a name="apigateway-definition-api-stage"></a>
对您的 API 生命周期状态（例如，“dev”、“prod”、“beta”、“v2”）的逻辑引用。API 阶段由 API ID 和阶段名称标识。

**应用程序开发人员**  <a name="apigateway-definition-app-developer"></a>
应用程序创建者，可能拥有也可能不拥有 AWS 账户并与您（API 开发人员）部署的 API 交互。应用程序开发人员是您的客户。应用程序开发人员通常由 [API 密钥](#apigateway-definition-api-key)标识。

**回调 URL**  
当新客户端通过 WebSocket 连接进行连接时，您可以调用 API Gateway 中的集成来存储客户端的回调 URL。然后，您可以使用该回调 URL 从后端系统向客户端发送消息。

**开发人员门户**  <a name="apigateway-definition-developer-portal"></a>
供 API 提供者向 API 使用者共享其 API 及 API 文档的应用程序。API 被分组到产品中，产品是 REST API 端点、API 文档和补充产品文档的集合。  
请参阅 [API Gateway 门户](apigateway-portals.md)。

**边缘优化的 API 终端节点**  <a name="apigateway-definition-edge-optimized-api-endpoint"></a>
API Gateway API 的默认主机名，它部署到指定区域，并使用 CloudFront 分配以方便客户端跨AWS区域进行常规访问。API 请求将路由至最近的 CloudFront 接入点 (POP)，通常可改进不同地理位置客户端的连接时间。  
请参阅 [API 终端节点](#apigateway-definition-api-endpoints)。

**集成请求**  <a name="apigateway-definition-integration-request"></a>
API Gateway 中的 WebSocket API 路由或 REST API 方法的内部接口，在其中您会将路由请求的正文或方法请求的参数和正文映射到后端所需的格式。

**集成响应**  <a name="apigateway-definition-integration-response"></a>
API Gateway 中的 WebSocket API 路由或 REST API 方法的内部接口，在其中您会将从后端接收到的状态代码、标头和负载映射到返回到客户端应用程序的响应格式。

**映射模板**  <a name="apigateway-definition-mapping-template"></a>
使用 [Velocity 模板语言 (VTL)](https://velocity.apache.org/engine/devel/vtl-reference.html) 表示的脚本，此脚本用于将请求正文从前端数据格式转换为后端数据格式，或将响应正文从后端数据格式转换为前端数据格式。映射模板可以在集成请求中或在集成响应中指定。它们可以将运行时提供的数据引用为上下文和阶段变量。  
映射可以像[身份转换](https://en.wikipedia.org/wiki/Identity_transform)一样简单，它通过集成将标头或正文从客户端按原样传递到请求的后端。对于响应也是如此，在其中负载从后端传递到客户端。

**方法请求**  <a name="apigateway-definition-method-request"></a>
API Gateway 中 API 方法的公共接口，定义了应用程序开发人员在通过 API 访问后端时必须在请求中发送的参数和正文。

**方法响应**  <a name="apigateway-definition-method-response"></a>
REST API 的公共接口，定义应用程序开发人员期望在 API 的响应中收到的状态代码、标头和正文模型。

**模拟集成**  <a name="apigateway-definition-mock-integration"></a>
在模拟集成中，API 响应直接从 API Gateway 生成，而无需集成后端。作为 API 开发人员，您可以决定 API Gateway 响应模拟集成的方式。为此，您可以配置方法的集成请求和集成响应，以将响应与给定的状态代码相关联。

**模型**  <a name="apigateway-definition-model"></a>
指定请求或响应负载的数据结构的数据架构。生成 API 的强类型的开发工具包时需要使用模型。它还用于验证负载。模型可以方便地用于生成示例映射模板，以便开始创建生产映射模板。虽然模型很有用，但不是创建映射模板所必需的。

**门户**  <a name="apigateway-definition-portal"></a>
请参阅[开发人员门户](#apigateway-definition-developer-portal)。

**门户产品**  <a name="apigateway-definition-product"></a>
您想要共享的服务或功能。门户产品是产品 REST 端点和产品页面的集合。产品 REST 端点是门户产品的接入点，它们由 REST API 的路径和方法以及 REST API 部署到的阶段组成。产品页面是您提供的文档，用于向 API 使用者说明如何使用您的产品端点。您可以跨 AWS 账户共享产品以将其添加到门户。  
请参阅[创建产品](apigateway-portals-create-portal-product.md)。

**私有 API**  <a name="apigateway-definition-private-api"></a>
请参阅[私有 API 终端节点](#apigateway-definition-private-api)。

**私有 API 终端节点**  <a name="apigateway-definition-private-api-endpoint"></a>
一个通过接口 VPC 终端节点公开的 API 终端节点，能让客户端安全地访问 VPC 内的私有 API 资源。私有 API 与公有 Internet 隔离，只能使用已授予访问权限的 API Gateway 的 VPC 终端节点访问它们。

**私有集成**  <a name="apigateway-definition-private-integration"></a>
一种 API Gateway 集成类型，供客户端通过私有 REST API 终端节点访问客户 VPC 中的资源，而不向公有 Internet 公开资源。

**代理集成**  <a name="apigateway-definition-proxy-integration"></a>
简化的 API Gateway 集成配置。您可以将代理集成设置为 HTTP 代理集成或 Lambda 代理集成。  
对于 HTTP 代理集成，API Gateway 在前端与 HTTP 后端之间传递整个请求和响应。对于 Lambda 代理集成，API Gateway 将整个请求作为输入发送到后端 Lambda 函数。API Gateway 随后将 Lambda 函数输出转换为前端 HTTP 响应。  
在 REST API 中，代理集成最常与代理资源一起使用，以“贪婪”路径变量（例如 `{proxy+}`）与“捕获所有”`ANY` 方法相结合的方式表示。

**快速创建**  <a name="apigateway-definition-quick-create"></a>
您可以使用快速创建来简化 HTTP API 的创建。使用“快速创建”可以创建具有 Lambda 或 HTTP 集成、默认“捕获全部”路由和默认阶段（配置为自动部署更改）的 API。有关更多信息，请参阅 [使用AWS CLI 创建 HTTP API](http-api-develop.md#http-api-examples.cli.quick-create)。

**区域 API 终端节点**  <a name="apigateway-definition-regional-api-endpoint"></a>
部署到指定区域并旨在服务于同一 AWS 区域中的客户端（例如 EC2 实例）的 API 的主机名。API 请求直接以区域特定的 API Gateway API 为目标而不经过任何 CloudFront 分配。对于区域内请求，区域性终端节点会绕过到 CloudFront 分配的不必要往返行程。  
此外，您可以在区域性终端节点上应用[基于延迟的路由](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-latency)，从而使用相同的区域性 API 终端节点配置将 API 部署到多个区域，为每个已部署的 API 设置相同的自定义域名，以及在 Route 53 中配置基于延迟的 DNS 记录以将客户端请求路由到具有最低延迟的区域。  
请参阅 [API 终端节点](#apigateway-definition-api-endpoints)。

**路由**  <a name="apigateway-definition-route"></a>
API Gateway 中的 WebSocket 路由用于根据消息内容将传入消息定向到特定集成，例如 AWS Lambda 函数。在定义 WebSocket API 时，指定路由键和集成后端。路由键是消息正文中的一种属性。当路由键在传入消息中匹配时，将会调用集成后端。  
还可以为不匹配的路由键设置默认路由，或者设置默认路由来指定一个将消息按原样传递给执行路由和处理请求的后端组件的代理模型。

**路由请求**  <a name="apigateway-definition-route-request"></a>
API Gateway 中 WebSocket API 方法的公共接口，定义了应用程序开发人员在通过 API 访问后端时必须在请求中发送的正文。

**路由响应**  <a name="apigateway-definition-route-response"></a>
WebSocket API 的公共接口，定义应用程序开发人员期望从 API Gateway 收到的状态代码、标头和正文模型。

**使用计划**  <a name="apigateway-definition-usage-plan"></a>
[使用计划](api-gateway-api-usage-plans.md)可以提供能够访问一个或多个部署的 REST 或 WebSocket API 的选定 API 客户端。您可以通过使用计划来配置限制和配额限制，这些限制会应用到单独的客户端 API 密钥。

**WebSocket 连接**  
API Gateway 在客户端和 API Gateway 本身之间维护持久连接。API Gateway 和后端集成（如 Lambda 函数）之间没有持久连接。基于从客户端收到的消息内容，根据需要来调用后端服务。

# 在 REST API 和 HTTP API 之间进行选择
<a name="http-api-vs-rest"></a>

REST API 和 HTTP API 都是 RESTful API 产品。REST API 支持的功能比 HTTP API 多，而 HTTP API 在设计时功能就极少，因此能够以更低的价格提供。如果您需要如 API 密钥、每客户端节流、请求验证、AWS WAF 集成或私有 API 端点等功能，请选择 REST API。如果您不需要 REST API 中包含的功能，请选择 HTTP API。

以下各节汇总了 REST API 和 HTTP API 中可用的核心功能。必要时，还会提供其他链接，以便于在《API Gateway 开发人员指南》的 REST API 部分与 HTTP API 部分之间导航。

## 端点类型
<a name="http-api-vs-rest.differences.endpoint-type"></a>

端点类型是指 API Gateway 为 API 创建的端点。有关更多信息，请参阅 [API Gateway 中针对 REST API 的 API 端点类型](api-gateway-api-endpoint-types.md)。


| 端点类型 | REST API | HTTP API | 
| --- | --- | --- | 
|  [边缘优化](api-gateway-api-endpoint-types.md#api-gateway-api-endpoint-types-edge-optimized)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [区域性](api-gateway-api-endpoint-types.md#api-gateway-api-endpoint-types-regional)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | 
|  [ Private](api-gateway-api-endpoint-types.md#api-gateway-api-endpoint-types-private)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

## 安全性
<a name="http-api-vs-rest.differences.security"></a>

API Gateway 提供了多种方法来保护您的 API 免受某些威胁危害，例如恶意行为者或流量高峰。要了解更多信息，请参阅 [保护 API Gateway 中的 REST API](rest-api-protect.md) 和 [保护 API Gateway 中的 HTTP API](http-api-protect.md)。


| 安全功能 | REST API | HTTP API | 
| --- | --- | --- | 
|  [双向 TLS 身份验证](rest-api-mutual-tls.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](rest-api-mutual-tls.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-mutual-tls.md)  | 
|  [用于后端身份验证的证书](getting-started-client-side-ssl-authentication.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [AWS WAF](apigateway-control-access-aws-waf.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

## 授权
<a name="http-api-vs-rest.differences.authorization"></a>

API Gateway 支持多种用于控制和管理对 API 的访问的机制：有关更多信息，请参阅[在 API Gateway 中控制和管理对 REST API 的访问](apigateway-control-access-to-api.md) 和[控制和管理对 API Gateway 中的 HTTP API 的访问](http-api-access-control.md)。


| 授权选项 | REST API | HTTP API | 
| --- | --- | --- | 
|  [（IAM](permissions.md)）  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](permissions.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-access-control-iam.md)  | 
|  [资源策略](apigateway-resource-policies.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持   | 
|  [Amazon Cognito](apigateway-integrate-with-cognito.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是 1  | 
|  [使用 AWS Lambda 函数的自定义授权](apigateway-use-lambda-authorizer.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](apigateway-use-lambda-authorizer.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-lambda-authorizer.md)  | 
|  [JSON Web 令牌 (JWT)](http-api-jwt-authorizer.md) 2  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg)否  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | 

1 您可以将 Amazon Cognito 与 [JWT 授权方](http-api-jwt-authorizer.md)结合使用。

2 您可以使用 [Lambda 授权方](apigateway-use-lambda-authorizer.md)以验证适用于 REST API 的 JWT。

## API 管理
<a name="http-api-vs-rest.differences.management"></a>

如果您需要 API 管理功能（例如 API 密钥和每客户端费率限制），请选择 REST API。有关更多信息，请参阅 [在 API Gateway 中向客户端分配 REST API](rest-api-distribute.md)、[API Gateway 中公共 REST API 的自定义域名](how-to-custom-domains.md) 和 [API Gateway 中的 HTTP API 的自定义域名](http-api-custom-domain-names.md)。


| 功能 | REST API | HTTP API | 
| --- | --- | --- | 
|  [自定义域](how-to-custom-domains.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](how-to-custom-domains.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-custom-domain-names.md)  | 
|  [API 密钥](api-gateway-api-usage-plans.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [每客户端费率限制](api-gateway-request-throttling.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [每客户端使用量节流](api-gateway-api-usage-plans.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [开发人员门户](apigateway-portals.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

## 开发
<a name="http-api-vs-rest.differences.development"></a>

在开发 API Gateway API 时，您可以决定 API 的许多特征。这些特征取决于 API 的使用案例。有关更多信息，请参阅[开发 API Gateway 中的 REST API](rest-api-develop.md)和[开发 API Gateway 中的 HTTP API](http-api-develop.md)。


| 功能 | REST API | HTTP API | 
| --- | --- | --- | 
|  [CORS 配置](how-to-cors.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](how-to-cors.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-cors.md)  | 
|  [测试调用](how-to-test-method.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [缓存](api-gateway-caching.md)：  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [用户控制的部署](how-to-deploy-api.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](how-to-deploy-api.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-stages.md)  | 
|  [自动部署](http-api-stages.md)  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg)否  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | 
|  [自定义网关响应](api-gateway-gatewayResponse-definition.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [Canary 版本部署](canary-release.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [请求验证](api-gateway-method-request-validation.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [请求参数转换](rest-api-data-transformations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](rest-api-data-transformations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-parameter-mapping.md)  | 
|  [请求正文转换](rest-api-data-transformations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

## 监控
<a name="http-api-vs-rest.differences.monitoring"></a>

API Gateway 支持多种选项来记录 API 请求和监控 API。有关更多信息，请参阅[监控 API Gateway 中的 REST API](rest-api-monitor.md)和[监控 API Gateway 中的 HTTP API](http-api-monitor.md)。


| 功能 | REST API | HTTP API | 
| --- | --- | --- | 
|  [Amazon CloudWatch 指标](monitoring-cloudwatch.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](monitoring-cloudwatch.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-metrics.md)  | 
|  [访问 CloudWatch Logs 的日志](set-up-logging.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](set-up-logging.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-logging.md)  | 
|  [访问 Amazon Data Firehose 的日志](apigateway-logging-to-kinesis.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [执行日志](set-up-logging.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [AWS X-Ray 跟踪](apigateway-xray.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

## 集成
<a name="http-api-vs-rest.differences.integrations"></a>

集成将 API Gateway API 连接到后端资源。有关更多信息，请参阅[针对 API Gateway 中的 REST API 的集成](how-to-integration-settings.md)和[针对 API Gateway 中的 HTTP API 创建集成](http-api-develop-integrations.md)。


| 功能 | REST API | HTTP API | 
| --- | --- | --- | 
|  [公有 HTTP 端点](setup-http-integrations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](setup-http-integrations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-develop-integrations-http.md)  | 
|  [AWS 服务](api-gateway-api-integration-types.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](api-gateway-api-integration-types.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-develop-integrations-aws-services.md)  | 
|  [AWS Lambda 函数](set-up-lambda-integrations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](set-up-lambda-integrations.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-develop-integrations-lambda.md)  | 
|  [与网络负载均衡器的私有集成](set-up-private-integration.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](set-up-private-integration.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](http-api-develop-integrations-private.md)  | 
|  [与应用程序负载均衡器的私有集成](http-api-develop-integrations-private.md)  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) [是](set-up-private-integration.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | 
|  [与 AWS Cloud Map 的私有集成](http-api-develop-integrations-private.md)  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg)否   |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | 
|  [模拟集成](how-to-mock-integration.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 
|  [响应流式处理](response-transfer-mode.md)  |  ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/success_icon.svg) 是  | ![\[alt text not found\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/negative_icon.svg) 不支持  | 

# 开始使用 REST API 控制台
<a name="getting-started-rest-new-console"></a>

在本入门练习中，您将使用 API Gateway REST API 控制台创建无服务器 REST API。无服务器 API 让您可以专注于应用程序，而不是花时间预置和管理服务器。本次练习应在 20 分钟内完成，并且可以使用[AWS 免费套餐](https://aws.amazon.com/free/)完成。

首先，您将使用 Lambda 控制台创建 Lambda 函数。接下来，使用 API Gateway REST API 控制台创建 REST API 。然后，您将创建一个 API 方法，并使用 Lambda 代理集成将该方法与 Lambda 函数集成。最后，您将部署并调用您的 API。

当您调用 REST API 时，API Gateway 会将请求路由到您的 Lambda 函数。Lambda 运行函数并将响应返回 API Gateway。然后 API Gateway 会向您返回该响应。

![\[您在本教程中创建的 REST API 的概述。\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/getting-started-overview-rest.png)


要完成本练习，您需要一个 AWS 账户以及一位具有控制台访问权限的 AWS Identity and Access Management (IAM) 用户。有关更多信息，请参阅 [设置为使用 API Gateway](setting-up.md)。

**Topics**
+ [步骤 1：创建 Lambda 函数](#getting-started-rest-new-console-create-function)
+ [步骤 2：创建 REST API](#getting-started-rest-new-console-create-api)
+ [步骤 3：创建 Lambda 代理集成](#getting-started-rest-new-console-create-integration)
+ [步骤 4：部署您的 API](#getting-started-rest-new-console-deploy)
+ [步骤 5：调用您的 API](#getting-started-rest-new-console-invoke-api)
+ [（可选）第 6 步：清除](#getting-started-cleanup-rest)

## 步骤 1：创建 Lambda 函数
<a name="getting-started-rest-new-console-create-function"></a>

将 Lambda 函数用作您的 API 后端。只有在需要时 Lambda 才运行您的代码，并且能自动扩缩，从每天几个请求扩展到每秒数千个请求。

在本练习中，您使用 Lambda 控制台中的默认 Node.js 函数。

**创建 Lambda 函数**

1. 通过以下网址登录 Lambda 控制台：[https://console.aws.amazon.com/lambda](https://console.aws.amazon.com/lambda)。

1. 选择**创建函数**。

1. 在**基本信息** 下，对于**函数名称**，输入 **my-function**。

1. 对于所有其它选项，请使用默认设置。

1. 选择**创建函数**。

默认的 Lambda 函数代码应类似于以下内容：

```
export const handler = async (event) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify('The API Gateway REST API console is great!'),
    };
    return response;
};
```

对于本练习，您可以修改 Lambda 函数，前提是函数的响应与 [API Gateway 所要求的格式](set-up-lambda-proxy-integrations.md#api-gateway-simple-proxy-for-lambda-output-format)保持一致。

将默认响应正文 (`Hello from Lambda!`) 替换为 `The API Gateway REST API console is great!`。当您调用示例函数时，它会向客户端返回 `200` 响应以及更新的响应。

## 步骤 2：创建 REST API
<a name="getting-started-rest-new-console-create-api"></a>

接下来，您将使用根资源 (`/`) 创建一个 REST API。

**创建 REST API**

1. 通过以下网址登录到 Amazon API Gateway 控制台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 请执行以下操作之一：
   + 要创建第一个 API，对于 **REST API**，请选择**构建**。
   + 如果您之前已经创建了 API，请选择**创建 API**，然后为 **REST API** 选择**构建**。

1.  对于 **API 名称**，请输入 **my-rest-api**。

1. （可选）对于**描述**，输入描述。

1. 将 **API 端点类型**设置保留为**区域**。

1. 为 **IP 地址类型**选择 **IPv4**。

1. 选择**创建 API**。

## 步骤 3：创建 Lambda 代理集成
<a name="getting-started-rest-new-console-create-integration"></a>

接下来，您将在根资源 (`/`) 上为您的 REST API 创建 API 方法，并使用代理集成将方法与您的 Lambda 函数集成。在 Lambda 代理集成中，API Gateway 将来自客户端的传入请求直接传递给 Lambda 函数。

**创建 Lambda 代理集成**

1. 选择 `/` 资源，然后选择**创建方法**。

1. 对于**方法类型**中，选择 `ANY`。

1. 对于**集成类型**，选择 **Lambda**。

1. 打开 **Lambda 代理集成**。

1. 对于 **Lambda 函数**，输入 **my-function**，然后选择您的 Lambda 函数。

1. 选择**创建方法**。

## 步骤 4：部署您的 API
<a name="getting-started-rest-new-console-deploy"></a>

接下来，您将创建 API 部署并将其与阶段关联。

**部署 API**

1. 选择**部署 API**。

1. 对于**阶段**，选择**新建阶段**。

1. 对于**阶段名称**，输入 **Prod**。

1. （可选）对于**描述**，输入描述。

1. 选择**部署**。

现在，客户端可以调用您的 API。要在部署 API 之前对其进行测试，您可以选择 **ANY** 方法，导航到**测试**选项卡，然后选择**测试**。

## 步骤 5：调用您的 API
<a name="getting-started-rest-new-console-invoke-api"></a>

**调用 API**

1. 从主导航窗格中选择**阶段**。

1. 在**阶段详细信息**下，选择复制图标以复制您 API 的调用 URL。  
![\[创建 REST API 后，控制台会显示 API 的调用 URL。\]](http://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/images/getting-started-rest-invoke-url.png)

1. 在 Web 浏览器中输入调用 URL。

   完整的 URL 应类似于 `https://abcd123.execute-api.us-east-2.amazonaws.com/Prod`。

   您的浏览器向 API 发送 `GET` 请求。

1. 验证 API 的响应。您应该会在浏览器中看到文本 `"The API Gateway REST API console is great!"`。

## （可选）第 6 步：清除
<a name="getting-started-cleanup-rest"></a>

为避免对您的 AWS 账户产生不必要的成本，请删除您在本练习中创建的资源。以下步骤将删除您的 REST API、Lambda 函数和相关资源。

**删除 REST API**

1. 在**资源**窗格中，选择 **API 操作**，然后选择**删除 API**。

1. 在**删除 API** 对话框中，输入**确认**，然后选择**删除**。

**删除 Lambda 函数**

1. 通过以下网址登录 Lambda 控制台：[https://console.aws.amazon.com/lambda](https://console.aws.amazon.com/lambda)。

1. 在**函数**页面上，选择您的函数。依次选择**操作**、**删除**。

1. 在**删除 1 函数**对话框中，输入 **delete**，然后选择**删除**。

**删除 Lambda 函数的日志组**

1. 在 Amazon CloudWatch 控制台中，打开[日志组](https://console.aws.amazon.com/cloudwatch/home#logs:)页面。

1. 在**日志组**页面上，选择函数的日志组 (`/aws/lambda/my-function`)。然后，对于**操作**，选择**删除日志组**。

1. 在 **Delete log group(s)**（删除日志组）对话框中，选择 **Delete**（删除）。

**删除 Lambda 函数的执行角色**

1. 打开 IAM 控制台的[角色页面](https://console.aws.amazon.com/iam/home?#/roles)。

1. （可选）在**角色**页面的搜索框中，输入 **my-function**。

1. 选择函数的角色（例如，`my-function-31exxmpl`），然后选择**删除**。

1. 在**删除 `my-function-31exxmpl`?** 对话框中，输入角色的名称，然后选择**删除**。

**提示**  
您可以使用 AWS CloudFormation 或 AWS Serverless Application Model (AWS SAM) 自动创建和清理 AWS 资源。有关示例 CloudFormation 模板，请参阅 **awsdocs** GitHub 存储库中的 [API Gateway 示例模板](https://github.com/awsdocs/amazon-api-gateway-developer-guide/tree/main/cloudformation-templates)。