Tutorial: Create an AWS Lambda Function with Node.js®

Photo by Helloquence on Unsplash

Amazon Web Services (AWS) is currently the most prominent provider of serverless technologies with support for over 150 services. AWS Lambda is Amazon’s version of Functions as a Service (FaaS). With a FaaS like AWS Lambda, a developer can write small pieces of functionality that can be deployed to the cloud servers and run on an as needed basis. With this design, you only have pay for the server time you use, which is great. So how do you get started with AWS Lambda? Note, for this tutorial, we will be using Node.js®.

There are several items you will need to setup before you can create a lambda function in the cloud. If you have not already done so, please follow the instructions in the AWS documentation to setup an account, install the command line interface (CLI), and configure the CLI. Additionally, you will need to install npm and Node.js®.

There are three ways to create a lambda function directly with AWS: 1.) using the AWS console, 2.) using the AWS CLI, and 3.) using the AWS Software Development Kit (SDK). Below we will walk through the steps to create a lambda with basic configuration using each tool. If, at any point, you would like more information about the AWS IAM or AWS Lambda services we will be working with, please refer to the AWS documentation:

AWS Identity and Access Management CLI
AWS Identity and Access Management SDK
AWS Lambda CLI
AWS Lambda SDK

Using the AWS Console

  • Login to AWS, and click on the “Services” menu in the top, left-hand corner of the navigation bar.
  • Navigate to Lambda by either typing “Lambda” into the search bar or finding Lambda in the list of “Compute” services.
  • From the “Dashboard” or the “Functions” webpage, click on the “Create function” button.
  • With “Author From Scratch” selected, fill out the form with a function name, the node.js 8.10 runtime, and a role. The role determines the permissions the lambda has to access other AWS services.
  • Choose “Create a custom role”. This option by default will open a new window and fill in the default options for the new role. The default option is called “lambda_basic_execution”, which permits lambdas that use this role to interact with the Cloudwatch service for logging purposes. In the future, you will not need to recreate this role. You can simply select “Choose an existing role” in the third form field and “lambda_basic_execution” in the fourth form field.
  • Click “Create function”.
  • Now the lambda has been configured. You can scroll down to the “Function code” and edit the function itself between lines 2 and 7.
  • And that’s it! Your lambda is ready to be called.
  • To run your lambda function, you will need to create a test event by clicking the “Test” button at the top of the webpage. Once created, your test event should be selected. Click “Test” again, and view the execution result by clicking “Details” in the message that appears.

Using the AWS CLI

  • First, create a JavaScript file for the new lambda function (e.g. exampleLambda.js). It is typical for AWS Lambda functions to use the exports.handler convention.
  • Zip the file. From the directory where the file exists, you can run zip exampleLambda.zip exampleLambda.js if you are using a unix-based operating system such as Mac OS.
  • Next, you will need to get the Amazon Resource Name (ARN) for the IAM role you will give the new lambda function. If you followed the “Using the AWS Console” walk-through above, you will already have a role in IAM named “lambda_basic_execution”. Use the following command to get the data about this role and note the “Arn” value in the returned JSON object. You will use this value when issuing the create-function command later. In the example output below, the xxxxxxxxxxxx value will be your account number.

get-role command:

output:

  • If you have not created a role for basic lambda execution before, you can follow these steps to do so using the CLI:
  • Create a file called trustPolicy.json with this JSON object, which will allow a lambda to assume the new role you will create shortly.

Use the create-role command to create the role with the policy file above.

  • Then attach an AWS managed policy called “AWSBasicLambdaExecution” to the new role.
  • Now you have everything you need to create the lambda function in the cloud. A couple things to note:

a) The “handler” argument is the name of method that is executed when the lambda function is invoked (in this case, “handler”). This value should be formatted as fileName.methodName.

b) The zip file is a blob value and requires the “fileb://” prefix.

c) Replace the xxxxxxxxxxxx value with your account number (without dashes). Your account number can be found in the navigation bar in the top, right-hand corner of the AWS webpage.

create-function command:

  • Congratulations, your lambda has been created! To run your new function, use the invoke command. The last argument is the name of a file where the execution result can be recorded.

Using the AWS SDK for Node.js 8.10

  • Again, you will need to start by creating a JavaScript file for the lambda function (e.g. exampleLambda.js). It is typical for AWS Lambda functions to use the exports.handler convention.
  • Then, zip the lambda file. To zip a file programmatically, you can use the execSync method from the Node.js child_process module to issue the zip command.
  • Next, you will need to create an IAM role that you eventually will give to the new lambda so it has basic execution permissions. If you followed the “Using the AWS Console” or “Using the AWS CLI” walk-throughs above, you should already have an IAM role named “lambda_basic_execution”, and therefore, can skip the instructions in the next two bullet points.
  • Making this AWS role is a two step process that starts with the createRole IAM method.
  • The role now exists, but you still need to give it permissions. Attach an AWS managed policy called “AWSBasicLambdaExecution” to the “lambda_basic_execution” role.
  • Finally, use the createFunction() method to create the lambda function in the cloud. You will need to input your account number (without dashes) and your default region. Your AWS region can be found in the navigation bar in the top, right-hand corner of the AWS webpage. Once you have identified the plaintext version of your region (e.g. US East N. Virginia), use this reference table to get the value you need to use for the code snippet below.
  • Congrats, your lambda now exists in the cloud! To run your lambda function, use the invoke() method.

Note: If you would like to run all of the AWS SDK methods in a single script, you need use JavaScript callbacks or promises in order ensure subsequent methods are not called before a response is received from AWS. However, when performing a sequence of asynchronous operations, it is advantageous to use the async/await pattern instead because it makes the code easier to maintain.

The BAM! framework uses these and other AWS SDK methods to quickly and programmatically create lambda functions. Check it out at bam-lambda.com!