ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

AWS CLI CloudTrail start-logging 命令实战:启用与管理 CloudTrail 日志记录

AWS CLI CloudTrail start-logging 命令实战:启用与管理 CloudTrail 日志记录 AWS CLI CloudTrail start-logging 命令实战启用与管理 CloudTrail 日志记录【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli导读本文以 AWS CLI 中aws cloudtrail start-logging命令为主线深入讲解如何启用、查看与停止 CloudTrail 的日志记录能力。结合 start-logging.rst 官方示例并串联仓库内同目录下的配套示例如 get-trail-status.rst、stop-logging.rst以及 botocore 中 CloudTrail 服务的 API 模型定义帮助读者掌握一条 Trail 从创建、启动日志记录、查看运行状态到停止记录的完整生命周期管理方法并理解命令底层对应的 API 调用与异常处理机制。一、start-logging 命令是什么start-logging是 AWS CLI 针对 CloudTrail 服务封装的高层命令对应 CloudTrail 服务端StartLoggingAPI 操作。其作用是启动一条已创建 Trail 的日志记录功能让 CloudTrail 开始捕获账户内的 AWS API 调用事件并将日志文件投递到该 Trail 绑定的 S3 桶以及可选的 CloudWatch Logs 日志组。官方示例原文在仓库文档 start-logging.rst 中给出的示例非常简洁aws cloudtrail start-logging --name Trail1该命令会开启名为Trail1的 Trail 的日志记录。底层 API 模型从 botocore 的 CloudTrail 服务模型 service-2.json 可以看到StartLogging操作具有如下特征请求方式POST请求路径为/所有 CloudTrail 操作共用该路径通过请求体中的 Action 字段区分请求参数StartLoggingRequest结构体只有一个必需成员Name即「Trail 的名称或 CloudTrail ARN」格式形如arn:aws:cloudtrail:us-east-2:123456789012:trail/MyTrail响应结构StartLoggingResponse为空结构体成功时不返回任何数据——这也是该命令执行成功后终端通常没有任何输出、仅返回退出码 0 的原因。参数说明参数是否必需说明--name必需指定要启动日志记录的 Trail 名称或该 Trail 的完整 CloudTrail ARNarn:aws:cloudtrail:region:account-id:trail/trail-name。CLI 层会将该值透传给 API 请求的Name字段。二、命令背后的工作流程与适用场景多区域 Trail 的调用约束根据StartLogging操作的官方文档说明记录在 service-2.json 中Starts the recording of Amazon Web Services API calls and log file delivery for a trail. For a trail that is enabled in all Regions, this operation must be called from the Region in which the trail was created. This operation cannot be called on the shadow trails of a trail that is enabled in all Regions.翻译成实践要点对于多区域 TrailIsMultiRegionTrail为 true即「enabled in all Regions」start-logging必须在该 Trail 的**创建区域Home Region**调用多区域 Trail 在其他区域自动生成的「影子 Trail」shadow trails即复制品上不能调用该操作因此在执行aws cloudtrail start-logging --name Trail1时需要注意当前 CLI 的默认区域可通过--region参数或AWS_DEFAULT_REGION环境变量设置是否与 Trail 的 Home Region 一致。单区域 vs 多区域 Trail结合 describe-trails.rst 的示例输出可以看到Trail 的配置中有一个关键字段IsMultiRegionTrail{ IncludeGlobalServiceEvents: true, Name: Trail1, TrailARN: arn:aws:cloudtrail:us-east-1:123456789012:trail/Trail1, LogFileValidationEnabled: false, IsMultiRegionTrail: false, S3BucketName: amzn-s3-demo-bucket, HomeRegion: us-east-1 }单区域 Trail 只记录当前区域的 API 调用多区域 Trail 记录所有区域的 API 调用含全局服务事件IncludeGlobalServiceEvents默认开启且 Trail 本身只需创建一次。start-logging对两者均适用但多区域 Trail 必须在其HomeRegion中执行启动操作。三、完整实战从创建到启动日志记录第 1 步创建 Trailstart-logging的前提是存在一条已创建的 Trail。参考 create-trail.rstaws cloudtrail create-trail \ --name Trail1 \ --s3-bucket-name amzn-s3-demo-bucket \ --is-multi-region-trail该示例创建一个名为Trail1的多区域 Trail并将日志投递到amzn-s3-demo-bucket桶。创建成功后返回{ IncludeGlobalServiceEvents: true, Name: Trail1, TrailARN: arn:aws:cloudtrail:us-west-2:123456789012:trail/Trail1, LogFileValidationEnabled: false, IsMultiRegionTrail: true, S3BucketName: amzn-s3-demo-bucket }注意新创建的 Trail 默认处于停止记录状态需要显式调用start-logging才开始捕获事件。第 2 步启动日志记录aws cloudtrail start-logging --name Trail1命令成功执行后没有任何输出StartLoggingResponse为空结构可通过echo $?查看退出码为 0 来判断成功。第 3 步确认日志记录状态启动后建议用get-trail-status验证日志记录确实已开启。参考 get-trail-status.rstaws cloudtrail get-trail-status --name Trail1返回的关键字段{ IsLogging: true, StartLoggingTime: 1446834998.695, TimeLoggingStarted: 2015-11-06T18:36:38Z, StopLoggingTime: 1446834996.933, LatestDeliveryTime: 1454022144.869, LatestDeliveryAttemptSucceeded: 2016-01-28T23:02:24Z, LatestCloudWatchLogsDeliveryTime: 1454022144.918, LatestNotificationAttemptSucceeded: 2016-01-28T23:02:24Z }字段含义解读字段含义IsLogging当前是否正在记录日志true表示已启动StartLoggingTime最近一次执行start-logging的时间戳Unix 秒TimeLoggingStarted本次日志记录开始的时间StopLoggingTime最近一次执行stop-logging的时间戳LatestDeliveryTime最近一次成功投递日志文件到 S3 的时间LatestDeliveryAttemptSucceeded最近一次日志投递尝试成功的时间LatestCloudWatchLogsDeliveryTime最近一次投递到 CloudWatch Logs 的时间LatestNotificationAttemptSucceeded最近一次 SNS 通知投递成功的时间通过IsLogging: true即可确认 Trail 已处于记录状态。四、停止记录与再次启动当需要暂停审计时使用stop-logging。参考 stop-logging.rstaws cloudtrail stop-logging --name Trail1该命令关闭Trail1的日志记录。StopLogging与StartLogging是同一对生命周期操作二者都只接受--name参数、都返回空响应。停止后get-trail-status中的IsLogging会变为false。典型运维场景合规审计窗口在重要变更操作前执行start-logging确保事件被完整记录变更结束后可暂时停止以减少存储成本故障排查发现日志缺失时先start-logging恢复记录再用get-trail-status检查投递是否正常多区域一致性多区域 Trail 的启停必须始终在 Home Region 执行否则会收到区域相关错误。五、其他配套管理命令生命周期全景完整的 Trail 生命周期由仓库awscli/examples/cloudtrail/目录下的示例覆盖阶段命令示例文档创建create-trailcreate-trail.rst更新update-trailupdate-trail.rst启动记录start-loggingstart-logging.rst停止记录stop-loggingstop-logging.rst状态查询get-trail-statusget-trail-status.rst事件选择器put-event-selectors/get-event-selectorsput-event-selectors.rst、get-event-selectors.rst日志校验validate-logsvalidate-logs.rst删除delete-traildelete-trail.rst其中validate-logs用于校验日志文件的完整性示例输出为Validating log files for trail arn:aws:cloudtrail:us-east-1:123456789012:trail/Trail1 between 2016-01-29T19:00:00Z and 2016-01-29T22:15:43Z Results requested for 2016-01-29T19:00:00Z to 2016-01-29T22:15:43Z Results found for 2016-01-29T19:24:57Z to 2016-01-29T21:24:57Z: 3/3 digest files valid 15/15 log files valid注意create-subscription、update-subscription和validate-logs是 AWS CLI 在 CloudTrail 低层 API 之上额外注入的高层命令其注入逻辑定义在 awscli/customizations/cloudtrail/init.py 的inject_commands函数中注册于building-command-table.cloudtrail事件。start-logging本身则是标准的低层 API 透传命令直接由 botocore 模型驱动生成。六、常见错误与处理建议根据 service-2.json 中StartLogging定义的错误列表执行该命令可能遇到以下异常异常典型触发场景TrailNotFoundException--name指定的 Trail 不存在检查名称拼写与所在区域InvalidTrailNameExceptionTrail 名称不符合命名规范如包含非法字符InvalidHomeRegionException多区域 Trail 在非 Home Region 调用应切到创建区域UnsupportedOperationException对影子 Trail 等不支持启动操作的对象调用OperationNotPermittedException当前 IAM 身份权限不足CloudTrailARNInvalidException传入的 ARN 格式非法ConflictException/ThrottlingException操作冲突或请求被限流排查建议当命令报错时先执行aws cloudtrail describe-trails --trail-name-list Trail1确认 Trail 存在及其HomeRegion再核对 CLI 的默认区域aws configure get region确保二者一致。七、小结aws cloudtrail start-logging --name trail-name是 CloudTrail 日志记录生命周期中的「开关」操作参数仅一个必需的--nameTrail 名称或 ARN成功执行无输出通过退出码或get-trail-status的IsLogging字段确认结果多区域 Trail 必须在 Home Region 执行配合create-trail、stop-logging、get-trail-status、delete-trail即可完成 Trail 的完整生命周期管理。如需查看上述命令的完整官方示例可继续阅读仓库awscli/examples/cloudtrail/目录下的对应.rst文件API 层的精确参数与错误定义则见 awscli/botocore/data/cloudtrail/2013-11-01/service-2.json。【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表