title: Resize and move Images using lambda function from one s3 to another s3
body: <blockquote>
    <p>WHAT I WANT TO DO </p>
  </blockquote>
  
  <ul>
  <li>I have 3 buckets. I want to trigger a lambda function when an image
  uploaded to bucket1. </li>
  <li>The lambda function will resize that image
  (500x500) and save that resized image in bucket2.</li>
  <li>the main image on bucket1 will be backed up in bucket3.</li>
  <li>the main image on bucket1 will be deleted then.</li>
  </ul>
  
  <blockquote>
    <p>WHAT I HAVE DONE SO FAR</p>
  </blockquote>
  
  <ul>
  <li><p>wrote a lambda function that will move images across the bucket.</p></li>
  <li><p>Made S3 trigger to the lambda function</p></li>
  </ul>
  
  <blockquote>
    <p>WHERE IS MY PROBLEM</p>
  </blockquote>
  
  <ul>
  <li><p>I am using PIL to resize the image. But PIL module is not in python STL. So I zipped my code
  with site packages and able to run. But the error says file not found.
  an example of a file key image/myimage.jpg</p></li>
  <li><p>Though I followed tutorials but if I try to use lambda layers instead of zipping everytime it seems not to find the PIL module.</p></li>
  </ul>
  
  <blockquote>
    <p>CODE</p>
  </blockquote>
  
  <pre><code>
  import boto3
  import os
  import sys
  import uuid
  from PIL import Image
  import PIL.Image
  
  s3_client = boto3.client('s3')
  
  def resize_image(image_path, resized_path):
      with Image.open(image_path) as image:
          image.thumbnail((500, 500))
          image.save(resized_path)
  
  def lambda_handler(event, context):
      #
      # giving a key error here event['Records']
      #
      for record in event['Records']: 
          bucket = 'mahabubelahibucket1'
          key = record['s3']['object']['key'] 
          download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
          upload_path = '/tmp/resized-{}'.format(key)
  
          s3_client.download_file(bucket, key, download_path)
          resize_image(download_path, upload_path)
          s3_client.upload_file(upload_path, 'mahabubelahibucket2', key)
  </code></pre>