Created
June 30, 2025 15:02
-
-
Save dot-mike/a9a84bd3a80099415f70c72a42748538 to your computer and use it in GitHub Desktop.
Process and transform Mikrotik DHCP logs into JSON format and store it in S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
import os | |
from datetime import datetime | |
s3 = boto3.client('s3') | |
def lambda_handler(event, context): | |
incoming_data = json.loads(event['body']) if 'body' in event else event | |
bucket_name = os.environ['S3_BUCKET'] | |
date_str = datetime.now().strftime("%Y-%m-%d") | |
file_name = f"dhcp_data_{date_str}.json" | |
try: | |
try: | |
response = s3.get_object(Bucket=bucket_name, Key=file_name) | |
existing_data = json.loads(response['Body'].read().decode('utf-8')) | |
except s3.exceptions.NoSuchKey: | |
existing_data = [] | |
if not isinstance(existing_data, list): | |
existing_data = [existing_data] | |
existing_data.append(incoming_data) | |
json_data = json.dumps(existing_data) | |
s3.put_object(Bucket=bucket_name, Key=file_name, Body=json_data) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Data saved successfully') | |
} | |
except Exception as e: | |
return { | |
'statusCode': 500, | |
'body': json.dumps(f'Error saving data: {str(e)}') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment