Blogs | Srijan

AWS Lex  - Cognito Auth and Translation

Written by Sanjay Rohila | Oct 17, 2018 7:00:00 AM

Lex is a powerful deep learning service by AWS, offering automatic speech recognition and natural language understanding features. 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 back an accessToken, which can be added to sessionAttributes in Lex calls. Lex will pass on sessionAttributes to Lambda in the request.

Authentication: We can use the accessToken to authenticate now. I have created a small Cognito helper class. Feel free to use it and tweak it to your requirements. 

Authorization: We can also decide authorization with this. Simply fetch the user group from Cognito and control access to content.

Translation: We have the user info which also has locale information. We can use this to translate content to user-specific language. I am going to stick with 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/.  

While this'll work fine, it can be made more efficient by adding 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, as we can directly get it from sessionAttributes within the same session.

Lex communication via API: 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.

So those were some of the basics of managing authentication and translation on Amazon Lex. You can also explore: