Home > database >  Does AWS EventBridge rule with a schedule fire right away when it is enabled?
Does AWS EventBridge rule with a schedule fire right away when it is enabled?

Time:11-13

Have defined an EventBridge rule, which is disabled by default.

    new Rule(this, 'EmailRule', {
      ruleName: 'email-event-rule',
      description: 'trigger email handler',
      enabled: false,
      eventPattern: {
        detailType: ['email-event-rule-event']
      },
      targets: [new LambdaFunction(emailHandlerConstruct.getLambda())],
      schedule: Schedule.rate(Duration.hours(1))
    });

In a certain scenario it is enabled. But I assumed it would run after an hour of being enabled. But it triggers the lambda every time its enabled right away.

Is that how its designed or am I missing something? I did not see anywhere in the doc regarding the schedule of an event bridge rule when it is enabled.

CodePudding user response:

Good question. The doc only mentions that it fires right away on the rule creation. Therefore, this behaviour is undocumented and you are not missing anything.

CodePudding user response:

That is indeed how it is designed. When you create an Eventbridge rule that uses a rate expression instead of a schedule (such as your example), it will run the moment it is created and then run again after whatever period of time you specified has passed. In your example it seems like you want the rule to trigger the lambda every hour. When the rule gets created it then triggers the lambda right away, and then will do it again every hour after that. This means that if you deployed the rule at 1:30pm then it would repeat at 2:30pm, 3:30pm etc. etc.

Link to AWS documentation about rate expression Eventbridge rules: https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html

  • Related