Lambda is like the brain of the node-Lambda-Serverless organism. It allows you to encapsulate complicated project features within a computing platform and runs them only when a specific event is triggered. Lambda functions carry out the whole Amazon Web Services magic: beautiful documentation with clear examples, scalable features, cloud storing, image recognition, and a lot more!
Serverless is a cloud computing framework. It comes with a service that allows you to develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require. It could be used to accomplish jobs via composed Lambda functions.
At Netguru, we use AWS often. The most frequent use case are features such as s3 storage and RDS as database services, CloudWatch with whole Management Tools for fast and clear application monitoring, and, of course, Amazon ES (Elasticsearch service).
We will use the following simple setup from Serverless’ main page with node.js as our backend:
- Set up the Serverless framework in three easy steps;
- Serverless.yml file: it’ll contain paths, configuration options and the AWS API gateway; without the gateway, we wouldn’t be able to communicate with our Lambda;
- Handler.js: our main work file, it will hold the function logic;
- After the setup and exporting our AWS credentials, our Lambda is ready to go!
Example 1: Lambda
We need a feature for resizing images, e.g. changing image width to 100px. We want to delegate it to a fast and scalable service, which will only be used when a certain event is triggered. With the setup steps above, we configured a boilerplate for our resizing logic. Now we can just use imagemagick, a node.js package with a resize function. And that’s it!
The biggest pros of this solution:
- Even if 10,000 users try to resize their avatars at the same time, Lambda will rescale and respond to all those requests. Lambda doesn’t know what server overload is.
- One request costs us exactly 0.0000002$.
We’ve created a simple Lambda function for resizing images by sending them via the AWS gateway. Our project has received a new low-cost Lambda micro-service.
Example 2: Serverless
Now we want to add the feature for storing images in S3, the AWS database, and in return, we want to receive a link to the resized picture stored in the DB. This will take two additional steps:
- we need to add AWS S3 configuration in our Serverless.yml file,
- we need to set our S3 bucket’s name in there,
- we need to tell Serverless to use it in our yml function definition through the ‘bucket’ keyword.
S3 costs vary by region, but in our example, we choose the EU (London) region. The pricing for will be somewhere in the region of the following:
By now, our project has expanded its functionality and kept its complexity by transferring the s3-image function to the Serverless-Lambda feature.
Example 3: Cloud Functions
Now it’s time to expand the project’s functionality with image recognition and save the results in a non-relational database. Recognition is a relatively new cloud-computing technology, but AWS offers a dedicated service for it. All we need to do is connect our Serverless-Lambda with the new services.
Once again, we’ve expanded the project’s functionality by using only the AWS SDK for Node.js. This example shows that a project doesn’t need to be bloated and unwieldy to have complex functionalities. In one lightweight Lambda function, we were able to contain the logic for uploading, processing (recognition) and receiving image data. All server logic remained in Amazon’s capable hands.
The last thing – pricing. Our project will be charged for processing images. We will need to pay $1 for 1,000 images, if we process up to one million of images each month. As the volume of images grows, this cost will be decreasing.
Without overly complicating our lives, we’ve solved many problems and complexity dependencies only by using our Node.js, Lambda and Serverless mix. We have everything in one place now, and we will be ready whenever users come up with new ideas.
There is no limit to connecting AWS modules together by using Serverless and Lambda. That’s the real power of Lambda functions. We recommend that you get rid of all excessive complications from your code and use the winning combination of Lambda, Serverless and cloud. This will improve your project’s features and performance, and save you a lot of headaches.