mot.la

Amazon Lambda Best Practices: Development and Deployment

Amazon Lambda is the new hotness. You can run custom computing tasks at will. It's convenient, but it has two inconveniences.

Inconveniences

First, local development is not friendly: The exports.handler wrapping function, event object, and context object makes running an Amazon Lambda function more complex than simply running node index.js.

Second, deployment is labor intensive: You can't just run a single command to deploy. You need to zip the index.js file and any dependencies. Then deploy that zip file. Furthermore, there is not a good way to specify if that zip file is for staging or production environments.

These inconveniences are solved with best practices.

In this blog post, I show you our best practices at RebelMail for developing and deploying Amazon Lambda functions.

These best practices could change in the future, but currently (December 2014) we are finding them effective. We've strived to make them as easy and automated as possible.


Local Development

Let's get right to our best practice for developing Amazong Lambda functions locally.

1. Clone the node-lambda-template and initialize it.

git clone https://github.com/rebelmail/node-lambda-template
cd node-lambda-template
npm install

2. Edit the contents of event.json and index.js as needed.

{
  "key": "value",
  "key2": "value2",
  "other_key": "other_value"
}
exports.handler = function( event, context ) {
  console.log( "event", event );
  context.done( );
}

3. Run node-lambda run.

./node_modules/.bin/node-lambda run

That's it! Repeat steps 2 and 3 during your development process.

Behind the scenes, the node-lambda module does the hard work of including the event object, context, object, and running the exports.handler function.


Remote Deployment

The following is our best practice for deploying Amazon Lambda functions.

Once again, let's get right to it. (Assumes you've completed Development:Step 1 above)

1. Edit the contents of .env.

AWS_ENVIRONMENT=development
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
AWS_ROLE=your_amazon_role
AWS_REGION=us-east-1
AWS_FUNCTION_NAME=
AWS_HANDLER=index.handler
AWS_MODE=event
AWS_MEMORY_SIZE=128
AWS_TIMEOUT=3
AWS_DESCRIPTION=
AWS_RUNTIME=nodejs

2. Run node-lambda deploy.

./node_modules/.bin/node-lambda deploy --environment staging

That's it! Change 'staging' to 'production' as needed.

Under the hood, the node-lambda module smartly zips up the entire directory (ignoring git files, dot files, and log files yet including dependencies). It then uploads that to Amazon Lambda and appends -environment to the end of the function name.


Conclusion

We developed these best practices at RebelMail after working with Amazon Lambda for a few weeks.

If you find yourself wanting for more, make a pull request or file an issue on one of these repositories.

Finally, I'd love to hear your best practices for development and/or deployment of your Amazon Lambda functions. Email me at mot@rebelmail.com. We're always looking for better best practices.