aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.71k stars 3.93k forks source link

aws-lambda: introduce `Function.addSchedule()` integration with EventBridge Scheduler #32266

Open garysassano opened 4 days ago

garysassano commented 4 days ago

Describe the feature

Currently, you need to write the following code to invoke a function on a schedule:

import { join } from "path";
import {
  Schedule,
  ScheduleExpression,
} from "@aws-cdk/aws-scheduler-alpha";
import { LambdaInvoke } from "@aws-cdk/aws-scheduler-targets-alpha";
import { Duration, Stack, StackProps } from "aws-cdk-lib";
import { Architecture, LoggingFormat, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const cronFunction = new NodejsFunction(this, `CronFunction`, {
      functionName: `cron-function`,
      entry: join(__dirname, "..", "functions", "cron", "index.ts"),
      runtime: Runtime.NODEJS_22_X,
      architecture: Architecture.ARM_64,
      memorySize: 1024,
      timeout: Duration.minutes(1),
      loggingFormat: LoggingFormat.JSON,
    });

    new Schedule(this, "RateBasedSchedule", {
      scheduleName: "rate-based-schedule",
      schedule: ScheduleExpression.rate(Duration.minutes(1)),
      target: new LambdaInvoke(cronFunction),
    });
  }
}

Use Case

The code could be optimized by introducing a native integration between AWS Lambda and EventBridge Scheduler.

Proposed Solution

import { join } from "path";
import { ScheduleExpression } from "@aws-cdk/aws-scheduler-alpha";
import { Duration, Stack, StackProps } from "aws-cdk-lib";
import { Architecture, LoggingFormat, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const cronFunction = new NodejsFunction(this, `CronFunction`, {
      functionName: `cron-function`,
      entry: join(__dirname, "..", "functions", "cron", "index.ts"),
      runtime: Runtime.NODEJS_22_X,
      architecture: Architecture.ARM_64,
      memorySize: 1024,
      timeout: Duration.minutes(1),
      loggingFormat: LoggingFormat.JSON,
    });

    cronFunction.addSchedule({
        schedule: ScheduleExpression.rate(Duration.minutes(1)),
    });
  }
}

Other Information

No response

Acknowledgements

CDK version used

2.170.0

Environment details (OS name and version, etc.)

Ubuntu 24.04 LTS

pahud commented 4 days ago

This is interesting and seems very useful. Making this a p2 now. Please help us prioritize with 👍 and we welcome PRs.

ashishdhingra commented 4 days ago

On a side note, should this be limited to only NodejsFunction? Also, @aws-cdk/aws-scheduler-targets-alpha is not stabilized yet, so we should probably wait to add dependency on experimental module.

garysassano commented 4 days ago

On a side note, should this be limited to only NodejsFunction?

No, it should apply to the generic IFunction interface like the other add methods.

image