{"id":1268,"date":"2018-10-10T19:05:21","date_gmt":"2018-10-11T00:05:21","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1268"},"modified":"2018-10-10T19:06:24","modified_gmt":"2018-10-11T00:06:24","slug":"aws-lambda-and-python","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/aws-lambda-and-python\/","title":{"rendered":"AWS Lambda and Python"},"content":{"rendered":"
AWS Lambda is a server less computing platform. You can execute your code without provisioning or managing servers. <\/p>\n
Tested an example of copying a text file dropped into one S3 bucket to another.<\/p>\n
1. Create a IAM role with the CloudWatch and S3 policies.
\n\tCall role lambda_s3 and add policies: AWSOpsWorksCloudWatchLogs, AmazonS3FullAccess
\n2. Create two S3 buckets for source and target.
\n3. Create Lambda function for Copying a file from one bucket to another.
\n\tAuthor from scratch, Name = copyS3toS3 Python2.7, Existing Role = lambda_s3
\n\tAdd S3 from left selections
\n\tTrigger select the source bucket, Object Created(All), Suffix = .txt, Check Enable Trigger
\n\tClick on function copyS3toS3 and add python code as showed in Appendix A
\n4. Save the Lambda function and upload a text file to the source s3 bucket to test.
\n5. You can go to Cloudwatch logs to root cause if test .txt file not showing up in target.<\/p>\n
Appendix A: Lambda function python code
\n#######################################<\/p>\n
\r\nfrom __future__ import print_function\r\n\r\nimport json\r\nimport boto3\r\nimport time\r\nimport urllib\r\n\r\nprint('Loading function')\r\n\r\ns3 = boto3.client("s3")\r\n\r\ndef lambda_handler(event,context):\r\n source_bucket = event['Records'][0]['s3']['bucket']['name']\r\n key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])\r\n target_bucket = 'iqonda-test02' # target s3 bucket name\r\n copy_source = {'Bucket':source_bucket, 'Key':key}\r\n \r\n try:\r\n print('Waiting for the file persist in the source bucket')\r\n waiter = s3.get_waiter('object_exists')\r\n waiter.wait(Bucket=source_bucket, Key=key)\r\n print('Copying object from source s3 bucket to target s3 bucket')\r\n s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source)\r\n except Exception as e:\r\n print(e)\r\n print('Error getting object {} from bucket {}. Make sure they exist '\r\n 'and your bucket is in the same region as this '\r\n 'function.'.format(key, bucket))\r\n raise e\r\n<\/pre>\nAppendix B:
\n###########
\nhttps:\/\/gist.github.com\/anonymous\/0f6b21d1586bd291d4ad0cc84c6383bb#file-s3-devnull-py<\/p>\n","protected":false},"excerpt":{"rendered":"AWS Lambda is a server less computing platform. You can execute your code without provisioning or managing servers. Tested an<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"class_list":["post-1268","post","type-post","status-publish","format-standard","hentry","category-aws"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1268"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1268\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}