

# 使用 Amazon Config 监控 API Gateway API 配置
<a name="apigateway-config"></a>

您可以使用 [Amazon Config](https://www.amazonaws.cn/config/) 来记录对您的 API Gateway API 资源所做的配置更改，并根据资源更改发送通知。维护 API Gateway 资源的配置更改历史记录对于运行问题排查、审计与合规性使用案例非常有用。

Amazon Config 可以跟踪以下项的更改：
+ **API 阶段配置**，如：
  + 缓存集群设置
  + 限制设置
  + 访问日志设置
  + 为阶段设置的活动部署
+ **API 配置**，如：
  + 终端节点配置
  + 版本
  + 协议
  + 标签

此外，Amazon Config 规则 功能让您能够定义配置规则并自动检测、跟踪和提醒对于这些规则的违反行为。通过跟踪对这些资源配置属性的更改，您还可以为 API Gateway 资源编写更改触发的 Amazon Config 规则，并根据最佳实践测试资源配置。

您可以使用 Amazon Config 控制台或 Amazon Config 为账户启用 Amazon CLI。选择要跟踪更改的资源类型。如果您之前配置了 Amazon Config 来记录所有资源类型，那么这些 API Gateway 资源将会自动记录到您的账户中。所有‭Amazon 公共区域和‭ Amazon GovCloud (US) 均提供对 ‭Amazon Config 中的 Amazon API Gateway 的支持。有关受支持区域的完整列表，请参阅《Amazon Web Services 一般参考》中的 [Amazon API Gateway 端点和限额](https://docs.amazonaws.cn/general/latest/gr/apigateway.html)。

**Topics**
+ [支持的资源类型](#apigateway-config-resources-rules)
+ [设置 Amazon Config](#apigateway-config-setup)
+ [配置 Amazon Config 以记录 API Gateway 资源](#apigateway-config-configuring)
+ [在 Amazon Config 控制台中查看 API Gateway 配置详细信息](#apigateway-config-console)
+ [使用 Amazon Config 规则评估 API Gateway 资源](#apigateway-config-rules)

## 支持的资源类型
<a name="apigateway-config-resources-rules"></a>

以下 API Gateway 资源类型与 Amazon Config 集成并记录到 [Amazon Config 支持的Amazon资源类型和资源关系中](https://docs.amazonaws.cn/config/latest/developerguide/resource-config-reference.html)：
+ `AWS::ApiGatewayV2::Api`（WebSocket 和 HTTP API）
+ `AWS::ApiGateway::RestApi` (REST API)
+ `AWS::ApiGatewayV2::Stage`（WebSocket 和 HTTP API 阶段）
+ `AWS::ApiGateway::Stage`（REST API 阶段）

有关 Amazon Config 的更多信息，请参阅 [Amazon Config 开发人员指南](https://docs.amazonaws.cn/config/latest/developerguide/)。有关定价信息，请参阅 [Amazon Config 定价信息页](https://www.amazonaws.cn/config/pricing/)。

**重要**  
如果您在部署 API 后更改以下任一 API 属性，则*必须* [重新部署](how-to-deploy-api.md) API 以传播更改。否则，您将在 Amazon Config 控制台中看到属性更改，但之前的属性设置仍有效；API 的运行时行为将保持不变。  
**`AWS::ApiGateway::RestApi`** – `binaryMediaTypes`, `minimumCompressionSize`, `apiKeySource`
**`AWS::ApiGatewayV2::Api`** – `apiKeySelectionExpression`

## 设置 Amazon Config
<a name="apigateway-config-setup"></a>

要初次设置 Amazon Config，请参阅 [Amazon Config 开发人员指南](https://docs.amazonaws.cn/config/latest/developerguide/)中的以下主题。
+ [使用控制台设置 Amazon Config](https://docs.amazonaws.cn/config/latest/developerguide/gs-console.html)
+ [使用 Amazon CLI 设置 Amazon Config](https://docs.amazonaws.cn/config/latest/developerguide/gs-cli.html)

## 配置 Amazon Config 以记录 API Gateway 资源
<a name="apigateway-config-configuring"></a>

默认情况下，Amazon Config 会记录在环境的运行区域中发现的所有受支持类型的区域性资源的配置更改。您可以自定义 Amazon Config，使其仅记录特定资源类型的更改或仅记录对全局资源的更改。

要了解区域与全局资源以及了解如何自定义您的 Amazon Config 配置，请参阅[选择 Amazon Config 记录的资源](https://docs.amazonaws.cn/config/latest/developerguide/select-resources.html)。

## 在 Amazon Config 控制台中查看 API Gateway 配置详细信息
<a name="apigateway-config-console"></a>

您可使用 Amazon Config 控制台来查找 API Gateway 资源，并获取有关其配置的当前和历史详细信息。以下过程显示了如何查找有关 API Gateway API 的信息。

**在 Amazon Config 控制台中查找 API Gateway 资源**

1. 打开 [Amazon Config 控制台](https://console.amazonaws.cn/config)。

1. 选择 **Resources (资源)**。

1. 在 **Resource (资源)** 清单页面上，选择 **Resources (资源)**。

1. 打开 **Resource type (资源类型)** 菜单，滚动到 APIGateway 或 APIGatewayV2，然后选择一个或多个 API Gateway 资源类型。

1. 选择 **Look up (查找)**。

1. 选择 Amazon Config 显示的资源列表中的一个资源 ID。Amazon Config 将显示配置详细信息以及有关所选资源的其他信息。

1. 要查看记录的配置的完整详细信息，请选择 **View Details (查看详细信息)**。

要了解在此页面上查找资源和查看信息的更多方法，请参阅“Amazon Config 开发人员指南”中的[查看Amazon资源配置和历史记录](https://docs.amazonaws.cn/config/latest/developerguide/view-manage-resource.html)。

## 使用 Amazon Config 规则评估 API Gateway 资源
<a name="apigateway-config-rules"></a>

您可以创建 Amazon Config 规则，这些规则表示 API Gateway 资源的理想配置设置。您可以使用预定义的 [Amazon Config 托管规则](https://docs.amazonaws.cn/config/latest/developerguide/managed-rules-by-aws-config.html)，也可以定义自定义规则。Amazon Config 会持续跟踪对您的资源配置的更改，以确定这些更改是否符合规则中设定的所有条件。Amazon Config 控制台将显示您的规则与资源的合规性状态。

如果某个资源违反了某规则并且被标记为“不合规”，Amazon Config 可提醒您使用 [Amazon Simple Notification Service 开发人员指南](https://docs.amazonaws.cn/sns/latest/dg/) (Amazon SNS) 主题。要以编程方式使用这些 Amazon Config 提醒中的数据，请使用 Amazon Simple Queue Service (Amazon SQS) 队列作为 Amazon SNS 主题的通知终端节点。

要了解有关设置和使用规则的更多信息，请参阅 [Amazon Config 开发人员指南](https://docs.amazonaws.cn/config/latest/developerguide/)中的[使用规则评估资源](https://docs.amazonaws.cn/config/latest/developerguide/evaluate-config.html)。