August 14, 2020 / Nirav S
Do you know that you can create thumbnails for images stored in S3 dynamically? If you didn’t know this and we looking to do so, then we will show you how we can create thumbnails using a simple AWS Lambda function.
We have created two buckets in which one bucket is the source bucket and the second one is the destination bucket.
Above is bucket one is named “test2571961” (the source bucket).
Bucket number two’s name is “test2571961-resized” (the destination bucket)
And we just added an image in bucket number one. (test2571961”)
Now create the Lambda function,
The role gives permission to lambda to access S3 to read and write the images.
Now click on “ create function”
Configure an Amazon S3 Bucket as a Lambda Event Source.
First Click “Add trigger” and then configure
Select a trigger: S3
Bucket: Select your bucket (test2571961)
Event type: All object create events
Scroll to the bottom and click “Add” Click on “create thumbnail”
Now it’s time to configure the Lambda function.
Scroll down to the Function code and configure the following settings
Code entry type: Upload a file from Amazon S3
Runtime: Python 3.7
Click Save at the top of the window Your Lambda function has been configured now,
Trigger a Lambda function by uploading an image to Amazon S3
See the below template will be displayed that shows the event data sent to a Lambda function when it is triggered by upload into Amazon S3.
now time for the final stage, Click “Create” and then click “Test”
Now add some images on the source bucket and you will see logs on above. Once you get the success log, go to the S3 test2571961-resized bucket and you can see the resized image in it.
Source bucket image,
Resized or target bucket image,
Finally, we are ready with our dynamically resized image. For more AWS related technical information, bookmark our blog.
Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.