Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: events.Schedule.expression does not work with cron expressions #32753

Closed
1 task
datamystic opened this issue Jan 6, 2025 · 6 comments
Closed
1 task
Assignees
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. p3

Comments

@datamystic
Copy link

Describe the bug

Event Bridge rules do not parse any cron expressions correctly, and some rate expressions.

Some rate strings work correctly e.g.:
rate(5 minutes)
but this does not:
rate(1 minutes)

Similar errors occur for the expected 6-field cron expressions e.g.:
cron(* * * * ? *)
cron(0 50 0,6,12,18 * ? *)

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

The expressions should be parsed correctly.

Current Behavior

Resource handler returned message: "Parameter ScheduleExpression is not valid. (Service: EventBridge, Status Code: 400, Request ID: 80fb7dec-5fcc-4c64-bd98-229b64c4c09f)" (RequestToken: 2195b1a9-2f62-ed3c-4395-c0ea64a323e8, HandlerErrorCode: GeneralServiceException)

Reproduction Steps

import * as events from 'aws-cdk-lib/aws-events';
.
.
.

this.cronRule( logGroup, 'JadeMergeExercise', 'http://app.jadediabetes.com/admin-sac/merge_exercise.php', 'jade/merge-exercise', 'rate(1 minutes)' ) ;

.

cronRule( logGroup : cloudwatch.LogGroup, name : string, website : string, log_stream : string, cron_schedule : string )
{

return new events.Rule(this, 'Cron' + name, {
  ruleName: 'Cron' + name,

  // Schedule expression to run every 5 minutes 
  schedule: events.Schedule.expression( cron_schedule ),  
  
  // Target the Lambda function
  targets: [
    new targets.LambdaFunction(this.lambdaWebScraper, {
    })
  ],

});

}

}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.174.0 (build 9604329)

Framework Version

No response

Node.js Version

20.11.0

OS

Windows 10

Language

TypeScript

Language Version

No response

Other information

No response

@datamystic datamystic added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 6, 2025
@github-actions github-actions bot added the @aws-cdk/aws-events Related to CloudWatch Events label Jan 6, 2025
@ashishdhingra ashishdhingra self-assigned this Jan 6, 2025
@ashishdhingra ashishdhingra added p2 needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels Jan 6, 2025
@ashishdhingra
Copy link
Contributor

@datamystic Good morning. Somehow, I'm unable to reproduce the issue. Used below code:

import * as cdk from 'aws-cdk-lib';
import * as redshiftserverless from 'aws-cdk-lib/aws-redshiftserverless'
import * as events from 'aws-cdk-lib/aws-events';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as targets from 'aws-cdk-lib/aws-events-targets';

export class CdktestStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const workgroup = new redshiftserverless.CfnWorkgroup(this, 'RedshiftWorkgroup', {
      workgroupName: 'TestRedshiftWorkgroup'
    });
    
    const rule5MinuteRate = new events.Rule(this, '5MinuteRateRule', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
    });

    const rule1MinuteRate = new events.Rule(this, '1MinuteRateRule', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
    });
    
    const ruleCron1 = new events.Rule(this, 'CronRule1', {
      schedule: events.Schedule.cron({
        minute: '*',
        hour: '*',
        day: '*',
        month: '*',
        //weekDay: '?',
        year: '*'
      }),
    });

    const ruleCron2 = new events.Rule(this, 'CronRule2', {
      schedule: events.Schedule.cron({
        minute: '0',
        hour: '50',
        day: '0,6,12,18',
        month: '*',
        //weekDay: '?',
        year: '*'
      }),
    });

    const dlq = new sqs.Queue(this, 'DeadLetterQueue');
    const target = new targets.RedshiftQuery(workgroup.attrWorkgroupWorkgroupArn, {
      database: 'dev',
      deadLetterQueue: dlq,
      sql: ['SELECT * FROM foo','SELECT * FROM baz'],
    })

    rule5MinuteRate.addTarget(target);
    
    rule1MinuteRate.addTarget(target);

    ruleCron1.addTarget(target);

    ruleCron2.addTarget(target);
  }
}

This synthesizes successfully to below CFN template:

Resources:
  RedshiftWorkgroup:
    Type: AWS::RedshiftServerless::Workgroup
...
  5MinuteRateRuleD8FB2596:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: rate(5 minutes)
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - RedshiftWorkgroup
              - Workgroup.WorkgroupArn
          DeadLetterConfig:
            Arn:
              Fn::GetAtt:
                - DeadLetterQueue9F481546
                - Arn
          Id: Target0
          RedshiftDataParameters:
            Database: dev
            Sqls:
              - SELECT * FROM foo
              - SELECT * FROM baz
          RoleArn:
            Fn::GetAtt:
              - 5MinuteRateRuleEventsRoleC75E085E
              - Arn
    Metadata:
      aws:cdk:path: CdktestStack/5MinuteRateRule/Resource
  5MinuteRateRuleEventsRoleC75E085E:
    Type: AWS::IAM::Role
    Properties:
...
  5MinuteRateRuleEventsRoleDefaultPolicy7BB6A11E:
    Type: AWS::IAM::Policy
    Properties:
...
  1MinuteRateRule874DCF22:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: rate(1 minute)
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - RedshiftWorkgroup
              - Workgroup.WorkgroupArn
          DeadLetterConfig:
            Arn:
              Fn::GetAtt:
                - DeadLetterQueue9F481546
                - Arn
          Id: Target0
          RedshiftDataParameters:
            Database: dev
            Sqls:
              - SELECT * FROM foo
              - SELECT * FROM baz
          RoleArn:
            Fn::GetAtt:
              - 1MinuteRateRuleEventsRoleC88407F2
              - Arn
    Metadata:
      aws:cdk:path: CdktestStack/1MinuteRateRule/Resource
  1MinuteRateRuleEventsRoleC88407F2:
    Type: AWS::IAM::Role
    Properties:
...
  1MinuteRateRuleEventsRoleDefaultPolicy25A34B6E:
    Type: AWS::IAM::Policy
    Properties:
...
  CronRule1E407A55B:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: cron(* * * * ? *)
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - RedshiftWorkgroup
              - Workgroup.WorkgroupArn
          DeadLetterConfig:
            Arn:
              Fn::GetAtt:
                - DeadLetterQueue9F481546
                - Arn
          Id: Target0
          RedshiftDataParameters:
            Database: dev
            Sqls:
              - SELECT * FROM foo
              - SELECT * FROM baz
          RoleArn:
            Fn::GetAtt:
              - CronRule1EventsRole1AADBDF2
              - Arn
    Metadata:
      aws:cdk:path: CdktestStack/CronRule1/Resource
  CronRule1EventsRole1AADBDF2:
    Type: AWS::IAM::Role
    Properties:
...
  CronRule1EventsRoleDefaultPolicy19EAA4A5:
    Type: AWS::IAM::Policy
    Properties:
...
  CronRule29EC7AECF:
    Type: AWS::Events::Rule
    Properties:
      ScheduleExpression: cron(0 50 0,6,12,18 * ? *)
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - RedshiftWorkgroup
              - Workgroup.WorkgroupArn
          DeadLetterConfig:
            Arn:
              Fn::GetAtt:
                - DeadLetterQueue9F481546
                - Arn
          Id: Target0
          RedshiftDataParameters:
            Database: dev
            Sqls:
              - SELECT * FROM foo
              - SELECT * FROM baz
          RoleArn:
            Fn::GetAtt:
              - CronRule2EventsRoleBEB739EA
              - Arn
    Metadata:
      aws:cdk:path: CdktestStack/CronRule2/Resource
  CronRule2EventsRoleBEB739EA:
    Type: AWS::IAM::Role
    Properties:
...
  CronRule2EventsRoleDefaultPolicy6801243E:
    Type: AWS::IAM::Policy
    Properties:
      PolicyDocument:
...
  DeadLetterQueue9F481546:
    Type: AWS::SQS::Queue
...

If you are getting error Error: Cannot supply both 'day' and 'weekDay', use at most one, it is expected per note You can't specify the Day-of-month and Day-of-week fields in the same cron expression. If you specify a value or a * (asterisk) in one of the fields, you must use a ? (question mark) in the other. at Using cron and rate expressions to schedule rules in Amazon EventBridge. Somehow specifying ? doesn't work as obvious of commented code //weekDay: '?', above.

Please confirm:

  • If your issue inability to specify ? in one of the fields when using both fields?
  • Do you get error during synthesis or deployment? Deployment errors are usually from CloudFormation if CDK template correctly synthesizes.

Thanks,
Ashish

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-reproduction This issue needs reproduction. labels Jan 7, 2025
Copy link

github-actions bot commented Jan 9, 2025

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jan 9, 2025
@datamystic
Copy link
Author

Ah, I think the error must be me. I'm sure I read somewhere that the cron expression can be provided as a string - which is where I had issues. In your sample above you are not doing this, so I assume that feature does not exist?

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jan 10, 2025
@ashishdhingra
Copy link
Contributor

Ah, I think the error must be me. I'm sure I read somewhere that the cron expression can be provided as a string - which is where I had issues. In your sample above you are not doing this, so I assume that feature does not exist?

@datamystic The CDK L2 construct's aws_events.Schedule.cron(options) doesn't have any overload that accepts string as parameter. You might be referring to CfnRule L1 construct, where for scheduleExpression is modeled with string data type. When Rule L2 construct is synthesized, it returns L1 CfnRule behind the scenes, with the cron() schedule converted to string (refer here, it returns LiteralSchedule).

Thanks,
Ashish

@ashishdhingra ashishdhingra added closing-soon This issue will automatically close in 4 days unless further comments are made. p3 and removed p2 labels Jan 10, 2025
@datamystic
Copy link
Author

Thanks @ashishdhingra - it would be great if it accepted a string cron expression or rate expression.

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. closing-soon This issue will automatically close in 4 days unless further comments are made. p3
Projects
None yet
Development

No branches or pull requests

2 participants