

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

# 使用记录 AWS IoT OTA API 调用 AWS CloudTrail
<a name="iot-using-cloudtrail-afr"></a>

FreeRTOS CloudTrail 与一项服务集成，该服务可 AWS IoT 捕获 OTA API 调用并将日志文件传输到您指定的 Amazon S3 存储桶。 CloudTrail 捕获从你的代码到 AWS IoT OTA 的 API 调用 APIs。使用收集的信息 CloudTrail，您可以确定向 AWS IoT OTA 发出的请求、发出请求的源 IP 地址、谁发出了请求、何时发出请求等。

有关更多信息 CloudTrail，包括如何配置和启用它，请参阅《[https://docs.aws.amazon.com/awscloudtrail/latest/userguide/](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/)。

## FreeRTOS 信息在 CloudTrail
<a name="aws-iot-info-in-cloudtrail-afr"></a>

在您的 AWS 账户中启用 CloudTrail 日志记录后，对 AWS IoT OTA 操作的 API 调用将在 CloudTrail 日志文件中进行跟踪，这些调用与其他 AWS 服务记录一起写入日志文件。 CloudTrail 根据时间段和文件大小决定何时创建和写入新文件。

以下 AWS IoT OTA 控制平面操作由以下人员记录 CloudTrail：
+ [CreateStream](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateStream.html)
+ [DescribeStream](https://docs.aws.amazon.com/iot/latest/apireference/API_DescribeStream.html)
+ [ListStreams](https://docs.aws.amazon.com/iot/latest/apireference/API_ListStreams.html)
+ [UpdateStream](https://docs.aws.amazon.com/iot/latest/apireference/API_UpdateStream.html)
+ [DeleteStream](https://docs.aws.amazon.com/iot/latest/apireference/API_DeleteStream.html)
+ [创建OTAUpdate](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateOTAUpdate.html)
+ [获取 OTAUpdate](https://docs.aws.amazon.com/iot/latest/apireference/API_GetOTAUpdate.html)
+ [列表OTAUpdates](https://docs.aws.amazon.com/iot/latest/apireference/API_ListOTAUpdates.html)
+ [删除OTAUpdate](https://docs.aws.amazon.com/iot/latest/apireference/API_DeleteOTAUpdate.html)

**注意**  
AWS IoT OTA 数据平面操作（设备端）不会由记录 CloudTrail。 CloudWatch 用于监视这些。

每个日志条目都包含有关生成请求的人员的信息。日志条目中的用户身份信息可帮助您确定以下内容：
+ 请求是使用根用户凭证还是 IAM 用户凭证发出的。
+ 请求是使用角色还是联合用户的临时安全凭证发出的。
+ 请求是否由其他 AWS 服务发出。

有关更多信息，请参阅[CloudTrail 用户身份元素](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html)。 AWS IoT OTA 操作记录在 [AWS IoT OTA API 参考](https://docs.aws.amazon.com/iot/latest/apireference)中。

日志文件可以在 Amazon S3 存储桶中存储任意长时间，不过您也可以定义 Amazon S3 生命周期规则以自动存档或删除日志文件。默认情况下，系统将使用 Amazon S3 服务器端加密 (SSE) 对日志文件进行加密。

如果您希望在日志文件传送时收到通知，则可以配置 CloudTrail 为发布 Amazon SNS 通知。有关更多信息，请参阅为其[配置 Amazon SNS 通知](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/getting_notifications_top_level.html)。 CloudTrail

您还可以将来自多个 AWS 区域和多个 AWS 账户的 AWS IoT OTA 日志文件聚合到单个 Amazon S3 存储桶中。

有关更多信息，请参阅[从多个区域接收 CloudTrail 日志文件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-receive-logs-from-multiple-accounts.html)和[从多个账户接收 CloudTrail 日志文件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-receive-logs-from-multiple-accounts.html)。

## 了解 FreeRTOS 日志文件条目
<a name="understanding-aws-iot-entries-afr"></a>

CloudTrail 日志文件可以包含一个或多个日志条目。每个条目列出了多个 JSON 格式的事件。一个日志条目表示来自任何源的一个请求，包括有关所请求的操作、操作的日期和时间、请求参数等方面的信息。日志条目不是公用 API 调用的有序堆栈跟踪，因此它们不会以任何特定顺序显示。

以下示例显示了一个 CloudTrail 日志条目，该条目演示了号召性用语中的日志。`CreateOTAUpdate`

```
{
    "eventVersion": "1.05",
    "userIdentity": {
        "type": "IAMUser",
        "principalId": "EXAMPLE",
        "arn": "arn:aws:iam::your_aws_account:user/your_user_id",
        "accountId": "your_aws_account",
        "accessKeyId": "your_access_key_id",
        "userName": "your_username",
        "sessionContext": {
            "attributes": {
                "mfaAuthenticated": "false",
                "creationDate": "2018-08-23T17:27:08Z"
            }
        },
        "invokedBy": "apigateway.amazonaws.com"
    },
    "eventTime": "2018-08-23T17:27:19Z",
    "eventSource": "iot.amazonaws.com",
    "eventName": "CreateOTAUpdate",
    "awsRegion": "your_aws_region",
    "sourceIPAddress": "apigateway.amazonaws.com",
    "userAgent": "apigateway.amazonaws.com",
    "requestParameters": {
        "targets": [
            "arn:aws:iot:your_aws_region:your_aws_account:thing/Thing_CMH"
        ],
        "roleArn": "arn:aws:iam::your_aws_account:role/Role_FreeRTOSJob",
        "files": [
            {
                "fileName": "/sys/mcuflashimg.bin",
                "fileSource": {
                    "fileId": 0,
                    "streamId": "your_stream_id"
                },
                "codeSigning": {
                    "awsSignerJobId": "your_signer_job_id"
                }
            }
        ],
        "targetSelection": "SNAPSHOT",
        "otaUpdateId": "FreeRTOSJob_CMH-23-1535045232806-92"
    },
    "responseElements": {
        "otaUpdateArn": "arn:aws:iot:your_aws_region:your_aws_account:otaupdate/FreeRTOSJob_CMH-23-1535045232806-92",
        "otaUpdateStatus": "CREATE_PENDING",
        "otaUpdateId": "FreeRTOSJob_CMH-23-1535045232806-92"
    },
    "requestID": "c9649630-a6f9-11e8-8f9c-e1cf2d0c9d8e",
    "eventID": "ce9bf4d9-5770-4cee-acf4-0e5649b845c0",
    "eventType": "AwsApiCall",
    "recipientAccountId": "recipient_aws_account"
}
```