All Ruby on Rails Node JS Android iOS React Native Frontend

3 AWS Tricks for Your Business - Lambda, Serverless and Cloud Functions

Node.js, Lambda, Serverless and some cloud functions - the perfect mix for building a scalable app that’s easy to manage and doesn’t have many vulnerable dependencies. Also, it’s not expensive at all. Find out more about how much money you can save in our introduction to this technological combo.

Node.js

Thanks to its unique I/O model, it excels at the sort of scalable and real-time situations we put our servers in increasingly often. It’s also lightweight and efficient. The ability to use Javascript on both the front end and the back end opens new possibilities.

Lambda

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

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.

Usage Examples

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!

1-snippet.png

In one short Javascript file, we wrap up our heavy image processing logic and push it as a Lambda function to AWS by using the Serverless deploy command. When a user needs to resize their avatar, for instance, the only thing we need to do is send the user’s avatar image to Lambda via the API gateway and receive the resized avatar as a callback.

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.

The last thing we need to do is to create a logic for storing images on S3. We strongly recommend using the AWS-SDK for JavaScript in Node.js. This way, we can easily handle the image-storing functionality and improve our Lambda.

2-snippet.png

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:

3-s3-pricing.png

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.

By using the AWS SDK for JavaScript, we can connect directly to the Rekognition module (AWS.Rekognition()) and use it anytime we want. The same goes for our new non-relational database feature. We just need to use DynamoDB from the AWS package, send our data to AWS, and it’s done. We can follow the steps from Example 2, implementing (AWS.DynamoDB()) and using the method put.

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.

4-lambda-pricing.png

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.

Conclusions

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.

New Call-to-action
Code stories CTA
READ ALSO FROM DevOps
Read also
Need a successful project?
Estimate project or contact us