AWS SDK for JavaScript
Developer Guide for SDK v2

Tutorial: Creating and Using Lambda Functions

In this tutorial, you learn how to:

  • Create AWS Lambda functions in Node.js and call them from JavaScript running in a web browser.

  • Call another service within a Lambda function and process the asynchronous responses before forwarding those responses to the browser script.

  • Use Node.js scripts to create the resources needed by the Lambda function.

The Scenario

In this example, a simulated browser-based slot machine game invokes a Lambda function that generates the random results of each slot pull. Those results are returned as the file names of the images that are used to display to the user. The images are stored in an Amazon S3 bucket that is configured to function as a static web host for the HTML, CSS, and other assets used to present the application experience.

This diagram illustrates most of the elements in this application and how they relate to one another. Versions of this diagram will appear throughout the tutorial to show the focus of each task


                    JavaScript running in a browser that invokes a Lambda function

Prerequisites

This tutorial assumes you have completed these tasks before you begin:

  • Install Node.js on your computer to run various scripts that help set up the Amazon S3 bucket and the Amazon DynamoDB table, and create and configure the Lambda function. The Lambda function itself runs in the AWS Lambda Node.js environment. For information about installing Node.js, see www.nodejs.org.

  • Install the AWS SDK for JavaScript on your computer to run the setup scripts. For information on installing the AWS SDK for JavaScript for Node.js, see Installing the SDK for JavaScript.

  • Download slotassets.zip from the code example archive on GitHub. This archive contains the browser assets that are used by the application, the Node.js code that's used in the Lambda function, and several setup scripts. In this tutorial, you modify the index.html file and upload all the browser asset files to an Amazon S3 bucket you provision for this application. As part of creating the Lambda function, you also modify the Node.js code in slotpull.js before uploading it to the Amazon S3 bucket.

After you meet the prerequisites, this tutorial should take about 30 minutes to complete.

Tutorial Steps

To create this application you'll need resources from multiple services that must be connected and configured in both the code of the browser script and the Node.js code of the Lambda function. In some cases, the tasks involved are described here, while other tasks require topics of their own to detail the instructions. After you complete each of the steps in an additional topic, return to the steps here and continue.

To construct the tutorial application and the Lambda function it uses

  1. Create a working directory on your computer for this tutorial.

  2. Copy slotassets.zip into your working directory.

  3. Create a JSON file with your account credentials in your working directory. This file is used by the setup scripts to authenticate their AWS requests. For details, see Loading Credentials in Node.js from a JSON File.

  4. Create an Amazon S3 bucket configured as a static website.

  5. Prepare the browser script. Save the edited copy of index.html for upload to Amazon S3.

  6. Open the slotassets.zip archive you downloaded from the code example archive on GitHub, and copy the contents into your working directory.

  7. Create a Lambda execution role in IAM.

  8. Create and populate an Amazon DynamoDB table.

  9. Prepare and create the Lambda function.

  10. Run the Lambda function.