-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_cloudtrail_list.py
40 lines (32 loc) · 1.18 KB
/
tool_cloudtrail_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from langchain.tools import tool
from datetime import datetime
import boto3
@tool
def list_cloudtrail_events(max_events: int, region: str) -> str:
"""
Lists the last 100 of cloudtrail events
Args:
args (int): the number of events to return, defaults to 100
region (str): the AWS region to use
Returns:
str: a list of cloudwatch events
"""
cloudtrail = boto3.client('cloudtrail', region)
try:
response = cloudtrail.lookup_events(
MaxResults=max_events
)
events = response['Events']
event_list = []
for event in events:
event_info = {
"EventTime": event.get('EventTime', '').isoformat() if isinstance(event.get('EventTime'), datetime) else event.get('EventTime', ''),
"EventName": event.get('EventName', ''),
"Username": event.get('Username', ''),
"EventSource": event.get('EventSource', ''),
"AWSRegion": event.get('AWSRegion', '')
}
event_list.append(event_info)
return event_list
except Exception as e:
return f"Error retrieving CloudTrail events: {str(e)}"