@aws-cdk/aws-scheduler-targets-alpha

中文文档
2.186.0-alpha.0发布于 2025/03/27Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 3.5万

This module is deprecated. All constructs are now available under aws-cdk-lib/aws-scheduler-targets.

此软件包已被标记为弃用

This package has been stabilized and moved to aws-cdk-lib

原始使用文档
文档目录13 节
  1. Invoke a Lambda function
  2. Start an AWS Step Function
  3. Start a CodeBuild job
  4. Send a Message To an SQS Queue
  5. Publish messages to an Amazon SNS topic
  6. Send events to an EventBridge event bus
  7. Start an Amazon Inspector assessment run
  8. Put a record to an Amazon Kinesis Data Stream
  9. Put a record to an Amazon Data Firehose
  10. Start a CodePipeline execution
  11. Start a SageMaker pipeline execution
  12. Schedule an ECS task run
  13. Invoke a wider set of AWS API

Amazon EventBridge Scheduler Targets Construct Library


Deprecated

This API may emit warnings. Backward compatibility is not guaranteed.


All constructs moved to aws-cdk-lib/aws-scheduler-targets.

Amazon EventBridge Scheduler is a feature from Amazon EventBridge that allows you to create, run, and manage scheduled tasks at scale. With EventBridge Scheduler, you can schedule millions of one-time or recurring tasks across various AWS services without provisioning or managing underlying infrastructure.

This library contains integration classes for Amazon EventBridge Scheduler to call any number of supported AWS Services.

The following targets are supported:

  1. targets.LambdaInvoke: Invoke an AWS Lambda function
  2. targets.StepFunctionsStartExecution: Start an AWS Step Function
  3. targets.CodeBuildStartBuild: Start a CodeBuild job
  4. targets.SqsSendMessage: Send a Message to an Amazon SQS Queue
  5. targets.SnsPublish: Publish messages to an Amazon SNS topic
  6. targets.EventBridgePutEvents: Put Events on EventBridge
  7. targets.InspectorStartAssessmentRun: Start an Amazon Inspector assessment run
  8. targets.KinesisStreamPutRecord: Put a record to an Amazon Kinesis Data Stream
  9. targets.FirehosePutRecord: Put a record to an Amazon Data Firehose
  10. targets.CodePipelineStartPipelineExecution: Start a CodePipeline execution
  11. targets.SageMakerStartPipelineExecution: Start a SageMaker pipeline execution
  12. targets.EcsRunTask: Start a new ECS task
  13. targets.Universal: Invoke a wider set of AWS API

Invoke a Lambda function

Use the LambdaInvoke target to invoke a lambda function.

The code snippet below creates an event rule with a Lambda function as a target called every hour by EventBridge Scheduler with a custom payload. You can optionally attach a dead letter queue.

import * as lambda from 'aws-cdk-lib/aws-lambda';

const fn = new lambda.Function(this, 'MyFunc', {
    runtime: lambda.Runtime.NODEJS_LATEST,
    handler: 'index.handler',
    code: lambda.Code.fromInline(`exports.handler = handler.toString()`),
});

const dlq = new sqs.Queue(this, "DLQ", {
    queueName: 'MyDLQ',
});

const target = new targets.LambdaInvoke(fn, {
    deadLetterQueue: dlq,
    maxEventAge: Duration.minutes(1),
    retryAttempts: 3,
    input: ScheduleTargetInput.fromObject({
        'payload': 'useful'
    }),
});

const schedule = new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.hours(1)),
    target
});

Start an AWS Step Function

Use the StepFunctionsStartExecution target to start a new execution on a StepFunction.

The code snippet below creates an event rule with a Step Function as a target called every hour by EventBridge Scheduler with a custom payload.

import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';

const payload = {
  Name: "MyParameter",
  Value: '🌥️',
};

const putParameterStep = new tasks.CallAwsService(this, 'PutParameter', {
  service: 'ssm',
  action: 'putParameter',
  iamResources: ['*'],
  parameters: {
    "Name.$": '$.Name',
    "Value.$": '$.Value',
    Type: 'String',
    Overwrite: true,
  },
});

const stateMachine = new sfn.StateMachine(this, 'StateMachine', {
  definitionBody: sfn.DefinitionBody.fromChainable(putParameterStep)
});

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.hours(1)),
  target: new targets.StepFunctionsStartExecution(stateMachine, {
    input: ScheduleTargetInput.fromObject(payload),
  }),
});

Start a CodeBuild job

Use the CodeBuildStartBuild target to start a new build run on a CodeBuild project.

The code snippet below creates an event rule with a CodeBuild project as target which is called every hour by EventBridge Scheduler.

import * as codebuild from 'aws-cdk-lib/aws-codebuild';

declare const project: codebuild.Project;

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.CodeBuildStartBuild(project),
});

Send a Message To an SQS Queue

Use the SqsSendMessage target to send a message to an SQS Queue.

The code snippet below creates an event rule with an SQS Queue as a target called every hour by EventBridge Scheduler with a custom payload.

Contains the messageGroupId to use when the target is a FIFO queue. If you specify a FIFO queue as a target, the queue must have content-based deduplication enabled.

const payload = 'test';
const messageGroupId = 'id';
const queue = new sqs.Queue(this, 'MyQueue', {
  fifo: true,
  contentBasedDeduplication: true,
});

const target = new targets.SqsSendMessage(queue, {
    input: ScheduleTargetInput.fromText(payload),
    messageGroupId,
});

new Schedule(this, 'Schedule', {
    schedule: ScheduleExpression.rate(Duration.minutes(1)),
    target
});

Publish messages to an Amazon SNS topic

Use the SnsPublish target to publish messages to an Amazon SNS topic.

The code snippets below create an event rule with a Amazon SNS topic as a target. It's called every hour by Amazon EventBridge Scheduler with a custom payload.

import * as sns from 'aws-cdk-lib/aws-sns';

const topic = new sns.Topic(this, 'Topic');

const payload = {
  message: 'Hello scheduler!',
};

const target = new targets.SnsPublish(topic, {
  input: ScheduleTargetInput.fromObject(payload),
});

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.hours(1)),
  target,
});

Send events to an EventBridge event bus

Use the EventBridgePutEvents target to send events to an EventBridge event bus.

The code snippet below creates an event rule with an EventBridge event bus as a target called every hour by EventBridge Scheduler with a custom event payload.

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

const eventBus = new events.EventBus(this, 'EventBus', {
  eventBusName: 'DomainEvents',
});

const eventEntry: targets.EventBridgePutEventsEntry = {
  eventBus,
  source: 'PetService',
  detail: ScheduleTargetInput.fromObject({ Name: 'Fluffy' }),
  detailType: '🐶',
};

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.hours(1)),
  target: new targets.EventBridgePutEvents(eventEntry),
});

Start an Amazon Inspector assessment run

Use the InspectorStartAssessmentRun target to start an Inspector assessment run.

The code snippet below creates an event rule with an assessment template as the target which is called every hour by EventBridge Scheduler.

import * as inspector from 'aws-cdk-lib/aws-inspector';

declare const cfnAssessmentTemplate: inspector.CfnAssessmentTemplate;

const assessmentTemplate = inspector.AssessmentTemplate.fromCfnAssessmentTemplate(this, 'MyAssessmentTemplate', cfnAssessmentTemplate);

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.InspectorStartAssessmentRun(assessmentTemplate),
});

Put a record to an Amazon Kinesis Data Stream

Use the KinesisStreamPutRecord target to put a record to an Amazon Kinesis Data Stream.

The code snippet below creates an event rule with a stream as the target which is called every hour by EventBridge Scheduler.

import * as kinesis from 'aws-cdk-lib/aws-kinesis';

const stream = new kinesis.Stream(this, 'MyStream');

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.KinesisStreamPutRecord(stream, {
    partitionKey: 'key',
  }),
});

Put a record to an Amazon Data Firehose

Use the FirehosePutRecord target to put a record to an Amazon Data Firehose delivery stream.

The code snippet below creates an event rule with a delivery stream as a target called every hour by EventBridge Scheduler with a custom payload.

import * as firehose from 'aws-cdk-lib/aws-kinesisfirehose';
declare const deliveryStream: firehose.IDeliveryStream;

const payload = {
  Data: "record",
};

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.FirehosePutRecord(deliveryStream, {
    input: ScheduleTargetInput.fromObject(payload),
  }),
});

Start a CodePipeline execution

Use the CodePipelineStartPipelineExecution target to start a new execution for a CodePipeline pipeline.

The code snippet below creates an event rule with a CodePipeline pipeline as the target which is called every hour by EventBridge Scheduler.

import * as codepipeline from 'aws-cdk-lib/aws-codepipeline';

declare const pipeline: codepipeline.Pipeline;

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.CodePipelineStartPipelineExecution(pipeline),
});

Start a SageMaker pipeline execution

Use the SageMakerStartPipelineExecution target to start a new execution for a SageMaker pipeline.

The code snippet below creates an event rule with a SageMaker pipeline as the target which is called every hour by EventBridge Scheduler.

import * as sagemaker from 'aws-cdk-lib/aws-sagemaker';

declare const pipeline: sagemaker.IPipeline;

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.SageMakerStartPipelineExecution(pipeline, {
    pipelineParameterList: [{
      name: 'parameter-name',
      value: 'parameter-value',
    }],
  }),
});

Schedule an ECS task run

Use the EcsRunTask target to schedule an ECS task run for a cluster.

The code snippet below creates an event rule with a Fargate task definition and cluster as the target which is called every hour by EventBridge Scheduler.

import * as ecs from 'aws-cdk-lib/aws-ecs';

declare const cluster: ecs.ICluster;
declare const taskDefinition: ecs.FargateTaskDefinition;

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(cdk.Duration.minutes(60)),
  target: new targets.EcsRunFargateTask(cluster, {
    taskDefinition,
  }),
});

The code snippet below creates an event rule with a EC2 task definition and cluster as the target which is called every hour by EventBridge Scheduler.

import * as ecs from 'aws-cdk-lib/aws-ecs';

declare const cluster: ecs.ICluster;
declare const taskDefinition: ecs.Ec2TaskDefinition;

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(cdk.Duration.minutes(60)),
  target: new targets.EcsRunEc2Task(cluster, {
    taskDefinition,
  }),
});

Invoke a wider set of AWS API

Use the Universal target to invoke AWS API. See https://docs.aws.amazon.com/scheduler/latest/UserGuide/managing-targets-universal.html

The code snippet below creates an event rule with AWS API as the target which is called at midnight every day by EventBridge Scheduler.

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.cron({
    minute: '0',
    hour: '0',
  }),
  target: new targets.Universal({
    service: 'rds',
    action: 'stopDBCluster',
    input: ScheduleTargetInput.fromObject({
      DbClusterIdentifier: 'my-db',
    }),
  }),
});

The service must be in lowercase and the action must be in camelCase.

By default, an IAM policy for the Scheduler is extracted from the API call. The action in the policy is constructed using the service and action prop. Re-using the example above, the action will be rds:stopDBCluster. Note that not all IAM actions follow the same pattern. In such scenario, please use the policyStatements prop to override the policy:

new Schedule(this, 'Schedule', {
  schedule: ScheduleExpression.rate(Duration.minutes(60)),
  target: new targets.Universal({
    service: 'sqs',
    action: 'sendMessage',
    policyStatements: [
      new iam.PolicyStatement({
        actions: ['sqs:SendMessage'],
        resources: ['arn:aws:sqs:us-east-1:123456789012:my_queue'],
      }),
      new iam.PolicyStatement({
        actions: ['kms:Decrypt', 'kms:GenerateDataKey*'],
        resources: ['arn:aws:kms:us-east-1:123456789012:key/0987dcba-09fe-87dc-65ba-ab0987654321'],
      }),
    ],
  }),
});

Note: The default policy uses * in the resources field as CDK does not have a straight forward way to auto-discover the resources permission required. It is recommended that you scope the field down to specific resources to have a better security posture.

版本与发布记录

@aws-cdk/aws-scheduler-targets-alpha 共发布 136 个版本,首次发布于 2023/09/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.186.0-alpha.0latest2025/03/271.6 年前
2.185.0-alpha.02025/03/201.6 年前
2.184.1-alpha.02025/03/141.7 年前
2.184.0-alpha.02025/03/141.7 年前
2.183.0-alpha.02025/03/121.7 年前
2.182.0-alpha.02025/03/061.7 年前

关于 @aws-cdk/aws-scheduler-targets-alpha 的常见问题

@aws-cdk/aws-scheduler-targets-alpha 是什么?

This module is deprecated. All constructs are now available under aws-cdk-lib/aws-scheduler-targets.注意:该包已被作者标记为弃用——This package has been stabilized and moved to aws-cdk-lib

如何安装 @aws-cdk/aws-scheduler-targets-alpha?

在项目目录运行 npm install @aws-cdk/aws-scheduler-targets-alpha,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:constructs(^10.0.0)、aws-cdk-lib(^2.186.0)、@aws-cdk/aws-scheduler-alpha(2.186.0-alpha.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws-cdk/aws-scheduler-targets-alpha 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-cdk/aws-scheduler-targets-alpha 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws-cdk/aws-scheduler-targets-alpha 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 14.15.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aws-cdk/aws-scheduler-targets-alpha 可以商用吗?

@aws-cdk/aws-scheduler-targets-alpha 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws-cdk/aws-scheduler-targets-alpha 的下载量有多少?

根据 npm 官方下载统计,@aws-cdk/aws-scheduler-targets-alpha 在 2026/10/08 – 2026/11/06 这 30 天内被下载 35,436 次,日均约 1,181 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-cdk/aws-scheduler-targets-alpha 还在维护吗?

最新版本 2.186.0-alpha.0 发布于 2025/03/27(1.6 年前);至今共发布 136 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/09/02。作者已将其标记为弃用,新项目建议寻找替代方案。