AWS Lambda and Python
AWS Lambda is a server less computing platform. You can execute your code without provisioning or managing servers.
Tested an example of copying a text file dropped into one S3 bucket to another.
1. Create a IAM role with the CloudWatch and S3 policies.
Call role lambda_s3 and add policies: AWSOpsWorksCloudWatchLogs, AmazonS3FullAccess
2. Create two S3 buckets for source and target.
3. Create Lambda function for Copying a file from one bucket to another.
Author from scratch, Name = copyS3toS3 Python2.7, Existing Role = lambda_s3
Add S3 from left selections
Trigger select the source bucket, Object Created(All), Suffix = .txt, Check Enable Trigger
Click on function copyS3toS3 and add python code as showed in Appendix A
4. Save the Lambda function and upload a text file to the source s3 bucket to test.
5. You can go to Cloudwatch logs to root cause if test .txt file not showing up in target.
Appendix A: Lambda function python code
#######################################
from __future__ import print_function import json import boto3 import time import urllib print('Loading function') s3 = boto3.client("s3") def lambda_handler(event,context): source_bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']) target_bucket = 'iqonda-test02' # target s3 bucket name copy_source = {'Bucket':source_bucket, 'Key':key} try: print('Waiting for the file persist in the source bucket') waiter = s3.get_waiter('object_exists') waiter.wait(Bucket=source_bucket, Key=key) print('Copying object from source s3 bucket to target s3 bucket') s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source) 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
Appendix B:
###########
https://gist.github.com/anonymous/0f6b21d1586bd291d4ad0cc84c6383bb#file-s3-devnull-py