Error Handling

Inventory Architecture

As you’ve seen in previous modules, error handling is crucial to a well operated event-driven architecture. Lambda offers two mechanisms for handling function exection failure: Dead Letter Queues (DLQ) and Lambda Destinations.

Dead Letter Queues have been available since 2016 and are a great way to handle asynchronous failure situations. Destinations provide more useful capabilities by passing additional function execution information, including code exception stack traces, to more destination services.

Destinations and DLQs can be used together and at the same time although Destinations should be considered a more preferred solution. If both Destinations and DLQ are used for Failure notifications, function invoke errors are sent to both DLQ and Destinations targets.

In this module, you will update the OrdersFunction Lambda function with an addition On failure Destination to a SQS queue. First, let’s take a closer look at the Lambda function code to see how to force errors within the execution in order to test this functionality.

import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

FORCE_ERROR_ATTRIBUTE_KEY = 'force-error'

def lambda_handler(event, context):
    logger.info('{}'.format(event))
    message = json.loads(event['Records'][0]['Sns']['Message'])

    if (FORCE_ERROR_ATTRIBUTE_KEY in message and message[FORCE_ERROR_ATTRIBUTE_KEY]):
        error_message = 'FAILED! (force-error == true)'
        logger.error(error_message)
        raise Exception(error_message)

    return event

As you can see, the Lambda function processes the SNS event and parses the serialized JSON in the Message property of the event. If the message contains a property called force-error and the value is true, then function execution fails when an Exception is raised.

First, you will create the Lambda Destination to handle the failed execution and then verify error handling using the Event Generator for EventBridge.

Step 1: Configure Failure Destination on OrdersFunction Lambda function

  1. Open the AWS Management Console for Lambda in a new tab or window, so you can keep this step-by-step guide open.

  2. Enter OrdersFunction in the Lambda function filter.

    Lambda Function List

  3. Select the Lambda function from the list of functions.

    Lambda Function Select

  4. In the Lambda function Designer, choose Add destination.

    Lambda Function Add Destination

  5. In the Add destination dialog box:

    • For Condition, leave On failure selected.
    • For Destination type, select SQS queue.
    • For Destination, select OrdersFunctionDLQ.

    Lambda Function Add Destination

    The Lambda function must have permissions through its IAM Execution Policy to access the success and failure Destinations.
    For simplicity, the function’s IAM Execution Policy has been configured with access to EventBridge and SQS.

  6. Choose Save.

    Lambda Function Save Destination

    The On failure Destination is displayed.

    Lambda Function On Failure Destination

Step 2: Testing the end-to-end functionality

To test the end-to-end functionality, you will publish a message to the Orders EventBridge event bus with an EU location. This will trigger the following sequence of event-driven actions:

1) The message containing an EU location and force-error flag triggers the EventBridge Rule, which you created in the previous module, to route the message to the OrderProcessing StepFunctions workflow.

2) The OrderProcessing StepFunctions workflow sends the message to the InventoryTopic SNS topic.

3) The InventoryTopic SNS topic routes the message to the OrdersFunction Lambda function.

4) On failed execution, the OrdersFunction Lambda function sends a message to the OrdersFunctionDLQ SQS queue through the On failure Lambda destination.

Let’s test it!

  1. Open the Event Generator for EventBridge in a new tab or window, so you can keep this step-by-step guide open.

  2. Make sure that the Event Generator is populated with the following (if you clicked the link above then you should see this pre-populated)

    • Event Bus selected to Orders
    • Source should be com.aws.orders
    • In the Detail Type add Order Notification
    • JSON payload for the Detail Template should be (note force-error is added):

      {
      "category": "office-supplies",
      "value": 1200,
      "location": "eu-west",
      "force-error": true
      }

    Event Generator EventBridge

  3. Click Publish.

    Event Generator EventBridge Publish

  4. Open the AWS Management Console for SQS in a new tab or window, so you can keep this step-by-step guide open.

  5. On the SQS page, enter OrdersFunctionDLQ in the Filter.

    SQS Select Queue

  6. Note the OrdersFunctionDLQ SQS queue has 1 message available.

    SQS Select Queue

CONGRATULATIONS! You have successfully used Lambda Destinations handle Lambda function execution failure.