Cloud Computing with Amazon AWS Lambda and Node.js

| Posted on November 20th, 2014 by Doug Riches

When you are building native or HTML5 mobile applications choosing the right platform for the
supporting services is essential. The platform you choose must be flexible and scaleable
enough to grow with your business as your app gains popularity. There are many cloud platform
services that fill this role but very few that do it well at a truly global scale.

The Amazon cloud infrastructure is an excellent platform for businesses of all sizes to buildrobust and extremely scalable applications without the investment in infrastructure. Amazonservers are distributed throughout the world and they provide multiple services to ensure thefastest possible response times globally. This isn’t recent news. What’s interesting is some ofthe new services that amazon provides to allow developers to more quickly build outfunctionality without the need to manage the underlying servers.

This month, November 2014, Amazon announced new additions to it’s web servicesoffering. Included in this announcement is AWS Lambda, a fast scaleable cloud computingservice integrated with AWS services and custom event triggers. What sets Lambda apart is that allyou need to upload is a .zip file with your Node.js code and set up a trigger when that codeshould run…that’s it…done.

Imagine a simple app, it allows users to upload an image. The user’s image needs to becropped and converted to greyscale by a server. Amazon for years has provided a great servicecalled S3 for mass storage of files like images etc. Amazon S3 allows you to emit a notificationto listening services in AWS to respond to changes.

There are two main approaches we can take to building out our app:

#1 Build a service:

Developers can spin up a Linux or Windows server virtual instance on Amazon EC2(Elastic Cloud) and write some code to listen to S3 events and respond to changes. Asthe load for the servers increases new instances of the server have to be created tohandle the load. This generation of new servers is called horizontal scaling. In this typeof scaling we are creating new full stack server instances automatically to respond to theload of the app. This is how we have been building cloud apps for a few years, and it’sworked well.

#2 Use a AWS Lambda Function

Instead of provisioning Linux/Windows servers that are running 24/7 to listen for events($$$); Lambda allows you to take away all the server maintenance and complexity. Injust a bit of code and configuration you can create stateless code that is triggeredautomatically without all the hassle of worrying about provisioning scaling andmaintenance. These stateless functions, because they don’t maintain any data, can bespun up into thousands of instances automatically in order to process the incomingevents as required. You are not paying to have a server running 24/7, especially whenthere are no requests coming in.

Currently the AWS Lambda service supports Node.js as it’s only language, with more to comeas the service matures. This is a great use case for Node.js as it’s extremely fast to executewhen written properly, and is easy to learn.

If you have a use case similar to this, the costs to scale can be of great benefit to your business.Instead of running one or more full stack servers 24/7 you are charged only by the compute timerequired for the function and number of requests made. The calculations for this can be a bitconfusing, but Amazon has provided some examples that showcase the cost benefits of thiskind of approach.

When you are looking to build your application out, take a look at what the different servicesoffer. Often we build Amazon Lambda type services ourselves, this new service allows you tofocus on your code and business logic reducing the time required to get apps to market.If you have any questions about this topic, or other Mobile or Desktop Application topics feelfree to call, or ask in the comments below.