Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raecoo/109b4b47a3a5bebd397248d9234706fd to your computer and use it in GitHub Desktop.
Save raecoo/109b4b47a3a5bebd397248d9234706fd to your computer and use it in GitHub Desktop.
"""
Copy objects from one bucket/prefix to another bucket with the same prefix.
Used to allow CloudFront logs to get parsed for uploading to ES *AND* analyzed
by WAF.
CloudFront Distribution logs -> s3://es-bucket/incoming -> Lambda (this) -> s3://waf-bucket/
Set environment variable `destination_bucket`
"""
from __future__ import print_function
import json
import os
import urllib
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
destination_bucket = os.environ.get('destination_bucket')
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("CONTENT TYPE: " + response['ContentType'])
destination_key = key
destination_key_parts = destination_key.split('/')
# strip off `/incoming` prefix
if destination_key_parts[0] == 'incoming':
destination_key = '/'.join(destination_key_parts[1:])
print("Copying s3://{}/{} to s3://{}/{} ".format(
bucket,
key,
destination_bucket,
destination_key))
response_copy = boto3.resource('s3').Object(
destination_bucket,destination_key).copy_from(
CopySource='{}/{}'.format(bucket, key),
ContentType=response['ContentType'],
StorageClass='STANDARD'
)
return destination_key
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment