Search code examples
node.jsamazon-web-servicesaws-lambdaserverless-frameworkaws-event-bridge

Why is the Lambda function added as a target to Event Bridge Rule is not triggered?


I am trying to put a rule and a lambda target to the rule for EventBridge. The rule and the target are successfully added, however, the target Lambda is not triggered. And I cannot see the relevant Trigger when I navigate to the target Lambda function on AWS Console.

I'm using Serverless Framework to deploy the stack. The putRuleAndTarget lambda is the one that creates the rule and adds notifyUser as the target. Here's the relevant section of the serverless.ts file:

functions: {
      putRuleAndTarget: {
            handler: 'src/functions/putRuleAndTarget.handler',
            name: 'putRuleAndTarget'
      },
      notifyUser: {
            handler: 'src/functions/notifyUser.handler',
            name: 'notifyUser'
      }
}

This is the code for putRuleAndTarget (I'm using aws javascript sdk v3):

import { EventBridgeClient, PutRuleCommand, PutTargetsCommand, } from '@aws-sdk/client-eventbridge'
import type { PutRuleCommandInput, PutTargetsCommandInput } from '@aws-sdk/client-eventbridge'


const client = new EventBridgeClient({ region: 'eu-central-1' })

async function putRuleAndTarget( ) {
      try {
            const putRuleCommandInput: PutRuleCommandInput = {
                  Name: 'notifyUserRule',
                  Description: 'Schedule a lambda to send notification to the user',
                  ScheduleExpression: 'rate(1 minute)',
            }
            const putRuleCommand = new PutRuleCommand(putRuleCommandInput)
            const putRuleCommandOutput = await client.send(putRuleCommand)

            const putTargetsCommandInput: PutTargetsCommandInput = {
                  Rule: 'notifyUserRule',
                  Targets: [
                        {
                              Arn: 'arn:aws:lambda:eu-central-1:AccountID:function:notifyUser',
                              Id: 'notifyUser'
                        },
                  ]
            }
            const putTargetsCommand = new PutTargetsCommand(putTargetsCommandInput)
            const putTargetsCommandOutPut = await client.send(putTargetsCommand)
      

      } catch (error) {
            console.log('[ putRuleAndTarget error ]', error)
      }

}

export const handler = putRuleAndTarget

The following screenshots are taken after running putRuleAndTarget The created Rule on AWS EventBridge Console: The rule on EventBridge Console

The details of the rule showing the target: Rule details and Target on EventBridge Console

notifyUser function on Lambda Console, note that the trigger is missing: notifyUser function on Lambda Console


Moreover, running sls logs -f notifyUser does not bring about any log statements, the function is never invoked, though it is supposed to run every 1 minute.


Solution

  • It looks like you may be missing the permissions. The rule needs to have permission to invoke the lambda.

    This tutorial shows the steps. In particular, check out the portion that covers

    AWS::Lambda::Permission
    

    Example for request in comments:

     LambdaInvokePermissionsExample:
        Type: AWS::Lambda::Permission
        Properties:
          Action: lambda:InvokeFunction
          FunctionName:
            Fn::GetAtt:
              - NameOfYourLambdaResource
              - Arn
          Principal: events.amazonaws.com
          SourceArn:
            Fn::GetAtt:
              - EventRuleResourceName
              - Arn