Benefits of Serverless Image resizing (Amazon S3 & AWS Lamda)
Serverless Image resizing (AWS Lambda and AWS S3)
Here, we have expertise in Lambda where create different image sizes after you uploaded the origin image to the AWS s3 storage.
S3 storage
Amazon S3 stores data as objects within resources called “buckets”. You can store as many objects as you want within a bucket, and write, read, and delete objects in your bucket. Amazon S3 Storage Management features allow customers to take a data-driven approach to storage optimization, compliance, and management efficiency.
Lambda
If you want to create a thumbnail for each image (.jpg and .png objects) that is uploaded to a bucket. You can create a Lambda function (CreateThumbnail
) that Amazon S3 can invoke when objects are created.
Lambda function can read the image object from the source
bucket and create a thumbnail image target bucket (in this tutorial, it’s called the
bucket).source
resized
Let’s setting up the AWS Lambda function
Now that we have everything we need setup, let’s start implementing our lambda function. Since it will be a python code, we will the Amazon linux machine (EC2 instance) where the function will be implemented and packaged and then uploaded to AWS Lambda.
Now , please follow below steps to create new Lambda function.
1.Create Lambda function.
2. Select blue print. [Here we are creating function with Node.js]. Click on Blank Function
3. Now Configure trigger.
A .Select your integration services [like S3, SNS etc..]
Here, we are go with S3 service.
4.Now we select our source bucket where original files are created.
- Select Event Type as Object Created (All)
- Prefix and Suffix are not mandatory here.
- Check Enable Trigger
5. Now configure the function details
- Add your function name
- Now upload zipfile
6. Upload thumbnailer.zip file
[Note: Download “Thumbnailer.zip” file from here: https://github.com/sailyapp/aws-s3-lambda-thumbnail-generator
7. Now create custom role for thumbnail.
8.Now create custom role from here.
- Select create a new IAM Role.
- Write Role Name [Note:- Any text you can enter]
- Click on “Allow”.
9. After create new custom role , it will automatically filled. Please check below screen.
- Then click on text button.
10.Now go to Advanced setting. And set timeout second as “15”.
11.Then finally click on “Create Function” button.
12. Function created successfully.
- We can see that function in “Function List”.
13. Now in S3 in your source bucket folder create “thumbnails” folder.
14. Then give permission to your “thumbnails” and your “bucket” folder.
Select “thumbnails” folder and click on “permission” tab. [check above screen.]
Now give Read/Write to authenticate user.
15. Now add bucket policy. [ click on “Bucket Policy” tab]
-Then save details.
Policy :
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:PutObjectAcl",
"Resource": "arn:aws:s3:::your-bucketname/*"
}
]
}
16. Upload manually file [ jpg] in bucket.
- Same file [ jpg] automatically uploaded on “thumbnails” folder.
- Original size of image is “455.9 kb”. And in thumbnails it save “24.5 kb”
How Thumbnail Image Creation Works :-
Benefits of Lambda function:-
With AWS Lambda, you are charged for every 100ms your code executes and the number of times your code is triggered. You don’t pay anything when your code isn’t running.
byPeople Technologies is top mobile application development company in India. We provide mobile app services in all over world. We have expert mobile app developer.
Recent Comments