Ugly Green Logo

4n6ir.com

Container Registry

Download Website

GitHub Organization

Slack Workspace

April 05, 2024

Do not forget the AWS Amplify Logs

by John Lukach

I recently needed AWS Amplify logs for an investigation that became a painful experience; thus, I recommend adding an AWS Lambda that exports access logs daily with the provided Python example.

Log File Format

Python Libraries

import boto3
import datetime
import gzip
import os
import requests

Previous Day

yesterday = datetime.datetime.now() - datetime.timedelta(days=1)

Generate Access Logs

    client = boto3.client('amplify', region_name = 'us-east-2')

    response = client.generate_access_logs(
        startTime = datetime.datetime(yesterday.year, yesterday.month, yesterday.day),
        endTime = datetime.datetime(yesterday.year, yesterday.month, yesterday.day),
        domainName = '4n6ir.com',
        appId = os.environ['APP_ID']
    )

Download Access Logs

d = requests.get(response['logUrl'])

Set Filename

fname = str(yesterday.year)+'-'+str(yesterday.month)+'-'+str(yesterday.day)+'-4n6ircom.csv'

Write Access Logs

    if d.status_code == 200:
        with open('/tmp/'+fname, 'wb') as f:
            f.write(d.content)

Compress Access Logs

    with open('/tmp/'+fname, 'rb') as f_in:
        with gzip.open('/tmp/'+fname+'.gz', 'wb') as f_out:
            f_out.writelines(f_in)

Archive Access Logs

    s3 = boto3.client('s3')

    s3.upload_file(
        '/tmp/'+fname+'.gz', 
        os.environ['S3_BUCKET'],
        'year='+str(yesterday.year)+'/month='+str(yesterday.month)+'/'+fname+'.gz'
    )
tags: aws - amplify - cloudfront - access - logs