Having spent some time writing several Node.js Lambda functions, I thought it would be useful to document the packaging / deployment steps involved for reuse. This article is not a complete introduction to AWS, nor will it detail the steps involved in creating a function, instead we focus on subsequently publishing a function. This article will make use of CoffeeScript for aspects of the work.
We will be using the AWS SDK to deploy code from our development system to Lambda, which requires that we provide credentials for an IAM User with appropriate permissions. The AWS SDK provides several options to load credentials, listed below in order of recommendation:
aws_access_key_id = <ACCESS_KEY>
aws_secret_access_key = <SECRET_ACCESS_KEY>#### A Word on IAM Policy
In order for the AWS SDK to deploy your code, you must attach an IAM policy with an appropriate set of permissions to the User (“lambda”) that will deploy the code. For our purposes, the following policy provides the least permissions required (be sure to attach this policy to the User):
}### Create NPM Package
Next, we will create a new NPM package for our sample project, which we will call “my-sample-lambda-function”.
npm initOpen the package.json file in your favorite text editor and add the following:
“description”: “A simple continuous integration sample.”,
}Before we move to wiring our packaging and deploy tasks, be sure to install dependencies via the command line:
npm install### Gruntfile
Before we start writing our Lambda code, let’s set-up our build and deployment process using Grunt (how to install Grunt on Mac). Create a file named Gruntfile.coffee at the root of the my-sample-lambda-function directory:
module.exports = (grunt) ->
# compile all CoffeeScript files to a single JS file, index.js
grunt.loadNpmTasks(‘grunt-aws-lambda’) # simplify packaging and deployment
grunt.registerTask(‘package’, [ ‘coffee’, ‘lambda_package’ ])
grunt.registerTask(‘deploy’, [ ‘package’, ‘lambda_deploy’ ])The exact details of the Gruntfile are beyond the scope of this article.
Before you can deploy your function, you will need to create the Lambda function via the AWS Console or CLI with handler named “index.handler” (note that our policy above does not allow the “lambda” user to create a Lambda function). Grab the Region and ARN for the Lambda and paste into the Gruntfile as appropriate.
The Gruntfile includes two tasks to simplify packaging of the function and deployment: “package” and “deploy”. These functions make use of the grunt-aws-lambda plug-in for Grunt to easily create the Lambda package and push to AWS.
Now that we have laid the groundwork for deploying to Lambda, we can write some code. Our example is a “Hello World” application, my next installment will cover utilizing the AWS SDK.
Create a new directory named “src” with a file named “index.coffee”:
touch index.coffeeOpen index.coffee in your favorite text editor and insert the following:
console.log ‘Loading function’exports.handler = (event, context, callback) ->
# echo back ‘Hello World’
callback null, ‘Hello World’
returnNow, launch a command prompt and deploy to Lambda:
grunt deployIf successful, you will see the message:
Done, without errors.### Conclusion
At this point, we have a nice base to develop, package, and deploy code to AWS Lambda that we will use to build additional functionality in the future.