Sunday, January 9, 2022

[SOLVED] Every-Monday and Every-month cron expression doesn't work properly

Issue

I've created a lambda and EventBridge stack with cloudformation, I have two lambda with exact same cloudformation template with different cron expressions, one is cron(0 8 1 * ? *) for every month 8:00Am and one is cron(0 16 ? * 2 *) for every monday 4:00Pm, but my lambda are not logging means they haven't been executed, here's the cloudformation template.yml:

Transform: AWS::Serverless-2016-10-31

Parameters:
  ImageUri:
    Type: String
  LambdaName: 
    Type: String
  RoleName:
    Type: String
  DatabaseHost:
    Type: String
  DatabaseUsername:
    Type: String
  DatabasePassword:
    Type: String
  DatabaseName:
    Type: String
  SQSQueueUrl:
    Type: String

  

Resources:
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Join ['/', ['/aws/lambda', !Ref LambdaName]]

  ScheduledRule:
    Type: AWS::Events::Rule
    Properties:
      Description: "Montly Collision Cronjob"
      ScheduleExpression: "cron(0 8 1 * ? *)"
      State: "ENABLED"
      Targets:
        -
          Arn:
            Fn::GetAtt:
              - LambdaFunction
              - Arn
          Id: !Join
          - ''
          - - "monthly-collision-cronjob"
            - !Ref LambdaName
  
  LambdaPermission:
      Type: "AWS::Lambda::Permission"
      Properties:
          Action: "lambda:InvokeFunction"
          FunctionName: !Join
          - ''
          - - !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:"
            - !Ref LambdaName
          Principal: "events.amazonaws.com"
          SourceArn: !GetAtt ScheduledRule.Arn

  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Ref RoleName
      AssumeRolePolicyDocument:
        Statement:
          - Action:
            - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
              - lambda.amazonaws.com
        Version: 2012-10-17
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole # For cloudwatch logging
        - arn:aws:iam::aws:policy/AmazonSQSFullAccess

  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Ref LambdaName
      PackageType: Image
      MemorySize: 256
      Timeout: 45
      ImageUri: !Ref ImageUri
      Environment:
        Variables:
          DATABASE_HOST: !Ref DatabaseHost 
          DATABASE_USERNAME: !Ref DatabaseUsername
          DATABASE_PASSWORD: !Ref DatabasePassword
          DATABASE_NAME: !Ref DatabaseName
          SQS_QUEUE_URL: !Ref SQSQueueUrl
      Role: !GetAtt
        - LambdaRole
        - Arn

Any help is more than welcome!


Solution

First of all, what you can do to save code is to directly use the Event property of the serverless function:

  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Ref LambdaName
      PackageType: Image
      MemorySize: 256
      Timeout: 45
      ImageUri: !Ref ImageUri
      Environment:
        Variables:
          DATABASE_HOST: !Ref DatabaseHost 
          DATABASE_USERNAME: !Ref DatabaseUsername
          DATABASE_PASSWORD: !Ref DatabasePassword
          DATABASE_NAME: !Ref DatabaseName
          SQS_QUEUE_URL: !Ref SQSQueueUrl
      Role: !GetAtt
        - LambdaRole
        - Arn
      Events:
        EveryFirstOfTheMonth:
          Type: Schedule
          Properties:
            Schedule: cron(0 8 1 * ? *)

Your cron expressions look right, I am not sure what the problem is, but maybe you can try this solution and it works.



Answered By - Robert Kossendey