A developer is implementing a microservice that processes message batches from an Amazon SQS queue. The processing logic is deployed as an AWS Lambda function with a timeout of minutes. However, the developer notices that some message batches are processed multiple times, and the application logs show that the Lambda function is frequently terminated prematurely. Additionally, the Lambda function code currently initializes the SQS client by passing hardcoded IAM credentials.
Which of the following actions should the developer take to resolve these issues? (Select TWO.)
- Remove the hardcoded credentials from the SDK client initialization and associate an IAM execution role containing SQS permissions with the Lambda function.Answer
- Increase the Lambda function's timeout to exceed the maximum processing time, and set the SQS queue's visibility timeout to at least times the Lambda function's timeout.Answer
- CDecrease the SQS queue's visibility timeout to seconds so that messages can be quickly picked up by other concurrent Lambda executions.
- DRe-initialize the SQS client inside the Lambda handler function on every invocation to guarantee that execution context variables are cleared after each run.
- EPass the IAM access key and secret access key directly to the SQS client constructor in the code, using the default credential provider chain as a fallback.
Answer
The developer should remove the hardcoded credentials from the SDK client initialization and associate an IAM execution role containing SQS permissions with the Lambda function, while also increasing the Lambda function's timeout to exceed the maximum processing time and setting the SQS queue's visibility timeout to at least times the Lambda function's timeout.
To secure the function, the developer should associate an IAM execution role with the Lambda function and remove hardcoded credentials from the SDK client. To resolve the timeouts and duplicates, the developer should increase the Lambda timeout to accommodate the processing duration and set the SQS queue's visibility timeout to at least times the Lambda function's timeout, which is the AWS recommended ratio for SQS-Lambda integrations.
Step-by-Step Solution
Key Concept
Integrating Amazon SQS with AWS Lambda, managing visibility timeouts, and securing SDK clients using IAM execution roles.