Through AWS Lambda, AWS is completely changing the entire web industry, again. If you're not familiar with Lambda, it's a new AWS compute service that manages the AWS resources for us. Said differently, it allows developers to write their applications without having to manage servers. It promises scalable, serverless, event-based code execution with micro billing. For example, if you're a developer, just think about only having to write the methods needed to complete your app, uploading them to AWS and only paying for the time it takes to execute the functions.

Think JSBin or Codepen but on steroids. Serious.

With Lambda, you no longer have to manage Express, SailsJS, Java Spring, Django, Elastic Beanstalk, EC2, Ubuntu, NodeJS, Apache httpd, Nginx, Apache Tomcat, apt-get, pip, yum, nothing. Goodbye!

To top it off, Lambda can auto-scale without requiring you to even spin up a new server. This means that it could become a decent competitor to Erlang, Elixir, Phoenix framework, Functional programming. Wow!

Getting Started

The Lambda workflow is pretty simple to understand.

  1. Some event triggers a Lambda function.
  2. Some code runs as a result of the Lambda function being triggered.
  3. Some actions happen as a result of the Lambda code being run.

Step 1 - Identity Access Management

Before you start any development, please make sure you've created a user with an Access Key and Access Secret. This tutorial will show you how to do that.

You will also need to assign a IAM Policy File to you, the user. To keep things simple, for now, simply add AWSLambdaFullAccess.

Lambda Full Access Policy

Note, this is different from creating a Role and assigning policies to that role. This is about give you, the developer, the necessary permissions to later use the AWS command line interface.

Step 2 - AWS Command Line Interface

There are two ways to do Lambda development. The first way is through the AWS Lambda Web Console but the better way is through the AWS Command Line Interface. The command-line interface is the only way you can include 3rd party npm packages such as faker, lodash or Bluebird.

Step 3 - Spot Check

Once you've completed Step 2 and you've also configured your AWS client, then try running this function.

aws lambda list-functions

If you get an error describing that you don't have access, then it probably means that you need to add a policy to your user.

Creating a Lambda Function

Time to create a Lambda function using NodeJS.

Step 1 - Create a Function

nano helloWorld.js

Paste this code inside of getStatus.js

exports.handler = function(event, context){
  //A. Event tells us what triggered this function
  console.log("event: ", event);
  //B. Context offers a few things (see below)
  context.done(null, "Hello World");

A. event describes the external stimulus that your function is responding to. For example, if a file within an AWS S3 bucket changed and triggered the function, this event would include the bucket name and the object key.

B. context helps you know when the function itself, or any callbacks, have finished executing.

context also gives you useful information and access about the Lambda environment. For example, you can use context to find the AWS CloudWatch log stream associated with the function. Or, use the context.client property to learn more about who invoked this function.

Step 2 - Package a Function

Lambda requires you to zip up javascript files. You can specify which files you want to zip.

zip helloWorld.js

If you're planning to use npm and include 3rd party node_modules, then you also also zip a bunch of files at once.

zip -r helloWorld *

Step 3 - Prepare for Publish

This is the part where we create a IAM Role so that Lambda can execute our function no our behalf. Since our Lambda function will only connect to CloudWatch, then we only need to assign CloudWatch permissions to our Policy.

Here's what the final result should look like.
Visit the Role section of CloudWatch and create a new role. Include a Policy that include AWSLambdaExecute.



Lambda Final Review

Step 4 - Publish

The three Lambda commands you'll use regularly are:

  • create-function allows you to upload and create new functions.
  • update-function-code enables you to modify code you've already created.
  • update-function-configuration is great for modifying the memory size, timeouts, etc.


Upload a Function

This is an example of how to upload a function to Lambda using command line interface.

aws lambda create-function \
--region us-east-1 \
--function-name helloWorld \
--zip-file fileb://path/to/zip/ \
--role arn:aws:iam::233860676076:role/BasicExecutionRole \
--handler helloWorld.handler \
--runtime nodejs4.3 \
--timeout 3 \
--description "a simple explainer" \

Take note that --role matches the execution role we created earlier.

Lambda Role Details

--handler is the name of the javascript file we created helloWorld.js.

Invoke a function with arguments

This example will invoke the helloWorld function we created above and pass a text file full of arguments.

aws lambda invoke-async \
--function-name helloWorld \
--invoke-args /path/to/file/with/arguments.txt \

Get the metadata about a function

If you want to know where the URL of your function exists, try this command

aws lambda get-function --function-name helloWorld

Update a function

You need to prepend fileb:// to the path of your local zip file.

aws lambda update-function-code \
--function-name helloWorld \
--zip-file fileb://path/to/zip/ \

Delete a Function

aws lambda delete-function --function-name helloWorld

Double check your work through AWS CloudWatch

Cloudwatch allows you to see a log of all events happening on your function.

My Cloudwatch from Region:Oregon


The AWS command line interface offers everything you need to publish your lambda function. If at any point you get stuck, type aws lambda help and you'll get a list of available functions.

aws lambda help