CloudTrail を使用して、EC2 インスタンスの EBS ボリュームを誰が削除したかなど、特定のリソース上のアクションを検索する方法を教えてください。
AWS CloudTrail を使用して特定のリソースで情報を検索したいと考えています。
解決策
重要:
- 検索リクエストのレートは、アカウントごとに 1 秒あたり 1 リクエストに制限されています。この制限を超えると、スロットリングエラーが発生します。
- イベントが発生する前に、CloudTrail ロギングが有効になっていることを確認します。そうしないと、発生した時間範囲内のイベントを検索できません。
- AWS サービスは引き続きイベントを追加する可能性があります。CloudTrail はイベントを[イベント履歴]に記録します。
- AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーを受け取った場合は、最新の AWS CLI バージョンを使用していることを確認してください。
以下の例は AWS CLI から実行されています。これらのコマンドのほとんどは、コマンドライン JSON processor, jq を使用します。jq の詳細については、 jq の Web サイトを参照してください。
Amazon Linux に jq をインストールするには、yum を使用してください。
yum install jq
**注:**他のプラットフォーム用の jq インストールオプションがあります。jq のインストールオプションについて詳しくは、jq Web サイトの「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 は、最新の呼び出しの場合に予想よりも時間がかかる場合があります。
EBS スナップショットなどの異なるリソース ID を使用して同じ例を実行できます。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-0f59a355c2 example の 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 パラメータと**--endtime** パラメータを使用します。
$ 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 エラーが返されます。
関連するコンテンツ
- 質問済み 6年前lg...
- 質問済み 4年前lg...
- 質問済み 3年前lg...
- 質問済み 6ヶ月前lg...
- AWS公式更新しました 9ヶ月前
- AWS公式更新しました 9ヶ月前
- AWS公式更新しました 2年前