Using AWS Lambda in CFML: Creating a NodeJS Lambda Function that You Can Call From CFML
Posted 20 February 2019
While the FusionLess project is awesome in its potential to write and deploy CFML to AWS Lambda, this post will focus on calling a non-CFML Lambda function from within your CFML application.
To that end, I’ll show you how to create a very basic NodeJS function and get it running inside Lambda.
The AWSPlaybox NodeJS Lambda Function Example
My AWSPlaybox application has the code for a simple, NodeJS-based function which we can call, and get a response from, in CFML. All of the code can be found in the /nodejs/lambda/returnDataToCaller.js file:
Let me break this code down a bit:
Lambda expects a single function to be made available from your code. That’s the function it will run when the Lambda function is invoked by a call from your CFML application (or from any number of event sources in AWS). The de facto standard name for this function is “handler,” as evidenced in the first line of code above. You can name the function whatever you want, but most people go with “handler.”
The handler function takes three arguments: the event object, the context object, and a callback function.
- The event object contains all the data that gets passed to the function during invocation, provided in JSON format.
- The context object contains information about the current Lambda runtime, and is best left alone.
- The callback is, essentially, the invoker of the function. In this case, it’s your CFML application.
In this case, the event object contains:
- firstName [ string ]
- lastName [ string ]
- email [ string ]
- classes [ array ]
We pass in all of this data via our CFML Lambda function invocation, which we’ll see later in the series.
The rest of the function simply loops through the array of classes in the event object, and logs each class to the console. In a real application, you could write each of these records to a database.
Finally, we return a string built from the data found in the event object to our calling CFML.
Running the AWSPlaybox NodeJS Example in Lambda
As the AWS Console is constantly changing, I’m not going to provide extensive instructions on getting this function running inside of Lambda. AWS has a very clear tutorial on how to run a simple function in Lambda. Although the AWS example uses Python, you’ll set your runtime to NodeJS. The Handler function is “index.handler”. You don’t need to add any triggers and you don’t need to set any other configuration options beyond the defaults.
To enter the actual code, use the AWS Cloud 9 Editor that’s built into the Lambda console. You can literally copy and paste the code from the /nodejs/lambda/returnDataToCaller.js file.
As a side note, Cloud 9 is a pretty darn good cloud-based editor. I do almost all my Lambda development work in Cloud 9, as it has integrated debugging for NodeJS Lambda functions. It’s quite capable, robust, and easy to use.
Testing the AWSPlaybox NodeJS Example in Lambda
One of the nice things about developing functions in the Lambda console is that you can provide test data for your Lambda function from within the console itself, without having to call that event from a real, production source.
If you want to set up a test event, copy and paste the JSON below into the test event setup window:
Click the “Test” button with this sample data selected, and you’ll see the results of the Lambda function execution.
Alternatives to Developing Your Lambda Functions in the Console
Developing in the AWS console is a great way to get started, and to learn about the details of the service. If you need to rely on NodeJS packages in your Lambda function, though, you’ll need to develop locally, zip up your JS file(s) and the node_modules folder (but not the folder in which your JS and node_modules reside!), and upload that .zip file to either the Lambda console or a location in a S3 bucket. This makes for tedious development cycles, but is the only way to use NodeJS packages.
In the long run, though, you’re going to want to abstract away a lot of this manual work and, ultimately, move toward an automated build and deployment pipeline. Fortunately, Lambda has two mature frameworks for exactly this:
Covering either of these frameworks is beyond the scope of this post, but once you understand how to write and deploy a function in the AWS Lambda console, it’s absolutely worth your while to learn about — and use — these frameworks.
The Serverless framework has a robust ecosystem of plugins that can make complex serverless functions a lot simpler to build and deploy. SAM is maintained by AWS, has a CLI, and has the ability to test Lambda functions locally. A Google search for “Serverless framework vs SAM” will return a lot of discussion on the topic.
Both frameworks ultimately focus on accessing your Lambda functions via http calls to the AWS API Gateway, rather than direct invocation, which is what we’re doing in this series. As such, direct invocation is what we’ll cover in the next post.