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

Teaming open source development tools with Lambda

Node.js is growing among application developers. Grunt, one of many open source tools associated with Node.js, can facilitate Lambda functions and events.

One of the biggest trends in software development is the advancements in Node.js. At first, running JavaScript...

on the back end was a way for Web developers to write back-end components, but it has since exploded into something much bigger.

I started off as a Python developer, contributing heavily to the Python boto library for interacting with AWS APIs. But I have since moved almost exclusively to Node.js, which offers a lot of easy-to-use open source development tools; the most popular of which is Grunt.

An Introduction to Grunt

At its core, the Grunt framework is designed as a framework to run other commands. It doesn't do very much by default, but there is a massive amount of plug-ins for Grunt to do everything from run a Web server to build applications. Grunt is to JavaScript what "ant" is to Java; the framework provides a good method for developing workflows, but is essentially a way to configure scripts.

To get started with Grunt, developers need to first install the Grunt command-line tool globally and the grunt library locally:

npm install grunt-cli -g

npm install grunt —save-dev

After installing, developers need to create a Gruntfile.js file in their project directory, in which tasks are configured. A good start is to install the "load-grunt-tasks" script to automatically handle importing all Grunt modules that are installed with the Node Package Manager (NPM).

Here's a basic Gruntfile.js file:

var grunt = require('grunt');

require('load-grunt-tasks')(grunt);

grunt.initConfig({

     // Configure specific tasks here

});

Teaming automatic Lambda with open source development tools

Developers using AWS Lambda for more than just a few basic tasks will probably want to be able to commit code to a repository and upload a packaged .zip file. One of the simple open source development tools you can use is grunt-aws-lambda. It is recommended to dynamically configure functions in Grunt to automatically handle uploading functions.

To get started, create two folders in your repository, one called "functions" and another called "events." Next, set up the Gruntfile to load the functions and configure the "lambda_package," "lambda_deploy" and "lambda_invoke" tasks:

'use strict';

var grunt = require('grunt');

require('load-grunt-tasks')(grunt);

 

// Use the filesystem module to dynamically load

// all tasks in the "functions" directory

var fs = require('fs');

var lambda_functions = fs.readdirSync('functions').filter(function(fname){

   if(fname.match(/^[a-zA-Z]+\.js$/)){

      return true;

   }

   return false;

}).map(function(fname){

   return fname.replace(/\.js$/, '');

});

 

// Configure the lambda_invoke, lambda_deploy, and lambda_package tasks

var lambda = {

   invoke: {},

   deploy: {},

   package: {},

};

lambda_functions.forEach(function(fname){

   console.log('Function', fname);

   lambda.invoke[fname] = {

      package: fname,

      options: {

         file_name: 'index.js',

         handler: fname,

         event: 'events/' + fname + '.json',

      },

   };

   lambda.deploy[fname] = {

      package: fname,

      options: {

         file_name: 'index.js',

         handler: 'index.' + fname,

      },

      arn: 'arn:aws:lambda:us-east-1:ACCOUNT_ID:function:' + fname,

   };

   lambda.package[fname] = {};

});

 

grunt.initConfig({

   lambda_invoke: lambda.invoke,

   lambda_deploy: lambda.deploy,

   lambda_package: lambda.package,

   // When deploying, set NODE_ENV to "production"

   env: {

      prod: {

         NODE_ENV: 'production',

      },

   },

 

});

// Register two simple tasks, one called "deploy" to upload all packages

// and one called "test" that just runs lambda_invoke

grunt.registerTask('deploy', ['env:prod', 'lambda_package', 'lambda_deploy']);

grunt.registerTask('test', ['lambda_invoke']);

For this to work, you will need to install the following packages using npm:

npm install grunt grunt-aws grunt-aws-lambda grunt-env load-grunt-tasks —save-dev

Now create a new function in create functions/helloWorld.js.

module.exports = functions(data, context){

     context.done(null, ‘Hello ' + data.name);

};

Then set up the index.js file to include that function:

exports.helloWorld = require(‘helloWorld');

Finally, set up some sample events to test your function in events/helloWorld.json -- this will be passed in as "data."

{ "name": "World" }

To test the function, simply run:

grunt test

And to deploy

grunt deploy

The proper AWS credentials must be configured to deploy, which can be done by setting environment variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. The AWS Lambda functions must be created before developers use this tool to upload Lambda functions. That can be done through the developer console, as developers can choose to create a blank function to upload code directly from Grunt.

Next Steps

Use Node.js for AWS app development

How do I monitor and manage Lambda code?

IT pros look at Lambda, see dollar signs

This was last published in December 2015

Dig Deeper on AWS Lambda

Join the conversation

3 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What open source tools do you use for app development?
Cancel
Google Web Toolkit would be one example off one project. Was chosen because of the cross-browser support.
Cancel
Its good news to hear that Javascript frameworks like nodeJS are finally coming of age.   If they had been around ten years ago, maybe I would not have chosen a different path than pure coding.

Having an open source tool like Grunt, can take a lot of the headache out of figuring out how to setup and deploy applications.
Cancel

-ADS BY GOOGLE

SearchCloudApplications

TheServerSide.com

SearchSoftwareQuality

SearchCloudComputing

Close