AWS Certified Developer - Associate
Serverless
Lambda Destinations Demo
In this lesson, you'll learn how to configure AWS Lambda destinations to manage the outcome of asynchronous invocations. We'll create a Lambda function that processes new user registrations and routes successful events and errors to separate Amazon SQS queues.
Creating the Lambda Function
Begin by creating a Lambda function named "lambda_new_user". This function will execute every time a new user registers.
Update the function code to log the event and return a simple message confirming the registration:
export const handler = async (event) => {
console.log(event);
// Simulate a new user registration.
return {
statusCode: 200,
body: JSON.stringify('New User Registered'),
};
};
Deploy these changes before proceeding.
Setting Up the Trigger
Next, configure your Lambda function to be triggered when a new registration message is published to an SNS topic.
- When a new user registers, your application will publish a message to an SNS topic.
- In your Lambda function configuration, add the SNS topic as a trigger.
Select your "new user" SNS topic:
Configuring Lambda Destinations
Set up asynchronous invocation destinations for your Lambda function to handle both successful and failed events. In this example, you'll configure two Amazon SQS queues:
- One for successful invocations (e.g., "new_user_success").
- One for failed invocations (e.g., "new_user_failure").
- Create the two SQS queues.
- In the Lambda function configuration, under the destinations section (for asynchronous invocations, not event source mapping), set the destination for an on-failure event using the failure SQS queue.
- Similarly, configure the destination for an on-success event using the success SQS queue.
Alternatively, you can configure these settings using the AWS CLI if preferred.
Testing the Successful Invocation
To verify your configuration:
- Open the SNS topic in the AWS SNS console.
- Publish a message, for example using "user one" as the username, with an optional message like "new user registered."
After publishing the message, the Lambda function processes the event and sends a message to the success SQS queue. Verify that the success queue contains a message similar to:
{
"body": "1aa2c035c0d0",
"functionArn": "arn:aws:lambda:us-east-1:841860927337:function:lambda_new_user:$LATEST",
"condition": "Success",
"approximateInvokeCount": 1,
"requestPayload": {
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:us-east-1:841860927337:..."
}
]
}
}
You can inspect the message details by polling the SQS queue.
Note
Ensure that your SNS topic and Lambda function have the proper permissions for integration.
Testing the Failure Scenario
Next, update your Lambda function to simulate an error. Modify the code to throw an error, allowing you to test the on-failure destination configuration:
export const handler = async (event) => {
console.log(event);
// Simulate an error in processing.
throw new Error('something went wrong');
// The response below will not be returned.
return {
statusCode: 200,
body: JSON.stringify('New User Registered'),
};
};
Deploy this updated code. Then publish a new message to the SNS topic (for example, using "user two" as the username). The Lambda function will fail and trigger asynchronous retry behavior. After two retry attempts, the event is sent to the failure SQS queue.
Check CloudWatch logs for error details. A sample error log might look like this:
{
"errorType": "Error",
"errorMessage": "something went wrong",
"stack": [
"Error: something went wrong",
" at Runtime.handler (file:///var/task/index.mjs:4:19)",
" at Runtime.handleOneConstreaming (file:///var/runtime/index.mjs:1173:29)"
]
}
Once verified, navigate to the SQS failure queue.
Poll the failure queue to view the failed message. An example message might include:
{
"version": "1.0",
"timestamp": "2024-04-05T02:53:09.185Z",
"requestContext": {
"requestId": "79187a77-1b5e-47e2-a659-747328bd5988"
},
"functionArn": "arn:aws:lambda:us-east-1:841186092733:function:lambda_new_user:$LATEST",
"condition": "RetriesExhausted",
"approximateInvokeCount": 3
}
The error details within the message body may appear as follows:
{
"errorType": "Error",
"errorMessage": "something went wrong",
"trace": [
"Error: something went wrong",
" at Runtime.handler (file:///var/task/index.mjs:4:9)",
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
]
}
Warning
Remember that failing invocations will trigger retries. Monitor CloudWatch to ensure that errors are handled as expected.
Summary
In this lesson, you configured AWS Lambda destinations to manage asynchronous invocations effectively. By directing successful events to one SQS queue and routing errors to another, you can enhance the robustness of your serverless architecture and streamline the monitoring process. Whether you're processing new user registrations or capturing errors, these techniques help ensure that your system remains reliable and responsive during variable workloads.
Watch Video
Watch video content