Search code examples
amazon-web-servicesserverless-frameworkserverless

serverless - not able to trigger aws lambda using cloudwatch event


Currently my serverless.yml file looks like this:

service: bbb

provider:
  name: aws
  runtime: go1.x
  stage: dev

package:
  exclude:
    - ./**
  include:
    - ./bin/**
    
functions:
  ccc:
    handler: bin/executable
    name: my1minutelambda
    role: 
      'Fn::GetAtt':
        - mylambdaexecutionrole
        - Arn
            
resources:
  Resources:
    mylambdaexecutionrole:
      Type: AWS::IAM::Role
      Properties:
         RoleName: my-basiclambdaexec-role
         Description: This is my basiclambdaexecution role
         AssumeRolePolicyDocument:
           Version: "2012-10-17"
           Statement:
             -
               Effect: Allow
               Principal:
                 Service:
                     - "lambda.amazonaws.com"
               Action:
                 - "sts:AssumeRole"
         ManagedPolicyArns:
           - "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
    myminschedulerevent: 
      Type: AWS::Events::Rule
      Properties:
        Description: This is my 1 minute rate scheduler.
        Name: my-1-min-trigger-event-scheduler
        ScheduleExpression: rate(1 hour) 
        Targets: 
          - 
            Arn: "arn:aws:lambda:us-east-1:111111111111:function:my1minutelambda" #update your a/c Id
            Id: "TargetFunctionV1"

command used to deploy: sls deploy

After deployment finished, I can see on aws management console that all my resources got created.

BUT I am not able to see cloudwatch trigger extablishment for my lambda function.

See below screenshot:

  1. CloudWatch Event Rule created successfully. (Target section pointing to my lambda function)

enter image description here

  1. Trigger link not established for my lambda:

enter image description here

Please let me know what i am missing here. Thank you.

Update#1:

After adding following lines (as suggested by Marcin), I am able to see "CloudWatch event".

EventsPermission:
  Type: AWS::Lambda::Permission
  Properties:
    FunctionName: my1minutelambda
    Action: lambda:InvokeFunction
    Principal: events.amazonaws.com
    SourceAccount: !Ref 'AWS::AccountId'
    SourceArn: !GetAtt myminschedulerevent.Arn

But, I can't see CloudWatch logs!! So, I can't findout if my lambda function is executing. Please see image below:

enter image description here


Solution

  • I tried to replicate the issue using serverless framework.

    To do so I added the following AWS::Lambda::Permission to the end of your template:

        EventsPermission:
          Type: AWS::Lambda::Permission
          Properties:
            FunctionName: dsfgsdfg # <-- REPLACE this with your function name my1minutelambda
            Action: lambda:InvokeFunction
            Principal: events.amazonaws.com
            SourceArn: !GetAtt myminschedulrevent.Arn
    

    After adding the permissions, the console showed the trigger as expected:

    enter image description here