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.
import boto3
import datetime
import gzip
import os
import requests
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
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']
)
d = requests.get(response['logUrl'])
fname = str(yesterday.year)+'-'+str(yesterday.month)+'-'+str(yesterday.day)+'-4n6ircom.csv'
if d.status_code == 200:
with open('/tmp/'+fname, 'wb') as f:
f.write(d.content)
with open('/tmp/'+fname, 'rb') as f_in:
with gzip.open('/tmp/'+fname+'.gz', 'wb') as f_out:
f_out.writelines(f_in)
s3 = boto3.client('s3')
s3.upload_file(
'/tmp/'+fname+'.gz',
os.environ['S3_BUCKET'],
'year='+str(yesterday.year)+'/month='+str(yesterday.month)+'/'+fname+'.gz'
)