FAQ’s On Serverless Image Processing with AWS Lambda and S3

1. Why Two Different S3 Buckets Are Used For Uploaded And Processed Images?

Whenever the lambda function uploads the processed images to the source bucket it will create 2 triggers and the function will process the 2 images once again which then creates 4 triggers. This creates an infinite loop and generates many images, so we created two buckets to prevent an infinite loop.

2. How To See The Logs Of My Lambda Function ?

Go to CloudWatch > Log groups > ImageProcessing . There you will see multiple log streams with their corresponding time stamps. Click on any of them to see the logs.

3. Why Is The Processed Image Not Uploaded Into My S3 Bucket ?

Make sure you followed the tutorial properly. There can be several reasons for the processed image not being uploaded to the S3 bucket.

  1. Check the source code of you lambda function. The value of processedImageBucket variable should be the name of your destination S3 bucket where the processed images are to be stored. No type should be there.
  2. Make sure the bucket policy is configured correctly and it has all the permissions required for the S3 bucket.
  3. Check the timeout of your Lambda function in the Configuration tab. It should be around 30 s to 1 min. If the timeout is low then your Lambda function will be terminated before it can upload the processed image to the bucket.
  4. Make sure that the S3 trigger is set properly and the Lambda function is added to it. Check Step 5 once again.

4. Why Use Lambda Function Instead Of EC2 ?

EC2 instance is continuosly running and charged for the whole time. Lambda function runs only when it is requested. (when an image is uploaded) and it is charged only for the time it is runs. So depending on the use case Lambda function can be more cost effective than EC2.

5. Why Lambda Layer Is Used Instead Of Installing The Dependencies In The Code Itself?

You can include the dependencies in the code itself instead of creating a Lambda layer for it but doing so will increase the size of your code. Using Lambda layers you can create a separate layer for your dependencies and then reuse it in another Lambda function which requires same dependencies which reduces the code size and complexity as you do not have to include the dependencies in all functions. Instead you can just attach the layer to the Lambda functions in which it is required.



Serverless Image Processing with AWS Lambda and S3

AWS S3 (Simple Storage Service) is a cloud data storage service. It is one of the most popular services of AWS. It has high scalability, availability, security and is cost effective. S3 has different storage tiers depending on the use case. Some common use cases of AWS S3 are:

  1. Storage: It can be used for storing large amounts of data.
  2. Backup and Archive: S3 has different storage tiers based on how frequent the data is accessed which can be used to backup critical data at low costs.
  3. Static website: S3 offers static website hosting through HTML files stored in S3.
  4. Data lakes and big data analytics: Companies can use AWS S3 as a data lake and then run analytics on it for getting business insights and take critical decisions.

Similar Reads

AWS Lambda

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. Lambda functions run on demand i.e. they execute only when needed and you pay only for what you compute. Lambda is well integrated with may other AWS services. It supports a wide variety of programming languages....

Serverless Image Processing Flow

User uploads a file to the source S3 bucket (which is used for storing uploaded images). When the image is uploaded to a source S3 bucket, it triggers an event which invokes the Lambda function. The lambda function processes the image. Processed image is stored in the destination S3 bucket. The processed image is requested by the user....

Step 1 – Creating S3 buckets

We will use two S3 buckets:...

Step 2 – Configuring S3 bucket policy

In ‘Block Public Access settings for this bucket’ section disable “block all public access”. You will get a warning that the bucket and its objects might become public. Agree to the warning. (Note: we are making this bucket public only for this project, it is not recommended to make an S3 bucket public if not needed)....

Step 3 – Creating Lambda function

Go to AWS Lambda console. Navigate to Functions section. Click Create Function and name it “ImageProcessing”. Select runtime as “NodeJS 16.x” and architecture as “x86_64”. Leave all other settings as default. Create the function....

Step 4 – Creating Lambda layer and attaching it to Lambda function

...

Step 5 – Creating S3 trigger

Layers in Lambda is used to add dependencies to a Lambda Function. Lambda Layers reduces the code size of Lambda functions as we do not need to upload the dependencies with the function. It also useful for code reusability as we can reuse the layer with multiple functions if they require the same dependencies....

Step 6 – Testing the application

Now we need our Lambda function to know when an image is uploaded to the source bucket. We can do this by adding an event to the source S3 bucket and configure it to get triggered when an image is uploaded to the bucket which in turn invokes the Lambda function....

Why Two Different Buckets?

Upload an image file to source S3 bucket (“serverless-bucket-uploaded-images”). Wait for few seconds and check the destination bucket (“serverless-bucket-processed-images”). There you will see two images (thumbnail and coverphoto)....

FAQ’s On Serverless Image Processing with AWS Lambda and S3

We created two different buckets for this application because whenever the lambda function uploads the processed images to the source bucket it will create 2 triggers and the function will process the processed images once again which in creates 4 triggers. This creates an infinite loop and generated many images, so we created two buckets to prevent an infinite loop....