A developer is troubleshooting a serverless application where an AWS Lambda function is triggered by an Amazon SQS queue. The Lambda function processes incoming messages, invokes a downstream third-party REST API using the Python `requests` library, and records metrics. During testing under high load, the developer notices two main issues:
1. Downstream third-party REST API calls do not appear as subsegments in the AWS X-Ray service map.
2. Many messages are being processed multiple times by the Lambda function, resulting in duplicate API calls and redundant traces.
Which two actions must the developer take to resolve these issues?
- Use the AWS X-Ray SDK for Python to patch the requests library at the start of the Lambda function code.Answer
- Increase the visibility timeout of the SQS queue to be at least times the execution timeout of the Lambda function.Answer
- CReconfigure the Lambda integration in Amazon API Gateway from proxy integration to custom integration and manually map the HTTP headers in the integration request template.
- DHardcode temporary AWS IAM credentials retrieved from AWS STS inside the Lambda function initialization code to authorize the X-Ray recorder client.
- EIncrease the Lambda function's execution timeout to match the SQS queue's message retention period to prevent trace context loss during execution context reuse.
Answer
To resolve these issues, the developer must patch the requests library using the AWS X-Ray SDK for Python and increase the SQS queue's visibility timeout to be at least times the Lambda function's execution timeout.
To trace downstream HTTP calls made via the Python requests library, the developer must patch the library using the AWS X-Ray SDK for Python. This dynamically instruments HTTP client libraries to generate tracing subsegments for outgoing HTTP requests. Additionally, under high load, if the SQS queue's visibility timeout is too close to the Lambda function's execution timeout, messages may return to the queue and be processed by other concurrent invocations before the original execution finishes. Setting the SQS visibility timeout to at least times the Lambda execution timeout prevents these duplicate invocations and the resulting redundant traces.
Step-by-Step Solution
Key Concept
Instrumenting HTTP libraries with AWS X-Ray SDK and configuring SQS visibility timeouts for Lambda integration.
Estimated Time:2m 30s