I have a lambda function that I wanted to run everyday at sunset. I tried using eventbridge to trigger it every day and then used time.sleep()
to wait until sunset, but it turns out lambda functions timeout after at most 15 minutes. And as I don't live near the equator, sunset throughout the year varies by more than that. Is there anyway to invoke a lambda function at every sunset? (Preferably it should be free as I'm trying to only use AWS free tier.)
CodePudding user response:
You can schedule invocations of Lambda functions using an Event Bridge rule. The limitation there is that you have to specify a cron or rate expression which gives you a fixed pattern - every day at 7pm for example.
To get around that, you could create another Lambda which determines what time sunset is through an API and then uses the applicable AWS SDK to update your Event Bridge rule with a cron expression that matches the time for sunset.
Of course, you'd need to schedule this Lambda too, using an Event Bridge rule to call it at the same time every morning perhaps?
CodePudding user response:
Sunset time changes everyday, so you're going to have to retrieve it on a daily basis (I believe, I'm not an astronomer).
I would create a lambda function that fetches the sunset time, and I would invoke this lambda function daily with an EventBridge rule (in the morning). This lambda function would use the retrieved sunset time to create another EventBridge rule via the boto3 api which invokes the lambda you already have at the desired time. You could also have the lambda function delete already existing rules when it runs before creating the new one.