0

I want to know who created a particular instance. I am using Cloud Trail to find out the statistics, but I am not able to get a particular statistics of who created that instance. I am using Python and Boto3 for finding out the details. I am using this code- Lookup events() from Cloud trail in boto3, to extract the information about an instance.

ct_conn = sess.client(service_name='cloudtrail',region_name='us-east-1')


events=ct_conn.lookup_events()
upaang saxena
  • 779
  • 1
  • 6
  • 18

2 Answers2

2

I found out the solution to the above problem using lookup_events() function.

ct_conn = boto3.client(service_name='cloudtrail',region_name='us-east-1')

events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxxxxx'}])
for data in events_dict['Events']:
    json_file= json.loads(data['CloudTrailEvent'])
    print json_file['userIdentity']['userName']
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
upaang saxena
  • 779
  • 1
  • 6
  • 18
  • Can you provide the entire code if possible or can you also mention the line where you defined "sess" before using it in the line "ct_conn = sess.client(service_name='cloudtrail',region_name='us-east-1')" – karthik v Mar 22 '17 at 11:05
  • You can define the session object just above the ct_conn line. – upaang saxena Apr 10 '17 at 10:32
0

@Karthik - Here is the sample of creating session

import boto3
import json
import os

session = boto3.Session(region_name='us-east-1',aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'])

ct_conn = session.client(service_name='cloudtrail',region_name='us-east-1')

events_dict= ct_conn.lookup_events(LookupAttributes=[{'AttributeKey':'ResourceName', 'AttributeValue':'i-xxx'}])

for data in events_dict['Events']:
    json_file= json.loads(data['CloudTrailEvent'])
    print (json_file['userIdentity']['userName'])
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Prajith
  • 19
  • 1