This walk-through is designed to help newcomers become familiar with using an AWS Lambda function in combination with an S3 bucket and IAM roles. The walk-through assumes you already have an AWS account and some familiarity with AWS services and the console. All of the information in this post was recycled from the work of previous authors, and full credit goes to them.
I’m particularly indebted to the blog post by Nidhin kumar, and I reuse all of his work in this post. Note that, as Nidhin’s zip file is hosted in the US West Oregon region (us-west-2), we also need our Lambda function and S3 buckets to live in this same region.
Let’s get started with AWS S3. In the console, create two new buckets. Take note of their names; let’s call them something like
mybucket-resized. Make sure to give each one public access privileges using a bucket policy similar to the following:
Now hop on over to IAM in the console; let’s create an IAM role that will allow our S3 bucket to talk to the Lambda function we’re about to create. In the lefthand navigation pane click
roles and then
Create role. Find
Lambda and then click
next: permissions as follows:
Create role dialogue window, look for the following options and click the box next to them:
Once they’re selected, click
next: Tags and then
next:Review. Give your role a name, as follows, and click
Back in the IAM Roles dashboard, you should now be able to search for and view that role you just created, as follows:
We’ve set up our buckets and our IAM role, so now we’re ready for Lambda. In the AWS console, select
Lambda and then
Create Function. Under “Basic Information” give it a name and select
Python 3.7 as the runtime, like this:
Click the dropdown arrow for
Choose or create an execution role and under
Use an existing role search for the IAM role we just created. Then click
Create Function, as follows:
Next we’ll configure our S3 bucket as the Lambda event source by adding a trigger. Click
Add trigger , select S3 and then look for the bucket we created earlier. Leave all the defaults and click
You should now see the trigger added to your function
thumbnail-creater in the Designer window, like this:
Click on the function name to open the Function code dialogue window, and under
Code entry type select
Upload a file from Amazon S3. Leave the runtime as
Python 3.7 and change the Handler to
CreateThumbnail.handler. You must copy this handler exactly as-is or the function will not work, as it refers to the filename.handler-method value embedded in your function code. In the cell beneath
Amazon S3 link URL paste the following link, provided courtesy of Nidhin kumar:
Note that, as Nidhin’s zip file is hosted in the US West Oregon region (us-west-2), we also need our Lambda function and S3 buckets to live in this same region.
Save at the top of the Lambda console. Our function is ready to go — let’s test it out!
Back in the S3 console, navigate to the first bucket we created
mybucketand upload an image file using the simple upload tool. Note the size of the image (in my case, it’s 7 KB).
Now that the file’s there, navigate over to the second bucket
mybucket-resized. The same image has appeared here too, thanks to the magic of Lambda! Not only that, but it’s been resized to only half its original size (3.5 KB in my example).
You can download my image from this URL if you want to check it out:
Finally, back in the Lambda console, click on the same function as before and then click
Monitoring. Here you’ll see the CloudWatch logs showing that the function has been invoked one time, for a duration of about 1 millisecond
You’ve successfully done the following in this walk-through:
- Created two S3 buckets with a public-access bucket policy
- Created an IAM role that gives Lambda permission to invoke from S3
- Created a Lambda function that is triggered each time an image is uploaded to your first bucket, resizes the image and then saves it to the second bucket.
- Monitored the logs of your function using CloudWatch.
That’s it! Congratulations for completing this walk-through.
youtube recording: https://youtu.be/gZEVIIbuCjc