alphaspirit - Fotolia

Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

AWS Node.js integration eases app development

Whether using Lambda on the front end or DynamoDB on the back end, AWS provides an SDK that eases use of Node.js for application development.

The advantages of Node.js, which uses the familiar JavaScript language, have cloud developers interested in its prospects -- even though it's still in beta. In this tip, we'll dive deeper into why Node.js is so popular in cloud computing environments, specifically in AWS.

One of the most beneficial aspects of Node.js is how easily it integrates with the Amazon Web Services (AWS) software development kit (SDK). AWS Node.js use was spurred when Amazon released an SDK for the language. AWS was later able to use that exact same SDK to work in the browser, meaning that the same code can run on either client- or server-side and connect to AWS.

Developers can access DynamoDB on the back end to process data and have a client on the front end who can access that same DynamoDB record.

With the AWS SDK, IT teams can focus on development instead of managing the middle-logic of communicating to the server just to get data from a database. How many times have you written a simple wrapper back-end handler just to run an SQL query? The AWS SDK for JavaScript enables a rich front-end application to communicate directly using a database as the middleman.

Front-end code calls for Lambda functions

AWS Lambda can run a completely serverless application. It can give a front-end application access to call Lambda functions on specific events and get responses to those function calls. It's like being able to do remote procedure calls without having to run the servers.

Customers only pay for processing time and don't have to manage resources. Lambda eliminates concerns over scalability and what instance class runs services, as AWS automatically manages that. If there is an hour in which a million users simultaneously hit a company's servers, Lambda seamlessly auto scales to meet that demand.

Figure 1

Figure 1 shows how this sort of application would look. End users would load a browser application from Amazon CloudFront and the call functions directly within Amazon Lambda, using Cognito to get a set of temporary access credentials. The Lambda function could interact with any number of Amazon services. This example uses DynamoDB for key-value storage, such as session metadata or looking up user access.

Instead of something like this:

$.post(‘/purchase', data);

The user calls something like this:

lambda.invoke({

  FunctionName: ‘buyBook',

  Payload: data

}, function(err, data){

 

});

The "buyBook" function would need to be created in the AWS Lambda console, instead of having to run a server to listen for the "/purchase" URL.

The Lambda function can be Node.js, so the back-end component could be written in JavaScript. Developers may need to write Lambda functions to interact with the database for security reasons -- making sure the end user doesn't change things he shouldn't, tracking purposes, or to interact with services not accessible directly from the front-end client. For example, you may want to write to a SQL database, which is not accessible from the front-end service, as it shouldn't be. Having those services directly writable or even readable by the client could lead to security problems, as you can't guarantee that the end user hasn't manipulated the code. It's important to know the difference between which code is safe to run on the front end and which code needs to run on the back end.

Remember: Don't trust the front end. Because the front end can be changed, developers shouldn't assume that the request came from source code. If a user needs to write transaction information such as payments, that must be done through Lambda. If he is just setting preferences, that can be done on the front end.

Back-end coding on AWS

Not everything can be done within AWS Lambda; tasks that require access to third-party programs that aren't written in JavaScript are one exception. For example, I wrote a program that generates PDF documents from webpages, which uses "wkhtmltopdf." This is a command-line tool wrapped in a JavaScript library, which is not available in Lambda. To support this, I run Docker in EC2 Container Service (ECS); I have a private Docker hub running as well as a service within ECS, and I can deploy new containers based on a simple Dockerfile. This Dockerfile builds off of the node:0.12 Docker image.

Keeping up to date

It's important to keep up to date on the numerous releases of the core Node.js platform as well as individual packages. Recently, I noticed a large problem with one of my microservices that caused automated Docker builds to fail. The problem was that a new release of the Node Packet Manager (NPM) was no longer accepting "" for package versions. This caused the dependency installation process to fail, thus the image could not be built and the Elastic Beanstalk service stopped.

Upgrading to a new version of the dependency that had the blank version in it solved the problem. That version also needed a newer version of Express, which had made some breaking changes with a major release, causing even more problems. After digging around, I settled on reverting to a specific Docker build of node 0.12.2 until I could bring everything up to date.

It takes an event like that to remember that yes, Node.js is still in prerelease, and it's evolving on a daily basis. It's important to keep up with those changes. After pushing out a new release, a developer should take a few days to make sure everything updated correctly. It will save a headache down the road.

When building locally, I use Node Version Manager to switch, test and build out things in different versions of Node. This helps me ensure I'm always running on the same version of Node as my productions services. Check the "NPM outdated" function for the latest versions of packages in the package.JSON file.

Next Steps

Benefits of developing with Node.js in AWS

AWS Lambda has IT pros seeing dollar signs

Shear time off cloud development with AWS Lambda

Dig Deeper on Java development

Join the conversation

3 comments

Send me notifications when other members comment.

Please create a username to comment.

Have you used Node.js code in Lambda functions? If so, how?
Cancel
What about nvm capabilities on AWS : to work with several versions of node libraries ?
Cancel
Nice article. Always looking to know more ways of doing things.
Cancel

-ADS BY GOOGLE

SearchCloudApplications

TheServerSide.com

SearchSoftwareQuality

SearchCloudComputing

Close