'How to read log file from s3 (cloudtrail) in Lambda function

I have just started using aws and have no idea on how to read log files in lambda from s3 that have been created by CloudTrail (using python-boto3)



Solution 1:[1]

You just need to assign a role to the lambda function that has IAM permissions to read the object in S3. A detailed walkthrough can be found from AWS here.

Solution 2:[2]

First you need to assign proper permissions to your IAM role. For code - Use boto3 library (AWS - SDK) to write lambda function.

Code for lambda handler:

def lambda_handler(event, context):
    # Goal 1: Read file from csv
    object_key = "event_history_j.json"  # Name of file
    bucket = "demo-cloudtrail-logs-ec2"  # Name of bucket
    client = boto3.client("s3")
    data = client.get_object(Bucket=bucket, Key=object_key)["Body"].read()
    return data

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Foghorn
Solution 2 S.B