How to build an amazon Lambda function for conversational chatbot
Posted By : Ravindra Singh | 30-Apr-2018
What is Amazon Lambda :
Amazon Lambda is a serverless computing service that lets you run code without managing a server. Lambda having the responsibility of your code and executes only when needed and scales automatic, from a few requests per day to hundreds or thousands per second. You pay only as you go for the compute time you consume - there is no charge when your code is not running on aws platform.
AWS Lambda is having high-availability computing environment to run your code, Including server and operating system maintenance, and automatic scaling, and code monitoring and logging. just All the things you need to do is to supply your code in one of the languages that AWS Lambda supports currently aws Lambda supports Node.js, Java, C#, Go and Python.
by using amazon Lambda to run your code in response to aws lex events, or to HTTP requests using Amazon API Gateway, or invoke your code using API calls made using AWS SDK, such as updating into data in an Amazon S3 bucket, DynamoDB table. or we can create your own backend that operates at AWS scale, performance, and security. Only you are responsible to write your code. Rest of the things are handled by Amazon Lambda, Lambda manages the compute fleet that provides a balance of memory, CPU, network, and other resources.
Building Blocks of a Lambda-based Application :
Lambda function: AWS Lambda function is comprised of your custom code and any dependent libraries.
AWS Event sources: An Amazon AWS service, such as Amazon SNS, or a custom service, that triggers your function and executes its logic.
Downstream resources: An AWS service, such as DynamoDB tables or Amazon S3 buckets, that your Lambda function calls once it is triggered.
Logs stream: AWS Lambda automatically monitors your function invocation and reports metrics to CloudWatch, or in other words, we can say CloudWatch having the logs generated for your function to ensure it's working properly or not.
AWS SAM: Serverless Application Model is a model to define serverless applications. by native AWS SAM supported by AWS CloudFormation and defines the simple syntax for expressing serverless resources.
Features of amazon Lambda function :
1. No servers require to manage: AWS Lambda is automatically running your code without requiring you to provision or manage servers. Just what would you do, olny to write the code and upload it to Amazon Lambda.
2. AWS Continuous scaling: The main responsibility of aws Lambda to automatically scales your application by running code in response to each trigger. And runs your code in parallel and process individual trigger and scaling precisely with the size of the workload.
3. Completely automated: AWS Lambda functionality is completely automated means AWS Lambda manages all the infrastructure to run your code on highly available, fault-tolerant infrastructure, freeing you to focus on building differentiated back-end services. aws Lambda is properly responsible for resizing or adding new servers as your usage grows. AWS Lambda automatic deploys your code, does all maintenance, and security patches, and provides built-in logging and monitoring through Amazon CloudWatch.
4. Pay per use: With AWS Lambda, you are charged for every 100ms your code executes and the number of times your code is triggered. You don't pay anything when your code isn't running.
Create an AWS Lambda Function :
1. First Sign in to the AWS Management Console and open the AWS Lambda console at https://console.aws.amazon.com/lambda/.
2. Then next to Choose to Create a Lambda function.
3. next, On the Select blueprint page, choose Blank Function.
4. next, On the Configure triggers page, choose Next.
5. next, In the Runtime list, choose Java 8.
6. next, In your Lambda function, paste the following code sample as mentioned below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package lambdaClasses; import com.github.softwarebymark.lex.AbstractLexRequestHandler; import com.github.softwarebymark.lex.domain.FulfillmentState; import com.github.softwarebymark.lex.domain.InvocationSource; import com.github.softwarebymark.lex.domain.LexRequest; import com.github.softwarebymark.lex.domain.LexResponse; import java.util.Map; public class HelloBotRequestHandler extends AbstractLexRequestHandler { private static final String NAME = "Name"; //Below is the in-built method of AWS Lambda function that handle request and response in java @Override public LexResponse handleRequest(LexRequest lexRequest, Map<String, String> sessionAttributes) { try { if ("HelpIntent".equals(lexRequest.getIntent().getName())) { return processHelpIntent(); } else if ("HelloIntent".equals(lexRequest.getIntent().getName())) { return processHelloIntent(lexRequest, sessionAttributes); } else if ("GoodbyeIntent".equals(lexRequest.getIntent().getName())) { return processGoodbyeIntent(sessionAttributes); } else { return createElicitIntentDialogActionResponse(); } } catch (Exception e) { return createCloseDialogActionResponse(FulfillmentState.Failed, "Sorry, I'm having a problem fulfilling your request. Please try again later."); } } //it's aourcustom methods inside the aws Lambda function programming private LexResponse processHelpIntent() { return createCloseDialogActionResponse(FulfillmentState.Fulfilled, "I'm a friendly bot. Just say: hello I'm <insert your name here>."); } //it's our custom methods inside the aws Lambda function programming private LexResponse processHelloIntent(LexRequest lexRequest, Map<String,String> sessionAttributes) { String name = lexRequest.getIntent().getSlots().get(NAME); if (InvocationSource.DialogCodeHook.equals(lexRequest.getInvocationSource())) { if (name == null) { return createDelegateDialogActionResponse(lexRequest); } else { return sayHelloTo(name, sessionAttributes); } } else if (InvocationSource.FulfillmentCodeHook.equals(lexRequest.getInvocationSource())) { // The slot should not be empty if the InvocationSource is Fulfillment Code Hook return sayHelloTo(name, sessionAttributes); } else { throw new RuntimeException("Unknown Invocation Source: " + lexRequest.getInvocationSource()); } } //it's our custom methods inside the aws Lambda function programming private LexResponse sayHelloTo(String name, Map<String,String> sessionAttributes) { sessionAttributes.put(NAME, name); return createCloseDialogActionResponse(FulfillmentState.Fulfilled, "Hello " + name); } //it's our custom methods inside the aws Lambda function programming private LexResponse processGoodbyeIntent(Map<String,String> sessionAttributes) { String name = sessionAttributes.get(NAME); if (name == null) { // If the session has timed out, we will not know the person's name name = ""; } return createCloseDialogActionResponse(FulfillmentState.Fulfilled, "Goodbye " + name); } } |
7. In the Lambda function handler and role section, in the Role list, choose the service role for Lambda that you created in Task 2.
8. Choose Next, and then choose to Create function.
9. Finally test the Lambda function.
Amazon Lambda workflow architecture :
-> Application flow diagram :
-> BluePrints for creating amazon Lambda function for chatbot :
Step 1. AWS Lambda console :
Step 2. Next to click on the Blank function to create own custom function :
Step 3. Next is Configuration steps :
step 4. Next is preview screen :
Step 5. And next is the final creation of Lambda screen where you can upload your java code :
Step 6.
Next, integrate aws Lambda with Lex, then simply configure the Lambda function name on Lex Console and then Lambda function is responsible to handle request and response sent by aws Lex.
Thank you
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ravindra Singh
Ravindra is Sr. Associate Consultant Development- Java (Backend Developer). And Familiar with AWS Cloud Machine Learning Programming (AWS Lex, Lambda, Polly, Elasticsearch ), And also having good experience in Spring Boot Microservice Architecture Applica