-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
38 lines (32 loc) · 1.17 KB
/
Copy pathlambda_function.py
File metadata and controls
38 lines (32 loc) · 1.17 KB
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
import json
import boto3
import os
def handler(event, context):
# Initialize Boto3 clients
guardduty = boto3.client('guardduty')
sns = boto3.client('sns')
# Get the GuardDuty detector ID
detector_id = os.environ['DETECTOR_ID']
# Fetch GuardDuty findings
findings_response = guardduty.list_findings(DetectorId=detector_id)
finding_ids = findings_response['FindingIds']
# If there are findings, fetch the details
if finding_ids:
findings_details = guardduty.get_findings(DetectorId=detector_id, FindingIds=finding_ids)
# Format the findings for the SNS message
findings_message = json.dumps(findings_details, indent=2)
# Publish the findings to SNS
sns_response = sns.publish(
TopicArn=os.environ['SNS_TOPIC_ARN'],
Message=findings_message,
Subject='GuardDuty Findings Alert'
)
return {
'statusCode': 200,
'body': json.dumps('SNS notification sent successfully!')
}
else:
return {
'statusCode': 200,
'body': json.dumps('No GuardDuty findings at this time.')
}