AWS Lambda is a serverless compute service that lets you run applications and services without provisioning or managing servers. It automatically handles the underlying compute resources, allowing you to focus on your code and easily scale.
In this section, you will intentionally update the data_process_action
Lambda function with several bugs. Your goal is to use Amazon Q to debug and fix the issues.
{
"agent": {
"alias": "TSTALIASID",
"name": "Agent-AWS",
"version": "DRAFT",
"id": "ADI6ICMMZZ"
},
"sessionId": "975786472213626",
"httpMethod": "GET",
"sessionAttributes": {},
"inputText": "Can you get the number of records in the databse",
"promptSessionAttributes": {},
"apiPath": "/get_num_records",
"messageVersion": "1.0",
"actionGroup": "agent_action_group"
}
You’ll likely encounter an error because some dependencies are missing. Don’t worry, Amazon Q in the console will help you:
How can I add the official prebuilt AWS pandas lambda layer to my lambda function without using the CLI?
While the Amazon Q chat is helpful for general AWS questions, for more specific guidance, use the Troubleshoot with Amazon Q feature.
S3_OBJECT
environment variable if needed.clickstream_data.csv
Need help? Here’s what to do next
Here’s the full code for reference
import os
import json
import pandas
import boto3
S3_BUCKET = os.environ["S3_BUCKET"]
S3_OBJECT = os.environ["S3_OBJECT"]
def lambda_handler(event, context):
# Print the received event to the logs
print("Received event: ")
print(event)
# Initialize response code to None
response_code = None
# Extract the action group, api path, and parameters from the prediction
action = event["actionGroup"]
api_path = event["apiPath"]
inputText = event["inputText"]
httpMethod = event["httpMethod"]
print(f"inputText: {inputText}")
# Check the api path to determine which tool function to call
if api_path == "/get_num_records":
s3 = boto3.client("s3")
s3.download_file(S3_BUCKET, S3_OBJECT, "/tmp/data.csv")
df = pandas.read_csv("/tmp/data.csv")
# Get count of dataframe
count = len(df)
response_body = {"application/json": {"body": str(count)}}
response_code = 200
else:
# If the api path is not recognized, return an error message
body = {"{}::{} is not a valid api, try another one.".format(action, api_path)}
response_code = 400
response_body = {"application/json": {"body": str(body)}}
# Print the response body to the logs
print(f"Response body: {response_body}")
# Create a dictionary containing the response details
action_response = {
"actionGroup": action,
"apiPath": api_path,
"httpMethod": httpMethod,
"httpStatusCode": response_code,
"responseBody": response_body,
}
# Return the list of responses as a dictionary
api_response = {"messageVersion": "1.0", "response": action_response}
return api_response
Once the Lambda function is fixed, go back to the Agent and test it again by asking:
Can you help with the data processing task of getting the number of records in the production database?
This time, the Agent will be able to provide the correct answer. You can also view the trace to see how the Agent “thought” through the process to generate the correct response.