If you are utilizing the AWS SDK on a client with temporary credentials to send AWS requests, one method to implement a lambda event trigger is by using SQS long polling. This process involves several steps:
- Activate the DynamoDB stream
- Establish a lambda function and link the dynamodb as an event source
Now, you have a function operating in the background of DynamoDB. Any requests made to DynamoDB from the client will activate an event that triggers Lambda and contains the requested information from DynamoDB.
After successfully completing those steps, the next task is configuring the permissions to access SQS
- Create an SQS queue
- Add a new policy to the lambda role allowing access to sqs:SendMessage for the created SQS resources
- Add a new policy to your IAM authenticated/unauthenticated role granting access to sqs:ReceiveMessage and sqs:DeleteMessage
At this stage, both the lambda and client code need to be updated. The pseudocode for the lambda function includes:
- Fetching DynamoDB events
- Sending message to the SQS queue
The client's pseudocode would involve:
- Requesting AWS temporary credentials
- Performing SQS long polling using the ReceiveMessage method
- Upon receiving a new message, querying the DSB and invoking the SQS DeleteMessage method to avoid duplicate polling
The basic flow can be outlined as follows:
Client -> AWS SDK -> DynamoDB -> Lambda -> AWS SDK -> SQS (clients write to DynamoDB)
Client -> AWS SDK -> SQS (clients initiate long poll)
Client -> AWS SDK -> DynamoDB (clients read from DynamoDB)
Links:
Process New Items with DynamoDB Streams and Lambda
Enabling Long Polling in Amazon SQS