-1

I am new to aws and trying to figure out a problem. I have to create a lambda which should get triggered every 5th and (5+1)th minute.

I m using event bridge and have create two rules. First one works fine but second rule doesn't. Please find some sample code below. Any suggestions would be helpful.

Rule.Builder.create(stack.getStack(), id + "QueueEventsRuleN")
        .ruleName("my-rule-n-" + stack.getParameter(EParameter.APP_NAME).getValueAsString()
                + "-" + stack.getParameter(EParameter.TARGET_ENVIRONMENT).getValueAsString() + "-cdk" + stack.getDeploymentSuffix())
        .description("schedule events to trigger Lambda every "+n+" minutes")
        .schedule(Schedule.expression("rate("+n+" minutes)"))
        .targets(singletonList(LambdaFunction.Builder.create(function).build()))
        .build();

int scheduleTime = n+1;
Rule.Builder.create(stack.getStack(), id + "QueueEventsRuleNPlusOne")
        .ruleName("my-rule-n-plus-one" + stack.getParameter(EParameter.APP_NAME).getValueAsString()
                + "-" + stack.getParameter(EParameter.TARGET_ENVIRONMENT).getValueAsString() + "-cdk" + stack.getDeploymentSuffix())
        .description("schedule events to trigger Lambda every "+scheduleTime+" minutes")
        .schedule(Schedule.expression("rate("+scheduleTime+" minutes)"))
        .targets(singletonList(LambdaFunction.Builder.create(function).build()))
        .build(); 
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
CK30
  • 9
  • 1

1 Answers1

0

Judging from the question, you are able to create the rule successfully, the only issue is lambda not being triggered by the event bridge.

Lambda Permission (Triggers)

Check if the target lambda has permission to be triggered from the event bridge. by default, if you create a rule and add a target from the console, the AWS console takes care of adding lambda permissions for you. It can reviewed in lambda console > lambda permission > settings > Triggers

To add lambda permissions from AWS cli

aws lambda add-permission \
      --function-name {your-lambda-function-name} \
      --statement-id {anything-random-alhanumeric} \
      --action 'lambda:InvokeFunction' \
      --principal events.amazonaws.com \
      --source-arn arn:aws:events:*:{aws-account-id}:rule/*.

replace {} info with actual data. It will allow lambda to be triggered from any rule in your AWS account.

Refer to the AWS docs, on an in-depth guide

https://aws.amazon.com/premiumsupport/knowledge-center/eventbridge-lambda-not-triggered/

Nick
  • 179
  • 1
  • 10