Leveraging AWS Lex Intents with AWS Lambda Functions to Retrieve Data from DynamoDB

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:

  • Search for events by type (e.g., sports, concerts, workshops).
  • Support flexible date ranges like "this weekend" or "next month."
  • Provide fast, real-time responses.

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:

  1. User query — The user submits a query like "What are the sports events happening this weekend?" through the mobile app.
  2. Lex processes the input — Lex identifies key details: the event type (sports) and date (weekend).
  3. Lambda fetches data — Lex triggers a backend Lambda function with these extracted parameters.
  4. DynamoDB query — Lambda queries DynamoDB using event_type and event_date to fetch relevant events.
  5. Response to user — The Lambda function formats the results and sends them back to Lex, which returns a user-friendly response to the mobile app.

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:

  • "What sports events are happening this weekend?"
  • "Show me concerts next Friday."
  • "Are there any workshops near me tomorrow?"

Slot Definitions

Slots are placeholders that Lex uses to capture key details from the user query. For this chatbot, we defined:

  • EventType: Captures the type of event (e.g., sports, concert, workshop). We used a custom slot type with predefined values.
  • EventDate: Captures the date or time period (e.g., today, this weekend, next week). We used AMAZON.Date to handle date inputs flexibly.

Once Lex identifies the intent and fills the slots, it triggers the Lambda function to retrieve the requested data.

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

  1. Connect Lambda to Lex: In the Lex console, configure the FindEvents intent to invoke the Lambda function.
  2. Test Scenarios: Ensure the bot handles diverse queries like "Find music events this weekend" or "What sports events are on next Friday?"
  3. Deploy to Mobile App: Once the app development progresses, this feature can integrate seamlessly for event searching.

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:

  • Personalized User Experience: Event type and date slots enable precise searches.
  • Efficient Data Handling: DynamoDB structure supports rapid lookups based on user queries.
  • Scalability: The serverless approach scales effortlessly with user traffic.

CloudifyOps delivers scalable, serverless chatbot solutions with AWS Lex, Lambda, and DynamoDB.

Contact us to modernize your app.


To view or add a comment, sign in

More articles by CloudifyOps

Insights from the community

Others also viewed

Explore topics