CloudTrail を使用して、誰が EC2 インスタンスの EBS ボリュームを削除したかなど、特定のリソースに対するアクションを検索する方法を教えてください。
最終更新日: 2021 年 1 月 25 日
簡単な説明
AWS CloudTrail を使用すると、AWS インフラストラクチャ全体でアカウントのアクティビティを記録および監視することができます。これを行うには、CloudTrail をオンにして、照会する EC2 リソースが存在する AWS リージョンでのログインを有効にする必要があります。
重要
- ルックアップ要求の速度は、アカウントごとに 1 秒あたり 1 要求に制限されています。この制限を超えると、スロットリングエラーが発生します。
- イベントが発生したときに CloudTrail のログ記録が有効になっていなかった場合、選択した時間範囲内に発生したイベントはルックアップできません。
解決方法
注: AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、最新の AWS CLI バージョンを使用していることを確認してください。
以下の例は、AWS CLI から実行されています。これらのコマンドのほとんどは、jq を使用していることに注意してください。
yum を使用して、Amazon Linux に jq をインストールします。
yum install jq
注: 他のプラットフォーム向けの jq インストールオプションがあります。
特定のリソースのすべてのイベント名を一覧表示する
次の例では、EBS ボリューム ID を使用して、API アクションに関する最近のイベントのリストを取得します。vol-0f59a355c2example の AttributeValue および us-east-1 の --region を実際の値に置き換えます。
$ 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 が最近の呼び出しを入力するのに予想以上に時間がかかる場合があります。
EBS スナップショットなど、異なるリソース ID を使用して同じ例を実行することができます。snap-0993c0d9a8example の AttributeValue および us-east-1 の --region を実際の値に置き換えます。
$ 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 ボリュームを一覧表示します。--region 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 に置き換え、--region 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 パラメータを使用して、イベントの日付範囲を指定することができます。一覧表示されているイベントは、開始時刻の後、および終了時刻までの間に発生したイベントです。
デフォルトの開始時刻は、過去 90 日間でデータが利用可能である最も早い日付です。デフォルトの終了時刻は、現在の時刻に最も近いイベントが発生した時刻です。
指定した開始時刻が指定した終了時刻より後であると、InvalidTimeRangeException エラーになります。
$ 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
注: [Valid timestamp formats] (有効なタイムスタンプ形式) のリストから、希望するタイムスタンプを選択します。