2017-04-26 121 views
-2

我想從Cloud Trail日誌解析嵌套的JSON以獲取用戶名數據和時間我該怎麼做呢是否有我可以在Lambda中使用的代碼或者有一些工具,例如JSON文件看起來像這樣AWS Cloud Trail日誌解析

{"version":"0","id":"5bd0a964-0969-4b1a-badd-3b4f7e9e077f","detail-type":"AWS API Call via CloudTrail","source":"aws.ec2","account":"111111111","time":"2017-04-25T16:07:33Z","region":"us-west-2","resources":[],"detail":{"eventVersion":"1.05","userIdentity":{"type":"Root","principalId":"1111111","arn":"arn:aws:iam::137247507067:root","accountId":"111111111","accessKeyId":"AAAAAAAA","userName":"roger","sessionContext":{"attributes":{"mfaAuthenticated":"true","creationDate":"2017-04-25T05:44:56Z"}}},"eventTime":"2017-04-25T16:07:33Z","eventSource":"ec2.amazonaws.com","eventName":"ModifyImageAttribute","awsRegion":"us-west-2","sourceIPAddress":"X.X.X.X","userAgent":"console.ec2.amazonaws.com","requestParameters":{"imageId":"ami-36e85556","launchPermission":{"add":{"items":[{"userId":"879125893843"}]}},"attributeType":"launchPermission"},"responseElements":{"_return":true},"requestID":"06ae4745-2d29-4a3b-b526-c5d8c4b4a7fc","eventID":"fc57b805-ae30-4ec7-bf4f-7a9c971ae0c7","eventType":"AwsApiCall"}} 

回答

0

您可以使用AWS Athena。

它基本上將雲軌日誌加載到表中,所以我們可以很容易地查詢所有的東西。

它有更多的選擇來分析cloudtrail logs.For例如,如果你想知道是誰發動的EC2實例,然後查詢這個樣子,

SELECT date_format(from_iso8601_timestamp(eventTime), '%Y-%m-%d') AS EventDate,useridentity.arn UserARN, 
     awsregion AS Region, 
     json_extract_scalar(item,'$.instanceId') AS InstanceId 
FROM cloudtrail_logs 
CROSS JOIN UNNEST (cast(json_extract(responseElements,'$.instancesSet.items') AS array(json))) AS i (item) 
WHERE eventsource='ec2.amazonaws.com' 
    AND eventname = 'RunInstances' 
    AND eventtime >= '2017-04-25T02:00:00.000' 
order by eventtime desc limit 2; 

結果是, enter image description here

https://aws.amazon.com/blogs/big-data/aws-cloudtrail-and-amazon-athena-dive-deep-to-analyze-security-compliance-and-operational-activity/