使用 Amazon CloudWatch 记录 AWS CloudTrail API 调用 - Amazon CloudWatch

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

使用 Amazon CloudWatch 记录 AWS CloudTrail API 调用

Amazon CloudWatch 和 CloudWatch Synthetics 与 AWS CloudTrail 集成,后者是一项捕获服务,该服务提供由用户、角色或 AWS 服务执行的操作的记录。CloudTrail 会捕获由您的 AWS 账户或代表您的 AWS 账户发出的 API 调用。捕获的调用包含来自控制台的调用和对 API 操作的代码调用。

如果您创建跟踪,则可以使 CloudTrail 事件持续传送到 S3 存储桶(包括 CloudWatch 的事件)。如果您不配置跟踪,则仍可在 CloudTrail 控制台的 Event history (事件历史记录) 中查看最新事件。通过使用 CloudTrail 收集的信息,您可以确定向 CloudWatch 发出了什么请求、发出请求的 IP 地址、何人发出的请求、请求的发出时间以及其他详细信息。

要了解有关 CloudTrail 的更多信息,包括如何对其进行配置和启用,请参阅 AWS CloudTrail User Guide

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

  • 请求是使用根用户凭证还是 AWS Identity and Access Management (IAM) 用户凭证发出的。

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

  • 请求是否由其他 AWS 服务发出。

有关更多信息,请参阅 CloudTrail userIdentity 元素

要持续记录 AWS 账户中的事件(包括 CloudWatch 和 CloudWatch Synthetics 的事件),请创建跟踪。通过跟踪,CloudTrail 可将日志文件传送至 S3 存储桶。默认情况下,在控制台中创建跟踪时,此跟踪应用于所有 AWS 区域。此跟踪在 AWS 分区中记录所有区域中的事件,并将日志文件传送至您指定的 S3 存储桶。您可以配置其他 AWS 服务,进一步分析在 CloudTrail 日志中收集的事件数据并采取措施。有关更多信息,请参阅下列内容:

CloudTrail中的 CloudWatch 信息

CloudWatch 支持在 CloudTrail 日志文件中将以下操作记录为事件:

您可以在 CloudTrail 日志文件中记录以下 AWS 开发工具包指标 操作。这些操作仅供 开发工具包指标 使用,不可用于代码中。有关更多信息,请参阅 使用 AWS 开发工具包指标 监控应用程序

  • GetPublishingConfiguration – 用于确定如何发布指标。

  • GetPublishingSchema – 用于确定如何发布指标。

  • PutPublishingMetrics – 用于将 CloudWatch 代理运行状况指标发送到 AWS Support。

例如:CloudWatch日志文件条目

下面的示例显示一个 CloudTrail 日志条目,该条目演示 PutMetricAlarm 操作。

{ "Records": [{ "eventVersion": "1.01", "userIdentity": { "type": "Root", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:root", "accountId": "123456789012", "accessKeyId": "EXAMPLE_KEY_ID" }, "eventTime": "2014-03-23T21:50:34Z", "eventSource": "monitoring.amazonaws.com", "eventName": "PutMetricAlarm", "awsRegion": "us-east-1", "sourceIPAddress": "127.0.0.1", "userAgent": "aws-sdk-ruby2/2.0.0.rc4 ruby/1.9.3 x86_64-linux Seahorse/0.1.0", "requestParameters": { "threshold": 50.0, "period": 60, "metricName": "CloudTrail Test", "evaluationPeriods": 3, "comparisonOperator": "GreaterThanThreshold", "namespace": "AWS/CloudWatch", "alarmName": "CloudTrail Test Alarm", "statistic": "Sum" }, "responseElements": null, "requestID": "29184022-b2d5-11e3-a63d-9b463e6d0ff0", "eventID": "b096d5b7-dcf2-4399-998b-5a53eca76a27" }, ..additional entries ] }

以下日志文件条目显示某个用户调用了 CloudWatch Events PutRule 操作。

{ "eventVersion":"1.03", "userIdentity":{ "type":"Root", "principalId":"123456789012", "arn":"arn:aws:iam::123456789012:root", "accountId":"123456789012", "accessKeyId":"AKIAIOSFODNN7EXAMPLE", "sessionContext":{ "attributes":{ "mfaAuthenticated":"false", "creationDate":"2015-11-17T23:56:15Z" } } }, "eventTime":"2015-11-18T00:11:28Z", "eventSource":"events.amazonaws.com", "eventName":"PutRule", "awsRegion":"us-east-1", "sourceIPAddress":"AWS Internal", "userAgent":"AWS CloudWatch Console", "requestParameters":{ "description":"", "name":"cttest2", "state":"ENABLED", "eventPattern":"{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"]}", "scheduleExpression":"" }, "responseElements":{ "ruleArn":"arn:aws:events:us-east-1:123456789012:rule/cttest2" }, "requestID":"e9caf887-8d88-11e5-a331-3332aa445952", "eventID":"49d14f36-6450-44a5-a501-b0fdcdfaeb98", "eventType":"AwsApiCall", "apiVersion":"2015-10-07", "recipientAccountId":"123456789012" }

以下日志文件条目显示某个用户调用了 CloudWatch Logs CreateExportTask 操作。

{ "eventVersion": "1.03", "userIdentity": { "type": "IAMUser", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:user/someuser", "accountId": "123456789012", "accessKeyId": "AKIAIOSFODNN7EXAMPLE", "userName": "someuser" }, "eventTime": "2016-02-08T06:35:14Z", "eventSource": "logs.amazonaws.com", "eventName": "CreateExportTask", "awsRegion": "us-east-1", "sourceIPAddress": "127.0.0.1", "userAgent": "aws-sdk-ruby2/2.0.0.rc4 ruby/1.9.3 x86_64-linux Seahorse/0.1.0", "requestParameters": { "destination": "yourdestination", "logGroupName": "yourloggroup", "to": 123456789012, "from": 0, "taskName": "yourtask" }, "responseElements": { "taskId": "15e5e534-9548-44ab-a221-64d9d2b27b9b" }, "requestID": "1cd74c1c-ce2e-12e6-99a9-8dbb26bd06c9", "eventID": "fd072859-bd7c-4865-9e76-8e364e89307c", "eventType": "AwsApiCall", "apiVersion": "20140328", "recipientAccountId": "123456789012" }

CloudTrail 中的 CloudWatch Synthetics 信息

CloudWatch Synthetics 支持在 CloudTrail 日志文件中将以下操作记录为事件:

例如:CloudWatchSynthetics 日志文件条目

下面的示例显示一个 CloudTrail Synthetics 日志条目,其中包含 DescribeCanaries 操作相关信息。

{ "eventVersion": "1.05", "userIdentity": { "type": "AssumedRole", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:assumed-role/role_name", "accountId":"123456789012", "accessKeyId":"AKIAIOSFODNN7EXAMPLE", "sessionContext": { "sessionIssuer": { "type": "Role", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::111222333444:role/Administrator", "accountId":"123456789012", "userName": "SAMPLE_NAME" }, "webIdFederationData": {}, "attributes": { "mfaAuthenticated": "false", "creationDate": "2020-04-08T21:43:24Z" } } }, "eventTime": "2020-04-08T23:06:47Z", "eventSource": "synthetics.amazonaws.com", "eventName": "DescribeCanaries", "awsRegion": "us-east-1", "sourceIPAddress": "127.0.0.1", "userAgent": "aws-internal/3 aws-sdk-java/1.11.590 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.212-b03 java/1.8.0_212 vendor/Oracle_Corporation", "requestParameters": null, "responseElements": null, "requestID": "201ed5f3-15db-4f87-94a4-123456789", "eventID": "73ddbd81-3dd0-4ada-b246-123456789", "readOnly": true, "eventType": "AwsApiCall", "recipientAccountId": "111122223333" }

下面的示例显示一个 CloudTrail Synthetics 日志条目,其中包含 UpdateCanary 操作相关信息。

{ "eventVersion": "1.05", "userIdentity": { "type": "AssumedRole", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:assumed-role/role_name", "accountId":"123456789012", "accessKeyId":"AKIAIOSFODNN7EXAMPLE", "sessionContext": { "sessionIssuer": { "type": "Role", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::111222333444:role/Administrator", "accountId":"123456789012", "userName": "SAMPLE_NAME" }, "webIdFederationData": {}, "attributes": { "mfaAuthenticated": "false", "creationDate": "2020-04-08T21:43:24Z" } } }, "eventTime": "2020-04-08T23:06:47Z", "eventSource": "synthetics.amazonaws.com", "eventName": "UpdateCanary", "awsRegion": "us-east-1", "sourceIPAddress": "127.0.0.1", "userAgent": "aws-internal/3 aws-sdk-java/1.11.590 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.212-b03 java/1.8.0_212 vendor/Oracle_Corporation", "requestParameters": { "Schedule": { "Expression": "rate(1 minute)" }, "name": "sample_canary_name", "Code": { "Handler": "myOwnScript.handler", "ZipFile": "SAMPLE_ZIP_FILE" } }, "responseElements": null, "requestID": "fe4759b0-0849-4e0e-be71-1234567890", "eventID": "9dc60c83-c3c8-4fa5-bd02-1234567890", "readOnly": false, "eventType": "AwsApiCall", "recipientAccountId": "111122223333" }

下面的示例显示一个 CloudTrail Synthetics 日志条目,其中包含 GetCanaryRuns 操作相关信息。

{ "eventVersion": "1.05", "userIdentity": { "type": "AssumedRole", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::123456789012:assumed-role/role_name", "accountId":"123456789012", "accessKeyId":"AKIAIOSFODNN7EXAMPLE", "sessionContext": { "sessionIssuer": { "type": "Role", "principalId": "EX_PRINCIPAL_ID", "arn": "arn:aws:iam::111222333444:role/Administrator", "accountId":"123456789012", "userName": "SAMPLE_NAME" }, "webIdFederationData": {}, "attributes": { "mfaAuthenticated": "false", "creationDate": "2020-04-08T21:43:24Z" } } }, "eventTime": "2020-04-08T23:06:30Z", "eventSource": "synthetics.amazonaws.com", "eventName": "GetCanaryRuns", "awsRegion": "us-east-1", "sourceIPAddress": "127.0.0.1", "userAgent": "aws-internal/3 aws-sdk-java/1.11.590 Linux/4.9.184-0.1.ac.235.83.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.212-b03 java/1.8.0_212 vendor/Oracle_Corporation", "requestParameters": { "Filter": "TIME_RANGE", "name": "sample_canary_name", "FilterValues": [ "2020-04-08T23:00:00.000Z", "2020-04-08T23:10:00.000Z" ] }, "responseElements": null, "requestID": "2f56318c-cfbd-4b60-9d93-1234567890", "eventID": "52723fd9-4a54-478c-ac55-1234567890", "readOnly": true, "eventType": "AwsApiCall", "recipientAccountId": "111122223333" }