Lex must be used with some frontend interface - mobile app, web app etc. AWS provides SDKs to talk to Lex which can be used in applications. We can have a login form in the application and authenticate with Cognito (via SDK or API) and then get accessToken back which can be added to sessionAttributes in lex calls. Lex will pass on sessionAttributes to lambda in the request.

Authentication

We can use accessToken to authenticate now. I have created a small Cognito helper class, feel free to use it, tweak it: https://gist.github.com/crazyrohila/855f22d1ccbdaab78c320e56ad982bc5 

Authorization

We can also do authorization (initial) with help of this, we can get the user group from Cognito and control access to content.

Translation

We have the user info which also has locale information. we are going to use this to translate content to user-specific language. I am going to stick AWS Translate service here, but feel free to use any translation because AWS has limited language support. We can create one translate_api.py file under src/aws_api/ (for more info, follow this link: https://www.srijan.net/blog/aws-lex-handle-complex-workflows-and-dynamic-slots)  

This all seems to work fine, but to make it more efficient we should add locale and user groups to sessionAttributes while sending the response back to Lex. This way lambda doesn't have to make a request to Cognito for this info, we can directly get from sessionAttributes within the same session.

Lex communication via API

This all looks good, but lex doesn't understand any language other than English. So our application needs to translate user input into English then send to lex. To overcome this and have more control over what goes to lex as input, instead of using SDK we can have API gateway between application and Lex. This intermediate API (Lambda behind API) can do translation and other manipulation before sending to Lex.

Posts You May Like...