使用 Amazon CloudTrail 记录 Amazon API Gateway API 调用 - Amazon API Gateway
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon CloudTrail 记录 Amazon API Gateway API 调用

Amazon API Gateway 与 Amazon CloudTrail 集成,后者是一项提供用户、角色或 Amazon Web Service所采取操作的记录的服务。CloudTrail 将 API Gateway 服务的所有 REST API 调用作为事件捕获。捕获的调用包含来自 API Gateway 控制台的调用和对 API Gateway 服务 API 的代码调用。借助通过 CloudTrail 收集的信息,您可以确定向 API Gateway 发出的请求、发出请求的 IP 地址、请求的发出时间以及其它详细信息。

注意

未在 CloudTrail 中记录 TestInvokeAuthorizerTestInvokeMethod

每个事件或日记账条目都包含有关生成请求的人员信息。身份信息有助于您确定以下内容:

  • 请求是使用根用户凭证还是用户凭证发出的。

  • 请求是否代表 IAM Identity Center 用户发出。

  • 请求是使用角色还是联合用户的临时安全凭证发出的。

  • 请求是否由其他 Amazon Web Service 发出。

当您创建账户并可以自动访问 CloudTrail 事件历史记录时,CloudTrail 在您的 Amazon Web Services 账户 中处于活动状态。CloudTrail 事件历史记录提供对 Amazon Web Services 区域 中过去 90 天的已记录管理事件的可查看、可搜索、可下载和不可变记录。有关更多信息,请参见《Amazon CloudTrail 用户指南》的 使用 CloudTrail 事件历史记录。查看事件历史记录不会收取 CloudTrail 费用。

要持续记录您的 Amazon Web Services 账户 过去 90 天的事件,请创建跟踪或 CloudTrail Lake 事件数据存储。

CloudTrail 跟踪

通过跟踪记录,CloudTrail 可将日志文件传送至 Simple Storage Service (Amazon S3) 存储桶。使用 Amazon Web Services Management Console 创建的所有跟踪均具有多区域属性。您可以通过使用 Amazon CLI 创建单区域或多区域跟踪。建议创建多区域跟踪,因为您可记录您账户中的所有 Amazon Web Services 区域 的活动。如果您创建单区域跟踪,则只能查看跟踪的 Amazon Web Services 区域 中记录的事件。有关跟踪的更多信息,请参阅《Amazon CloudTrail 用户指南》中的为您的 Amazon Web Services 账户 创建跟踪为组织创建跟踪

通过创建跟踪,您可以从 CloudTrail 免费向您的 Amazon S3 存储桶传送一份正在进行的管理事件的副本,但会收取 Amazon S3 存储费用。有关 CloudTrail 定价的更多信息,请参阅 Amazon CloudTrail 定价。有关 Amazon S3 定价的信息,请参阅 Amazon S3 定价

CloudTrail Lake 事件数据存储

CloudTrail Lake 允许您对事件运行基于 SQL 的查询。CloudTrail Lake 可将基于行的 JSON 格式的现有事件转换为 Apache ORC 格式。ORC 是一种针对快速检索数据进行优化的列式存储格式。事件将被聚合到事件数据存储中,它是基于您通过应用高级事件选择器选择的条件的不可变的事件集合。应用于事件数据存储的选择器用于控制哪些事件持续存在并可供您查询。有关 CloudTrail Lake 的更多信息,请参阅《Amazon CloudTrail 用户指南》中的使用 Amazon CloudTrail Lake

CloudTrail Lake 事件数据存储和查询会产生费用。创建事件数据存储时,您可以选择要用于事件数据存储的定价选项。定价选项决定了摄取和存储事件的成本,以及事件数据存储的默认和最长保留期。有关 CloudTrail 定价的更多信息,请参阅 Amazon CloudTrail 定价

CloudTrail 中的 API Gateway 管理事件

管理事件提供对您 Amazon Web Services 账户 内的资源所执行管理操作的相关信息。这些也称为控制层面操作。原定设置情况下,CloudTrail 会记录管理事件。

Amazon API Gateway 将所有 API Gateway 操作记录为管理事件,但 TestInvokeAuthorizerTestInvokeMethod 除外。有关 API Gateway 记录到 CloudTrail 的 Amazon API Gateway 操作的列表,请参阅 Amazon API Gateway API Reference

API Gateway 事件示例

一个事件表示一个来自任何源的请求,包括有关所请求的 API 操作、操作的日期和时间、请求参数等方面的信息。CloudTrail 日志文件不是公用 API 调用的有序堆栈跟踪,因此事件不会按任何特定顺序显示。

下面的示例显示了一个 CloudTrail 事件,该事件演示了 API Gateway GetResource 操作。

{ Records: [ { eventVersion: "1.03", userIdentity: { type: "Root", principalId: "AKIAI44QH8DHBEXAMPLE", arn: "arn:aws:iam::123456789012:root", accountId: "123456789012", accessKeyId: "AKIAIOSFODNN7EXAMPLE", sessionContext: { attributes: { mfaAuthenticated: "false", creationDate: "2015-06-16T23:37:58Z" } } }, eventTime: "2015-06-17T00:47:28Z", eventSource: "apigateway.amazonaws.com", eventName: "GetResource", awsRegion: "us-east-1", sourceIPAddress: "203.0.113.11", userAgent: "example-user-agent-string", requestParameters: { restApiId: "3rbEXAMPLE", resourceId: "5tfEXAMPLE", template: false }, responseElements: null, requestID: "6d9c4bfc-148a-11e5-81b6-7577cEXAMPLE", eventID: "4d293154-a15b-4c33-9e0a-ff5eeEXAMPLE", readOnly: true, eventType: "AwsApiCall", recipientAccountId: "123456789012" }, ... additional entries ... ] }

有关 CloudTrail 记录内容的信息,请参阅《Amazon CloudTrail 用户指南》中的 CloudTrail 记录内容