Leveraging AWS Lex Intents with AWS Lambda Functions to Retrieve Data from DynamoDB
Authored by Jobin Sobachan
Our client, a mobile event booking platform, sought to enhance user engagement by enabling customers to discover events happening around them — specifically events matching their interests, like sports or music, on specific dates. To achieve this, we designed a serverless solution leveraging AWS Lex, Lambda, and DynamoDB.
This blog outlines the solution, reflecting the client's requirements, challenges, and the tailored architecture we implemented.
Client Requirements
The client’s mobile application is still in development, and while event booking functionality isn’t ready yet, they wanted to start by providing users with the ability to:
This functionality will serve as a core feature of the application once the booking system is complete.
Solution Overview
The architecture integrates AWS Lex for natural language understanding, AWS Lambda for backend logic, and DynamoDB for data storage. The workflow looks like this:
Section 1: Understanding AWS Lex
AWS Lex is a service for building conversational interfaces — chatbots — into applications. It uses the same technology behind Alexa to convert user input (text or speech) into structured data the application can act on.
What Are Lex Intents?
At the heart of Lex's functionality are intents. An intent represents a user’s goal or desired action. For example, in this use case, the goal is to find events.
We created a custom Lex intent tailored to the client’s use case:
Intent Name: FindEvents
Sample User Phrases:
Slot Definitions
Slots are placeholders that Lex uses to capture key details from the user query. For this chatbot, we defined:
Once Lex identifies the intent and fills the slots, it triggers the Lambda function to retrieve the requested data.
Recommended by LinkedIn
Section 2: DynamoDB Table Setup
We structured DynamoDB to efficiently store and retrieve event data:
Field Type Description
EventID String (PK) Unique event identifier
EventType String Type of event (sports, concert, etc.)
EventDate String Date of the event (YYYY-MM-DD)
EventLocation String Location of the event
EventName String Name of the event
EventDescription String Short event description
Example Event Data:
{
"EventID": "001",
"EventType": "Sports",
"EventName": "Local Football Match",
"EventLocation": "Bangalore",
"EventDescription": "Exciting local league match!",
"EventDate": "2025-03-29"
}
Section 3: AWS Lambda Backend for Event Retrieval
The Lambda function processes user requests and queries DynamoDB based on EventType and EventDate.
Lambda Function (Python):
import json
import boto3
from datetime import datetime
# DynamoDB setup
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('EventsTable')
def lambda_handler(event, context):
# Extract slots from Lex event
event_type = event['currentIntent']['slots']['EventType']
event_date = event['currentIntent']['slots']['EventDate'] or datetime.now().strftime('%Y-%m-%d')
# Query DynamoDB using event type and date
response = table.scan(
FilterExpression="EventType = :etype AND EventDate >= :edate",
ExpressionAttributeValues={":etype": event_type, ":edate": event_date}
)
# Prepare response
if 'Items' in response and len(response['Items']) > 0:
events = [f"{e['EventName']} on {e['EventDate']}: {e['EventDescription']}" for e in response['Items']]
message = "Here are the events I found:\n" + "\n".join(events)
else:
message = f"Sorry, no {event_type} events found for {event_date}."
return {
'dialogAction': {
'type': 'Close',
'fulfillmentState': 'Fulfilled',
'message': {'contentType': 'PlainText', 'content': message}
}
}
Section 4: Final Configuration and Testing
Conclusion
By customizing AWS Lex, Lambda, and DynamoDB to meet the client’s specific needs, we delivered a fast, scalable, and intuitive event search feature.
Key Takeaways:
CloudifyOps delivers scalable, serverless chatbot solutions with AWS Lex, Lambda, and DynamoDB.
Contact us to modernize your app.