-2

Here is my code snipped that retrieves data from customer-event-logs dynamoDB table with customer_id parameter from /events endpoint.

    elif path == '/events':
        # Handle the /events endpoint logic here

        request_json = event['body']
        request_data = json.loads(request_json)

        # Extract the customer-id from the request data
        customer_id = request_data.get('customer-id', 'default_customer_id')

        # Check if the customer-id exists in the customers table
        if not check_customer_exists(customer_id):
            return {
                "statusCode": 400,
                "body": json.dumps({"error": "Customer ID does not exist."})
            }

        # Query the customer-event-logs table for records associated with the customer_id
        try:
            table = dynamodb_resource.Table('customer-event-logs')
            response = table.scan(
                FilterExpression=Attr('customer_id').eq(customer_id)
            )
            
            event_logs = response.get('Items', [])
            
        except Exception as e:
            return {
                "statusCode": 500,
                "body": json.dumps({"error": "Failed to retrieve event logs."})
            }

        return {
            "statusCode": 200,
            "body": json.dumps(event_logs)
        }

There are plenty of record in my customer-event-logs table with 'customer123' however this code returns me always {"error": "Failed to retrieve event logs."} message.

I gave my Lambda function full access to this dynomoDB table.

Is there any idea to solve this issue?

  • 1
    Log and investigate what `e` is. Silently discarding errors is very bad practice. – luk2302 Aug 14 '23 at 08:20
  • the question needs sufficient code for a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – D.L Aug 14 '23 at 23:03

1 Answers1

0

OK folks fixed it. Thank you again.

The solution is

        try:
            table = dynamodb_resource.Table('customer-event-logs')
            response = table.scan(
                FilterExpression=Attr('customer_id').eq(customer_id)
            )
            
            event_logs = response['Items']
            
        except Exception as e:
            return {
                "statusCode": 500,
                "body": json.dumps({"error": e})
            }

        return {
            "statusCode": 200,
            "body": json.dumps(event_logs, default=str)
        }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 16 '23 at 23:33