AWS AI & Machine Learning
Lex
Build conversational chatbots using the same technology powering Alexa
Amazon Lex is a fully managed service for building conversational interfaces - chatbots and voice assistants - using the same deep learning technology that powers Alexa. It handles automatic speech recognition (ASR) for voice and natural language understanding (NLU) for text, letting you define intents, utterances, and slot types to build sophisticated multi-turn conversations. For cloud engineers, Lex is the backbone of customer service automation, IVR replacement, and internal productivity bots.
Lex Core Concepts - Intents, Slots, and Utterances
Understanding Lex's data model is essential before building any bot. Everything in Lex is built around these four concepts.
| Concept | Definition | Example |
|---|---|---|
| Intent | An action the user wants to perform | BookFlight, CheckOrderStatus, ResetPassword |
| Utterance | Sample phrases that map to an intent (you provide 10-20) | "I want to fly to London", "Book me a flight", "Get me a ticket to NYC" |
| Slot | A variable that needs to be filled to complete the intent | DepartureCity, ArrivalCity, TravelDate, CabinClass |
| Slot Type | The data type and valid values for a slot | Custom list (Economy, Business, First), Built-in (AMAZON.City, AMAZON.Date) |
| Fulfillment | What happens after all slots are filled | Lambda function call or return control to calling app |
| Elicitation | Bot prompts to collect missing slot values | "What city are you flying to?" - asked until DepartureCity is filled |
Lex V2 (current version) uses a concept called "conversation flows" with visual builder support. Lex V1 is legacy - all new bots should use V2. V2 also supports multi-language bots in a single bot definition.
Lambda Integration - Fulfillment and Validation Hooks
Lambda functions are the primary way to add business logic to Lex bots. There are two hook points: the dialog code hook (called after each user turn for validation) and the fulfillment code hook (called after all slots are filled).
# Lambda fulfillment handler for Lex V2
import json
def lambda_handler(event, context):
intent_name = event['sessionState']['intent']['name']
slots = event['sessionState']['intent']['slots']
if intent_name == 'BookFlight':
departure = slots['DepartureCity']['value']['interpretedValue']
arrival = slots['ArrivalCity']['value']['interpretedValue']
travel_date = slots['TravelDate']['value']['interpretedValue']
# Business logic here (call booking API, check availability, etc.)
booking_ref = book_flight(departure, arrival, travel_date)
return {
'sessionState': {
'dialogAction': {'type': 'Close'},
'intent': {'name': intent_name, 'state': 'Fulfilled'}
},
'messages': [{
'contentType': 'PlainText',
'content': f'Your flight from {departure} to {arrival} on {travel_date} is booked. Ref: {booking_ref}'
}]
}
| Hook Type | When Called | Can Do | Return |
|---|---|---|---|
| Dialog Code Hook | After every user turn | Validate slots, re-elicit, add context | Delegate, ElicitSlot, ElicitIntent, Close |
| Fulfillment Code Hook | After all slots filled | Execute business logic, call APIs | Close (success or failure message) |
Deployment Channels and Integration Patterns
Lex bots can be deployed to multiple channels without changing the bot definition. AWS provides native integrations for the most common contact center and messaging platforms.
| Channel | Integration Method | Use Case |
|---|---|---|
| Amazon Connect | Native Lex integration in contact flows | IVR replacement, voice + chat customer service |
| Facebook Messenger | Lex console channel configuration | Consumer-facing chatbot on Facebook pages |
| Slack | Lex console channel configuration | Internal IT helpdesk, DevOps bots |
| Twilio SMS | Lex console channel configuration | SMS-based customer support |
| Web/Mobile App | AWS Amplify AI or direct API calls | Custom UI with Lex backend |
| Genesys / Salesforce | Partner integrations | Enterprise contact center platforms |
Amazon Connect + Lex is the most common enterprise pattern. You can build a full IVR replacement where Lex handles natural language intents, and Connect routes to the right agent queue based on the Lex session attributes.
Lex Pricing
| Feature | Pricing | Notes |
|---|---|---|
| Text requests | $0.00075 per request | One request = one user message turn |
| Speech requests | $0.004 per request | Voice input uses both ASR and NLU |
| Free tier | 10,000 text + 5,000 speech requests/month | Free for 12 months |
For Amazon Connect integration, Lex charges are in addition to Connect charges. At scale, a contact center handling 1 million voice calls per month at 5 turns per call would incur $20,000/month in Lex charges alone - factor this into pricing models.
Interview Focus Points
- 1Explain the Lex data model - what are intents, slots, utterances, and how do they relate to each other?
- 2What is the difference between a dialog code hook and a fulfillment code hook in Lex?
- 3How would you build an IVR system using Amazon Lex and Amazon Connect?
- 4How does Lex handle ambiguous user input where the utterance could match multiple intents?
- 5How would you build a multi-language customer support bot using Lex V2?
- 6What is the slot elicitation process and how do you implement validation logic in Lambda?
- 7How would you test and iterate on a Lex bot in a CI/CD pipeline?