AWS Lambda

Due to security concerns and other factors, some users may prefer not to allow Aissistant direct access to their information systems. In such cases, creating a simplified API that grants restricted access to your internal system is a viable solution. This approach allows you to dictate the scope of accessible data, perform necessary processing and filtering, and then determine the output.

AWS Lambda offers an efficient platform for running your code without the need to manage servers, scaling, or load balancing. It's cost-effective, as you only pay for the resources you use, and it's straightforward to configure. This makes it an ideal choice for developing a simple RESTful API for Aissistant.

In this tutorial, I will guide you through the process of using AWS Lambda to create a basic API that fetches booking information from an internal database.

Step 1. Write code to retrieve data

We write a simple Python script designed to fetch booking details from the database using a specific booking ID.

import json
import mysql.connector

def lambda_handler(event, context):
    secrets = dict()
    with open("secrets.json") as input_fp:
        secrets = json.load(input_fp)
    database = mysql.connector.connect(

    query = """ SELECT * FROM cr_booking WHERE id = %s """
    cursor = database.cursor()
    cursor.execute(query, (json.loads(event['body'])["booking_id"],))
    column_names = [col[0] for col in cursor.description]
    bookings = [dict(zip(column_names, row)) for row in cursor.fetchall()]
    return {"book_details": bookings}

Step 2. Package the code

Now, we need to package the code, secrets file, and needed python packages into a zip file for AWS Lambda.

pip3 install --target ./packages mysql-connector
cd ./packages
zip -r ../ .
cd ..
zip secrets.json

Step 3. Create a AWS Lambda function

Go to AWS Lambda console, create a function. For simplicity, enable Function URL, set the Auth type to be None. You will use the function URL as the RESTful API url. You can also use AWS API gateway to connect to the Lambda function for extra security. After the function is created, just upload the zip file we created in last step. Then your RESTful API is ready.

Step 4. Connect Lambda to Aissistant

Follow the steps on Restful API to connect your Lambda function to Aissistant.

After the Lambda function is added, you can use the debugger to test if it is working well.

Last updated