Projects on AWS
Bots Just Got Better
with .NET and the AWS Toolkit for Visual Studio
Module 2: AWS Lambda Code Hook Creation
In this module you will create and configure the Lambda function as the code hook used with the Lex Bot.
In this module, you will create and configure the Lambda function as the code hook used with the Lex Bot to perform initialization, validation, and/or fulfillment in the Lex intent configuration. You have four (4) options available to choose from in order to create the .NET Lambda function.
Follow the step-by-step instructions below to create the Lambda function for Lex. To expand the section, click on each step number.
Step 1: Create a Lambda Function
Option 1: Create and Upload Lambda using AWS Toolkit for Visual Studio (Windows Option)
For our chatbot, we will need to create and upload the Lambda function inside Visual Studio using the AWS toolkit.
a. Inside Visual Studio, Click File > New > Project.
b. On the next screen, choose the AWS Lambda Project (.NET Core) project template under Other Languages > Visual C# > AWS Lambda. See the first screenshot below.
c. On the next page, choose the Order Flowers blueprint. See the second screenshot below.
d. Once the project has been created, right click on the project the Solution Explore and click Publish to AWS Lambda
e. Enter a function name and click Next.
f. On the next page, choose an existing role or choose AWS LambdaFullAccess under New Role Based on AWS Managed Policy.
g. Then click Upload and sign into the console to open your uploaded function for testing
Choose the AWS Lambda Project template
Choose the Order Flowers blueprint
Option 2: Create a Lambda function using the .NET Core CLI
For the Order Flowers Chatbot, we will create a C# .NET Core 2.0 Lambda function as a code hook to Lex using the .NET Core CLI.
a. Install the .NET Core CLI for Windows, Linux or MacOS.
b. Open a command line or terminal window and type
dotnet new -all. The result will show the options for the command and the available templates as verification that the installation was successful.
c. Install the AWS Lambda templates with the AWS Lambda NuGet package by running
dotnet new -i Amazon.Lambda.Templates::*
d. Verify that the new AWS Lambda templates have been installed by reviewing the updated available templates presented for Lambda by typing the following command on the command line:
dotnet new -all
e. Download and unzip the OrderFlowers file.
f. Download and save the json lambda policy file.
g. In the command line or terminal window, go to the OrderFlowers folder and package the lambda by typing in following commands:
aws iam create-role --role-name lambda_basic_execution --assume-role-policy-document file://Test-Role-Trust-Policy.json
dotnet lambda deploy-function -frole "lambda_basic_execution" --region "us-east-1"
h.Test the Lambda function on the command line or terminal window or in the AWS Lambda Management Console.
Option 3: Create a Lambda Function Using the AWS Management Console
For the Order Flowers Chatbot, we will create a C# .NET Core 2.0 Lambda function as a code hook to Lex using the AWS Lambda Management Console.
a. Login to the Lambda AWS Management Console.
b. On the Lambda Dashboard, click the Create function button.
c. Select the Author from scratch option.
d. In the Name textbox, type: OrderFlowers for the function name for the Lambda function.
e. In the Runtime dropdown, select C# (.NET Core 2.0).
f. In the Role dropdown, select Choose an existing Role.
g. In the Existing role dropdown, type lambda_. You should see the IAM role, lambda_basic_execution as one of the selections. Select this IAM role for your Lambda function execution permission.
h. Click the Create function button.
i. Upon successful creation of your Lambda function, you will be taken to the Lambda function configuration page. While this browser window is open, download the zip file and save the file to an accessible location in your file system.
j. Return to your browser and the Lambda function dashboard. Under the Function code section of the screen, go to the Hander textbox and type: OrderFlowers::OrderFlowers.Function::FunctionHandler
k. Again under the Function code section of the webpage, click the Upload button located under the Function package option.
l. In the Open file dialog window, locate and select the previously downloaded zip file and click Open button.
m. Under the Tags section of the webpage, type lambda-console:lex-dotnet-chat in the first textbox to add the key and then type lex-dotnet-order-flowers in the second textbox to add the value.
n. Click the Save button at the very top right corner of the browser on the Lambda function configuration webpage.
Option 4: Create a Lambda function using the AWS CLI
For our Chatbot, we will need to upload the Lambda function included with solution you just cloned from Github so that the bot can invoke it.
a. Install the AWS CLI.
b. Open a terminal window and type aws configure. You will be prompted to enter 4 items:
1. AWS Access Id.
2. AWS Secret Access Key.
3. Default region name. You chose the region for this spot in Module 1.
4. Default output format (i.e. json).
c. Download the lambda zip file.
d. Now that the AWS CLI is configured and you have downloaded the zip, you can now upload the lambda using the CLI.
e. To use the AWS CLI to create the Lambda function, type the command:
aws lambda create-function --function-name OrderFlowersDotnet --runtime dotnetcore2.0 --role "nameOfIAMRolehere" --handler dotNetOrderFlowers.LexCodeHook::handler –zipfile “<filelocation/name.zip>”
⚐ NOTE: The handler is: assembly::namespace.class-name::method-name. For example, "TaraApp::LexBot.Hello::BotHandler"
Step 2: Test the Lambda Function
a. Click the Test button in the right-hand corner to test the function.
b. It will open the Configure Test Event module that looks like this:
c. Choose the Test Order Flowers template if it is not already selected by default.
d. Create a name for your test event.
e. Click Create.
f. Click Test and see if your Lambda executes successfully.
Step 3: Configure the Amazon Lex Bot
a. In the AWS Management Console choose Serivces and use the search bar to type in "Amazon Lex".
b. Once you are inside Amazon Lex, click on the name of your bot. Once on the bot page, make sure to click Edit at the top. Then, under Lambda Initialization and Validation, select the checkbox Initialization and Validation Code Hook. Then from the drop down, select the Lambda you created in this Module.
c. Under Fulfillment, select AWS Lambda Function and again select the Lambda you created in this Module from the drop down.
d. Now make sure to build your bot and publish it (buttons at the top). Then you can run the code locally and see if your bot works!