- Newest
- Most votes
- Most comments
I would recommend going with Step Functions as an orchestrator. You could create a workflow where you can have a initial task that gets all the IDs using a Lambda function (same as one of the steps you planned for lambda-2) from the DynamoDB table and then, iterate through them using a Map state. Each iteration will process an ID using lambda-1.
Some additional notes:
- Now the orchestration is moved away from Lambda and handled by a Step Functions workflow. This way 1) you don't assume cost from a Lambda function that is most of the time waiting for other functions to do their job 2) you move away from the execution time out
- The Step Function workflow can be triggered daily using EventBridge as it is a supported target.
- You can - and probably should - leverage the optimized integration with DynamoDB to get all the IDs. This will reduce execution time and save cost.
- If all IDs don't fit in a single DynamoDB operation you will have to add pagination.
Your approach to solving this problem is on the right track, but we can refine it to create a more scalable and efficient solution. Here's a suggested architecture that addresses your requirements:
-
Create an EventBridge rule that triggers once a day.
-
This rule should invoke a new Lambda function (let's call it lambda-orchestrator) that will manage the process.
-
The lambda-orchestrator function will: a. Retrieve all the IDs from the DynamoDB table. b. For each ID, invoke lambda-1 asynchronously.
-
Lambda-1 remains unchanged, performing the analysis for a single ID.
This architecture has several advantages:
-
Scalability: By invoking lambda-1 asynchronously for each ID, you avoid the execution time limit issue. Each invocation of lambda-1 runs independently, allowing for parallel processing of multiple IDs.
-
Efficiency: The lambda-orchestrator function's job is relatively quick - it just needs to fetch the IDs and trigger the other Lambda functions. This reduces the risk of hitting execution time limits.
-
Flexibility: As new IDs are added to the DynamoDB table, they'll automatically be included in the next day's processing without any changes to your code.
-
Cost-effective: You only pay for the actual execution time of each Lambda function.
To implement this:
-
Create the EventBridge rule to run daily.
-
Create the lambda-orchestrator function. This function should:
- Scan the DynamoDB table to get all IDs.
- Use the AWS SDK to invoke lambda-1 asynchronously for each ID.
-
Ensure that lambda-1 is set up to receive the ID as an input parameter.
-
Grant necessary permissions:
- Allow EventBridge to invoke lambda-orchestrator.
- Allow lambda-orchestrator to read from DynamoDB and invoke lambda-1.
- Ensure lambda-1 has permissions to read from DynamoDB and perform its analysis tasks.
This solution should be able to handle a growing number of IDs efficiently, running the analysis for each ID once a day as required.
Sources
Tutorial: Create an EventBridge scheduled rule for AWS Lambda functions - Amazon EventBridge
Use scheduled events to invoke a Lambda function - AWS SDK Code Examples
Understanding basic Lambda concepts - AWS Lambda
Relevant content
- AWS OFFICIALUpdated 8 months ago
Hello Jesus. I appreciate your answer. As you mentioned, I think it's the best option from a resource perspective. We're going to implement it this way. Thanks!
This solution runs once a day, but each id is processed individually. I am not sure where the requirement to execute the function once a day comes from, but possibly you could execute the processing lambda function as soon as a new Id is inserted in DynamoDB by leveraging DynamoDB streams - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.Lambda.html