import json
import urllib.parse
import boto3
import gzip
from botocore.vendored import requests
import urllib3
s3 = boto3.client('s3')
http = urllib3.PoolManager()
splunk_hec_endpoint ='https://<<splunk_host>>:8088/services/collector/event'
splunk_hec_token = '<<hec_token>>'
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
with gzip.GzipFile(fileobj=response['Body']) as gz:
data = gz.read().decode('utf-8')
data = json.loads(data)
for cloudtrail_event in data['Records']:
payload = {
'event': cloudtrail_event,
'sourcetype': 'aws:cloudtrail'
}
payload = json.dumps(payload)
response = http.request(
'POST',
splunk_hec_endpoint,
body=payload,
headers={'Authorization': f'Splunk {splunk_hec_token}'}
)
if response.status != 200:
print(f'Failed to send event to Splunk: {response.data}')
else:
print('Event sent to Splunk succesfully')
except Exception as e:
print(e)
print('Error getting object. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e