Menu
Amazon Lex
Developer Guide

Step 1: Create a Lambda Function

First, create a Lambda function which fulfills a pizza order. You specify this function in your Amazon Lex bot, which you create in the next section.

To create a Lambda function

  1. Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. Choose the US East (N. Virginia) Region (us-east-1).

  3. Choose Create a Lambda function.

  4. On the Select blueprint page, choose Blank function.

    Because you are using custom code provided to you in this exercise to create a Lambda function, you choose the Blank function option.

  5. On the Configure triggers page, choose Next.

  6. On the Configure function page, do the following:

    1. Type the name (PizzaOrderProcessor) and choose Node.js.4.3 as the runtime.

    2. In the Lambda function code section, choose Edit code inline, and then copy the following Node.js function code and paste it in the window.

      Copy
      'use strict'; // Close dialog with the customer, reporting fulfillmentState of Failed or Fulfilled ("Thanks, your pizza will arrive in 20 minutes") function close(sessionAttributes, fulfillmentState, message) { return { sessionAttributes, dialogAction: { type: 'Close', fulfillmentState, message, }, }; } // --------------- Events ----------------------- function dispatch(intentRequest, callback) { console.log(`request received for userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`); const sessionAttributes = intentRequest.sessionAttributes; const slots = intentRequest.currentIntent.slots; const crust = slots.crust; const size = slots.size; const pizzaKind = slots.pizzaKind; callback(close(sessionAttributes, 'Fulfilled', {'contentType': 'PlainText', 'content': `Okay, I have ordered your ${size} ${pizzaKind} pizza on ${crust} crust`})); } // --------------- Main handler ----------------------- // Route the incoming request based on intent. // The JSON body of the request is provided in the event slot. exports.handler = (event, context, callback) => { try { dispatch(event, (response) => { callback(null, response); }); } catch (err) { callback(err); } };
    3. In the Lambda function handler and role section, choose Choose a new role from template(s) for the Role, and then type a role name.

    4. Choose Next.

  7. On the Review page, choose Create function.

Test the Lambda Function Using Sample Event Data

In the console, test the Lambda function by using sample event data to manually invoke it.

To test the Lambda function:

  1. Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.

  2. On the Lambda function page, choose Actions, and then choose Configure test event.

  3. Choose the Lambda function.

  4. On the Input test event page, copy the following Amazon Lex event into the window.

    Copy
    { "messageVersion": "1.0", "invocationSource": "FulfillmentCodeHook", "userId": "user-1", "sessionAttributes": {}, "bot": { "name": "PizzaOrderingApp", "alias": "$LATEST", "version": "$LATEST" }, "outputDialogMode": "Text", "currentIntent": { "name": "OrderPizza", "slots": { "size": "large", "pizzaKind": "meat", "crust": "thin" }, "confirmationStatus": "None" } }
  5. Choose Save and test.

AWS Lambda executes your Lambda function and displays the following output in the Execution result pane.

Copy
{ "sessionAttributes": {}, "dialogAction": { "type": "Close", "fulfillmentState": "Fulfilled", "message": { "contentType": "PlainText", "content": "Okay, I have ordered your large meat pizza on thin crust" } }

Next Step

Step 2: Create a Bot