Schedule SQS to trigger lambda


I need to schedule my sqs notification that when files are uploaded to s3 in a period, say it's from 11a.m to 12p.m, I need to get a notification only about the files uploaded within that period, and as a continuation, a lambda trigger should happen. I tried creating an Eventbridge scheduler but couldn't get the desired output. Can you help me with this use case and the steps to follow?

2 Answers

I can think of two ways to do this - there are probably others:

  1. Have EventBridge schedule a Lambda function for 10:55AM. This function creates the S3 trigger that sends notifications to SQS when files are uploaded. Have a second EventBridge schedule for another Lambda that deletes the S3 trigger at 12:05PM.
  2. Have the code in the Lambda function which is receiving the notifications look at the time and ignore events that come in outside the required time window.
profile pictureAWS
answered 2 months ago

1)Set Up Amazon S3 Event Notifications: Configure S3 to send event notifications to CloudWatch Events when files are uploaded. You can do this by setting up an S3 event notification for the bucket.

2)Create a CloudWatch Events Rule: 

Create a CloudWatch Events rule that triggers based on the S3 event. In the rule, specify the schedule to trigger within the desired time range (e.g., 11a.m to 12p.m).

3)Add a Target to the Rule: 

Add a Lambda function as the target for the CloudWatch Events rule. This Lambda function will be triggered when the rule matches the specified schedule.

  1. Implement Lambda Function: Write the Lambda function code to process the S3 events. In the Lambda function, you can filter out the events based on the timestamp to only process the files uploaded within the specified time range.

Here's a more detailed breakdown: Step 1: Set Up Amazon S3 Event Notifications

Go to the S3 console.
Select the bucket for which you want to receive notifications.
Click on "Properties" and then "Events".
Click "Add notification".
Configure the event type (e.g., "All object create events") and destination as CloudWatch Events.

Step 2: Create a CloudWatch Events Rule

Go to the CloudWatch console.
Click on "Rules" in the left navigation pane.
Click on "Create rule".
Choose "Event Source" as "Event Pattern".
For the "Event Pattern", select "Service Name" as "S3" and "Event Type" as "Object Created (All)".
In the "Targets" section, choose "Lambda function" and select your Lambda function.
Specify the desired schedule under the "Schedule" section. For example, you can use a cron expression to specify the time range (e.g., cron(0 11-12 * * ? *) for every minute between 11a.m and 12p.m).

Step 3: Implement Lambda Function

Create or update your Lambda function to process the S3 events.
Within the Lambda function, filter the events based on the timestamp to process only the files uploaded within the specified time range.
Perform any desired processing on the files (e.g., send notifications).

With this setup, your Lambda function will be triggered by CloudWatch Events according to the specified schedule, and it will only process the S3 events for files uploaded within the designated time period.

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions