amazon-web-servicesaws-cloudformationaws-cdkaws-cdk-typescriptaws-service-catalog

Take CFN stack creation time for AWS EB scheduler, not project build time


I have a problem working with AWS CDK JS Event Bridge Scheduler, I have a service catalog "ProductStack" and I defined the one-time scheduler in it, so ONLY when the user will provision that product - the scheduler will be created. Now, the problem is, I'm using js date for taking that provision time and adding 5 minutes to it, but it takes the build time of the whole CFN stack and not the provision time of the "ProductStack" defined in it. With this I end up with a situation, when the scheduler itself is created for ex. at 10:00, but it is set to be fired at 09:55. Here's the short definition for my scheduler:

this.scheduler = new CfnSchedule(this, `Something`, {
  flexibleTimeWindow: { mode: 'OFF' },
  scheduleExpression: `at(${ moment.utc().add(2, 'minutes').format('YYYY-MM-DDTHH:mm:ss') })`,
  scheduleExpressionTimezone: TimeZone.ETC_UTC.timezoneName,
  ...
});

instead of moment.utc() I used date-fns and js simple new Date() as well, but all of them end up with the same result.


Solution

  • If your goal is to "do something after the stack is deployed", consider instead creating an EventBridge rule that listens to CREATE_COMPLETE stack events that CloudFormation emits to the default event bus. The rule would trigger a target (e.g. Lambda) that runs when the stack is created.

    However, if you really need to create a one-time scheduled event relative to the stack deploy time, you could shift the work of creating the schedule to a Custom Resource. You'd write a Lambda with an SDK CreateSchedule command, passing the current time from the Lambda runtime. Hooked up as a Custom Function handler, the Lambda would be invoked at deploy time by CloudFormation.