Currently, We are triggering our lambda function from S3 Upload. S3 upload is not time-specific, it can happen at any time.
Now we got a new requirement that we can run lambda within a certain time range only e.g. between 13 UTC to 20 UTC. That means if the s3 upload happened at 5 UTC, lambda will be triggered at 13 UTC.
What is the best approach to implement this change?
Any suggestions would be appreciated. Thanks in advance.
CodePudding user response:
If the condition is that the upload must be done between the time range specified, then nothing special needs to be done.
If you want the upload to trigger the lambda which will only be executed once the time range is met, then you could use AWS Step Functions. Using a Wait state, a task time can delay the invocation of the lambda function. The delay can be dynamic (e.g. 13 UTC - current time).
The workflow can look like this:
- File is uploaded to S3
- Notification gets sent to EventBridge
- EventBridge starts state machine
- Wait state for a certain time period
- Lambda gets triggered once wait state is done
See:
CodePudding user response:
Since the timing to trigger your Lambda function is now fixed, you can setup CloudWatch Trigger at the desired time and set the Lambda function as the source for that event rule.
Steps -
Open the Functions page of the Lambda console.
Choose a function
Under Function overview, choose Add trigger.
Set the trigger type to EventBridge (CloudWatch Events).
For Rule, choose Create a new rule.
Configure the remaining options and choose Add.
Rule is nothing but a cron pattern which you need to set for the desired time. Once the rule is implemented, the trigger will start according to the cron, and run the lambda function.