Ace Cloud Interviews
🤖

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.

ConceptDefinitionExample
IntentAn action the user wants to performBookFlight, CheckOrderStatus, ResetPassword
UtteranceSample 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"
SlotA variable that needs to be filled to complete the intentDepartureCity, ArrivalCity, TravelDate, CabinClass
Slot TypeThe data type and valid values for a slotCustom list (Economy, Business, First), Built-in (AMAZON.City, AMAZON.Date)
FulfillmentWhat happens after all slots are filledLambda function call or return control to calling app
ElicitationBot 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).

bash
# 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 TypeWhen CalledCan DoReturn
Dialog Code HookAfter every user turnValidate slots, re-elicit, add contextDelegate, ElicitSlot, ElicitIntent, Close
Fulfillment Code HookAfter all slots filledExecute business logic, call APIsClose (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.

ChannelIntegration MethodUse Case
Amazon ConnectNative Lex integration in contact flowsIVR replacement, voice + chat customer service
Facebook MessengerLex console channel configurationConsumer-facing chatbot on Facebook pages
SlackLex console channel configurationInternal IT helpdesk, DevOps bots
Twilio SMSLex console channel configurationSMS-based customer support
Web/Mobile AppAWS Amplify AI or direct API callsCustom UI with Lex backend
Genesys / SalesforcePartner integrationsEnterprise 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

FeaturePricingNotes
Text requests$0.00075 per requestOne request = one user message turn
Speech requests$0.004 per requestVoice input uses both ASR and NLU
Free tier10,000 text + 5,000 speech requests/monthFree 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?