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.

Some common use cases for AWS Lambda are:

  1. File processing: You can use Lambda for processing files as they are uploaded in an S3 bucket or whenever some event triggers the function.
  2. Data and analytics: You can pass a data stream to your Lambda function and then create analysis from that.
  3. Website: Lambda can also be used for creating websites. This is cost effective because you are charged only for the time when the servers are running.

In this article, we will be using AWS Lambda for processing images.

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....