Adnan Mohib
Adnan Mohib

Reputation: 369

Run AWS Lambda for multiple parameters one at a time on schedule

I have a lambda function that accepts a parameter i.e a category_id, pulls some data from an API, and updates the database based on the response.

I have to execute the same lambda function for Multiple Ids after an interval of 1 minute on daily basis. For example, run lambda for category 1 at 12:00 AM, then run for category 2 at 12:01 AM and so one for 500+ categories.

What could be the best possible solution to achieve this?
This is what I am currently thinking:

Problems in the Above Solution:

Other solutions (not sure if they can be used):

Looking forward to hearing about the best solution.

Upvotes: 2

Views: 3506

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269091

Given that you are doing a large amount of processing, an Amazon EC2 instance might be more appropriate.

If the bandwidth requirements are low (eg if it is just making API calls), then a T3a.micro ($0.0094 per Hour) or even T3a.nano instance ($0.0047 per Hour) can be quite cost-effective.

A script running on the instance could process a category, then sleep for 30 seconds, in a big loop. Running 500 categories at one minute each would take about 8 hours. That's under 10c each day!

The instance can then stop or self-terminate when the work is complete. See: Auto-Stop EC2 instances when they finish a task - DEV Community

Upvotes: 3

John Rotenstein
John Rotenstein

Reputation: 269091

I would suggest using a standard Worker pattern:

  • Create an Amazon SQS queue
  • Configure the AWS Lambda function so that it is triggered to run whenever a message is sent to the SQS queue
  • Trigger a separate process at midnight (eg another Lambda function) that sends the 500 messages to the SQS queue, each with a different category ID

This will cause the Amazon SQS functions to execute. If you only want one of the Lambda functions to be running at any time (with no parallel executions), set the function's Concurrency Limit to 1 so that only one is running at any time. When one function completes, Lambda will automatically grab another message from the queue and start executing. There will be practically no "wasted time" between executions of the function.

Upvotes: 7

Related Questions