AWS CloudFormation Lambda Node.js Twilio Function – Part 1

A

Many web applications have a need to send and receive SMS messages. In this post, I am going to walk though the needed steps and code to setup an AWS CloudFormation template to build a simple AWS Lambda function written in Node.js and an AWS API Gateway to receive SMS messages from Twilio.

As part of this post, I am making some assumption regarding your development environment. I will assume that you have some familiarity of the AWS Command Line Interface and knowledge of Node.js and Javascript. Much of this post is based on and existing AWS tutorial called “Create Your Own Serverless Application“, but I will be adding examples and details when I think it makes sense.

Hello World Lambda Function

I will start with everyone’s favorite helloWorld example. While this will do little more the print “Hello World”, it will make sure that your AWS Command Line Interface is setup correctly and that you are able to use CloudFormation to build and deploy the code later in this project.

Start by creating a new folder for your project and within that directory we will create 2 files: index.js and template.json. The index.js file contains the Javascript code for the Lambda function and the template.json is the CloudFormation template that will create the resources needed to run your applications.

'use strict';

exports.helloWorld = function(event, context, callback) {
    callback(null, 'Hello World');
};
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "A simple Lambda function to receive and store SMS messages from Twilio",
  "Resources": {
    "HelloWorldFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.helloWorld",
        "Runtime": "nodejs6.10"
      }
    }
  }
}

Once these files are created, we will package the JavaScript contents in a zip file, upload it to an S3 bucket and make a reference to this zip file in the template. This is completed by opening a command prompt and running the command:

aws cloudformation package \
    --template-file template.json \
    --s3-bucket S3_Bucket_Name \
    --output-template-file packaged-template.json \
    --use-json

This creates a packaged-template.json file with a reference to the zip file that the package command creates. This template file can now be deployed to AWS using the command:

aws cloudformation deploy \
    --template-file packaged-template.json \
    --stack-name your-stackname-here \
    --region us-west-2 \
    --capabilities CAPABILITY_IAM

You should now be able to open the CloudFormation Service in your AWS Console and you should see your stack listed there with the status “CREATE_IN_PROGRSS”. When the status changes to “CREATE_COMPLETE” you should now have a new Lambda Function called “your-stackname-here-HelloWorldFunction-XXXXXXXXXXXX” and a new Role called “your-stackname-here-HelloWorldFunctionRole-XXXXXXXXXXXX”. You may now manually run the Lambda function, and it should output “Hello World”

API Gateway Interface

Assuming everything above worked as expected, you can now add an API Gateway interface to the HelloWorld function. The API Gateway will allow the Lambda function to be called from an external URL. Below are the updated index.js and template.json

'use strict';

const createResponse = function(statusCode, body) {
    return {
        statusCode: statusCode,
        body: body
    };
};

exports.helloWorld = function(event, context, callback) {
    callback(null, createResponse(200, 'Hello World'));
};
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Description": "A simple Lambda function to receive and store SMS messages from Twilio",
  "Resources": {
    "HelloWorldFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.helloWorld",
        "Runtime": "nodejs6.10",
        "Events": {
          "GetResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/hello-world",
              "Method": "get"
            }
          }
        }
      }
    }
  }
}

Then run the two command line scripts above (“aws cloudformation package …” and “aws cloudformation deploy …”). Upon completion, you should now have a new API Gateway that you can test using the Gateway Console and it should return “Hello World”

In Part 2, I will walk though the steps to save post data to a DynamoDB Table.

About the author

By mark

Recent Posts

Categories