Home > other >  Cloudwatch custom event triggered instantly even after passing schedule time AWS
Cloudwatch custom event triggered instantly even after passing schedule time AWS

Time:01-08

I am trying to use aws cloudwatch events for task scheduling in Node js, So , I created a cloudwatch event rule for this, Then I added a lambda function as a target. I am using event patterns to create a cloudwatch event rule here instead of scheduled expression. Now I am trying to add events to it at a scheduled time , but the event triggers instantly as soon as a put a event on cloudwatch. I don't know why this is happening, I researched a lot but didn't find anything relevant.

I used these official docs as reference :- https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchEvents.html#putEvents-property https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/cloudwatch-examples-sending-events.html

I am new to AWS , please help me here.

This is the code performing the above mentioned action.

var params = {
        Name: 'DEMO_EVENT',
        RoleArn: 'iamroleArn',
        EventPattern: JSON.stringify({
            source: ['myapp.events'],
        }),
        State: 'ENABLED'
      };
      
      cwevents.putRule(params, function(err, rule) {
        if (err) {
          console.log("Error", err);
        } else {
          console.log("Success", rule.RuleArn);
          var params = {
            Rule: 'DEMO_EVENT',
            Targets: [
              {
                Arn: 'lambda function arn',
                Id: "default"
              }
            ]
          };
          
          cwevents.putTargets(params, function(err, data) {
            if (err) {
              console.log("Error", err);
            } else {
              console.log("Success", data);
              var params = {
                Entries: [
                  {
                    Detail: JSON.stringify({
                      "meetingNo": "82565852285"
                    }),
                    DetailType: 'meeting',
                    Resources: [
                        rule.RuleArn
                    ],
                    Source: 'myapp.events',
                    Time: new Date("2022-01-07T08:00:00Z"), // here I am sending the schedule time in utc
                  }
                ]
              };
              
              cwevents.putEvents(params, function(err, data) {
                if (err) {
                  console.log("Error", err);
                } else {
                  console.log("Success", data.Entries);
                }
              });
            }
          });
        }
      });

CodePudding user response:

This is the expected behaviour. PutEvents puts events immediately. The Time parameter is not a scheduling mechanism, but rather an option for callers to provide a timestamp other than the time of the PutEvents call.

EventBridge (formerly CloudWatch Events) does have support for cron expressions and rate expressions, with the PutRule Command.

Scheduled rules generate events at set, periodic intervals

EventBridge PutRule scheduling works with regular recurring schedules. For instance, a rule with a 0/15 * * * ? * cron expression will generate an event with a generic payload 4x per hour every day for as long as the rule is enabled.

Alternative for arbitrary scheduling of payload delivery

If you are looking to raise events with custom detail at arbitrary times (e.g. do something with this order record in 5 days; or send an event on each user's birthday), then scheduled EventBridge rules are not sufficient. For such "deferred delivery" scenarios, you could use a scheduled event rule to periodically poll a data store for deliveries that are due. A PollingLambda, say, could in turn raise a custom event with {type:"HappyBirthday", name:"Zaphod", email:...} payload for each approaching birthday, which a second rule would send to a SendBirthdayGreetingLambda.

  •  Tags:  
  • Related