Skip to content

Enhance S3 cross region replication with lambda in AWS

implement S3 cross region replication with AWS lambda

copy when new object arrive

from __future__ import print_function
import json, time
import boto3
from boto3.s3.transfer import TransferConfig

print('Loading function')

def lambda_handler(event, context):
    key = event['Records'][0]['s3']['object']['key']
    source_bucket = "PUT-YOUR-SOURCE-BUCKET-HERE"
    target_bucket = "PUT-YOUR-DEST-BUCKET-HERE"
    config = TransferConfig(max_concurrency=20)
    s3 = boto3.client('s3')
    copy_source = {
        'Bucket': source_bucket,
        'Key': key
    }
    print("start delivering " + key + " from " + source_bucket)
    result = s3.copy(copy_source, target_bucket, key, Config=config)
    print(result)
    return 0

delete target object when source object removed

from __future__ import print_function

import json
import urllib
import boto3

print('Loading function')

s3 = boto3.client('s3')


def lambda_handler(event, context):

    # Get the object from the event and show its content type
    key = event['Records'][0]['s3']['object']['key']

    target_bucket = "PUT-YOUR-DEST-BUCKET-HERE"

    s3 = boto3.client('s3')
    result = s3.delete_object(Bucket=target_bucket, Key=key)
    print("deleting " + key + " from " + target_bucket)
    print(result)
    return 0

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.me, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No privacy information is collected here
Try my iOS App