如何使用 CloudTrail 搜尋特定資源的動作,例如誰刪除了 EC2 執行個體上的 EBS 磁碟區?
我想要使用 AWS CloudTrail 來搜尋特定資源以取得資訊。
解決方法
重要事項:
- 查詢請求的速率限制為每個帳戶每秒請求一次。如果超過此限制,則會發生節流錯誤。
- 在事件發生之前,請確認已開啟 CloudTrail 日誌記錄。否則,您無法在事件發生的時間範圍內查詢事件。
- AWS 服務可能會繼續新增事件。CloudTrail 會在事件歷史記錄中記錄事件。
- 如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請確定您使用的是最新版本的 AWS CLI。
從 AWS CLI 執行的範例如下。這些命令大多數會使用命令行 JSON 處理器「jq」。如需 jq 的相關資訊,請參閱 jq 網站。
若要在 Amazon Linux 上安裝 jq,請使用 yum:
yum install jq
**注意事項:**其他平台有 jq 安裝選項。如需 jq 安裝選項的相關資訊,請參閱 jq 網站上的下載 jq。
針對特定資源列出所有事件名稱
下列範例會使用 Amazon Elastic Block Store (Amazon EBS) 磁碟區 ID 以取得任何 API 動作的最近事件清單。將 vol-0f59a355c2example 的 AttributeValue 取代為您的磁碟區 ID,並且將 us-east-1 取代為您的區域。
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue=vol-0f59a355c2example --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output table --region us-east-1
**注意事項:**CloudTrail 可能需要比預期更長的時間來移入最近的呼叫。
您可以使用不同的資源 ID (例如 EBS 快照) 來執行相同的範例。將 snap-0993c0d9a8example 的 AttributeValue 取代為您的快照 ID,並且將 us-east-1 取代為您的區域。
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue=snap-0993c0d9a8example --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output table --region us-east-1
針對特定資源列出特定 API 動作的事件
下列範例會顯示如何針對 EBS 磁碟區取得 DeleteVolume API 動作的最近事件清單。將 vol-0f59a355c2example 的 AttributeValue 取代為您的磁碟區 ID。
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue=vol-0f59a355c2example --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output json --region us-east-1 | jq -r '.[] | select(.event == "DeleteVolume")'
範例輸出:
{ "username": "jdoe", "eventid": "e3ec4051-9999-4e87-9999-9cc72example", "resource": "vol-0f59a355c2example", "accesskey": "ASIAXUZVKEUACEXAMPLE", "time": 1550191014, "event": "DeleteVolume" }
針對所有資源列出特定事件名稱
下列範例會使用 DeleteVolume 事件名稱做為篩選條件,以列出已刪除的 EBS 磁碟區。將 us-east-1 取代為您的區域。
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=DeleteVolume --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output table --region us-east-1
針對所有資源列出已終止的 EC2 執行個體
下列範例會列出最近的 EC2 TerminateInstances。將 -region us-east-1 取代為您的區域。
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=TerminateInstances --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output table --region us-east-1
檢視特定事件 ID 的詳細資料
找到事件 ID 後,您可以檢視此事件的相關詳細資料。將 0840b15f-75b5-4082-a194-86e15example 的 AttributeValue 取代為您的事件 ID,並且將 us-east-1 取代為您的區域。
$ aws cloudtrail lookup-events --query "Events[0].CloudTrailEvent" --output text --lookup-attribute AttributeKey=EventId,AttributeValue=0840b15f-75b5-4082-a194-86e15example --region us-east-1 | jq -r '.'
範例輸出:
{ "eventVersion": "1.05", "userIdentity": { "type": "AssumedRole", "principalId": "AROAJ3THTCWDOKEXAMPLE:jdoe", "arn": "arn:aws:sts::52570EXAMPLE:assumed-role/Admin/jdoe", "accountId": "52570example", "accessKeyId": "ASIAXUZVKEUAKEXAMPLE", "sessionContext": { "attributes": { "mfaAuthenticated": "false", "creationDate": "2019-02-14T23:55:15Z" }, "sessionIssuer": { "type": "Role", "principalId": "AROAJ3THTCWDOKEXAMPLE", "arn": "arn:aws:iam::52570EXAMPLE:role/Admin", "accountId": "52570EXAMPLE", "userName": "Admin" } } }, "eventTime": "2019-02-15T00:48:05Z", "eventSource": "ec2.amazonaws.com", "eventName": "DeleteVolume", "awsRegion": "us-east-1", "sourceIPAddress": "999.999.999.999", "userAgent": "aws-cli/1.16.999 Python/2.7.15 Darwin/17.7.0 botocore/1.12.91", "requestParameters": { "volumeId": "vol-0c50d65c6eexample" }, "responseElements": { "_return": true }, "requestID": "a8a43ccd-736d-4b09-ba75-24b9cexample", "eventID": "0840b15f-75b5-4082-a194-86e15example", "eventType": "AwsApiCall", "recipientAccountId": "52570EXAMPLE" }
指定日期範圍
若要指定事件的日期範圍,請使用 --start-time 和 --end-time 參數:
$ aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=DeleteVolume --query 'Events[].{username:Username,time:EventTime,event:EventName,eventid:EventId,accesskey:AccessKeyId,resource:(Resources[0].ResourceName)}' --output table --region us-east-1 --start-time 2019-01-01T13:00Z --end-time 2019-03-01T14:00Z
**注意事項:**從有效時間戳記格式的清單中選擇您偏好的時間戳記。
列出的事件發生在開始時間之後,直到結束時間 (含)。
預設開始時間是過去 90 天內提供資料的最早日期。預設結束時間是最接近目前時間的事件發生時間。如果指定的開始時間晚於指定的結束時間,則會傳回 InvalidTimeRangeException 錯誤。
相關內容
- 已提問 5 個月前lg...
- 已提問 5 個月前lg...
- 已提問 1 年前lg...
- 已提問 10 個月前lg...
- 已提問 9 個月前lg...
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前