1
votes

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

2
But does it work? Lambda console sometimes does not show the triggers, even though they work.Marcin
Have you defined an aws permission: Type: AWS::Lambda::Permission for cloudwatch event to invoke the lambda in your serverless.yaml file? docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…salazarin
@Marcin - I click on "Monitoring "-> "View logs in CloudWatch". But i don't see any "Log stream" for my lambda function.Jatin
@salazarin: I have attached my serverless.yml file. I think, i have not defined any such permission. Can you please help me with the updated serverless.yml file or the missing yml section? I am pretty new to aws.Jatin

2 Answers

1
votes

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

0
votes

If all you are trying to do is get a Lambda function to execute on a schedule, the Serverless Framework already includes an event type expressly for that purpose:

functions:
  crawl:
    handler: crawl
    events:
      - schedule: rate(2 hours)
      - schedule: cron(0 12 * * ? *)

It will set everything up for you with no need to add additional CloudFormation. You can find the documentation here: https://www.serverless.com/framework/docs/providers/aws/events/schedule/#schedule/