AWSCDK::SchedulerTargets

27 types

Amazon EventBridge Scheduler Targets Construct Library

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.

require 'aws-cdk-lib'


fn = AWSCDK::Lambda::Function.new(self, "MyFunc", {
    runtime: AWSCDK::Lambda::Runtime.NODEJS_LATEST,
    handler: "index.handler",
    code: AWSCDK::Lambda::Code.from_inline("exports.handler = handler.toString()"),
})

dlq = AWSCDK::SQS::Queue.new(self, "DLQ", {
    queue_name: "MyDLQ",
})

target = AWSCDK::SchedulerTargets::LambdaInvoke.new(fn, {
    dead_letter_queue: dlq,
    max_event_age: AWSCDK::Duration.minutes(1),
    retry_attempts: 3,
    input: AWSCDK::Scheduler::ScheduleTargetInput.from_object({
        "payload" => "useful",
    }),
})

schedule = AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(1)),
    target: 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.

require 'aws-cdk-lib'


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

put_parameter_step = AWSCDK::StepFunctionsTasks::CallAWSService.new(self, "PutParameter", {
    service: "ssm",
    action: "putParameter",
    iam_resources: ["*"],
    parameters: {
        "Name.$" => "$.Name",
        "Value.$" => "$.Value",
        Type: "String",
        Overwrite: true,
    },
})

state_machine = AWSCDK::StepFunctions::StateMachine.new(self, "StateMachine", {
    definition_body: AWSCDK::StepFunctions::DefinitionBody.from_chainable(put_parameter_step),
})

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(1)),
    target: AWSCDK::SchedulerTargets::StepFunctionsStartExecution.new(state_machine, {
        input: AWSCDK::Scheduler::ScheduleTargetInput.from_object(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.

require 'aws-cdk-lib'

project = nil # AWSCDK::CodeBuild::Project


AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::CodeBuildStartBuild.new(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 message_group_id 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.

payload = "test"
message_group_id = "id"
queue = AWSCDK::SQS::Queue.new(self, "MyQueue", {
    fifo: true,
    content_based_deduplication: true,
})

target = AWSCDK::SchedulerTargets::SQSSendMessage.new(queue, {
    input: AWSCDK::Scheduler::ScheduleTargetInput.from_text(payload),
    message_group_id: message_group_id,
})

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(1)),
    target: 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.

require 'aws-cdk-lib'


topic = AWSCDK::SNS::Topic.new(self, "Topic")

payload = {
    message: "Hello scheduler!",
}

target = AWSCDK::SchedulerTargets::SNSPublish.new(topic, {
    input: AWSCDK::Scheduler::ScheduleTargetInput.from_object(payload),
})

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(1)),
    target: 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.

require 'aws-cdk-lib'


event_bus = AWSCDK::Events::EventBus.new(self, "EventBus", {
    event_bus_name: "DomainEvents",
})

event_entry = {
    event_bus: event_bus,
    source: "PetService",
    detail: AWSCDK::Scheduler::ScheduleTargetInput.from_object({Name: "Fluffy"}),
    detail_type: "🐶",
}

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.hours(1)),
    target: AWSCDK::SchedulerTargets::EventBridgePutEvents.new(event_entry),
})

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.

require 'aws-cdk-lib'

cfn_assessment_template = nil # AWSCDK::Inspector::CfnAssessmentTemplate


assessment_template = AWSCDK::Inspector::AssessmentTemplate.from_cfn_assessment_template(self, "MyAssessmentTemplate", cfn_assessment_template)

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::InspectorStartAssessmentRun.new(assessment_template),
})

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.

require 'aws-cdk-lib'


stream = AWSCDK::Kinesis::Stream.new(self, "MyStream")

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::KinesisStreamPutRecord.new(stream, {
        partition_key: "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.

require 'aws-cdk-lib'
delivery_stream = nil # AWSCDK::KinesisFirehose::IDeliveryStream


payload = {
    Data: "record",
}

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::FirehosePutRecord.new(delivery_stream, {
        input: AWSCDK::Scheduler::ScheduleTargetInput.from_object(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.

require 'aws-cdk-lib'

pipeline = nil # AWSCDK::Codepipeline::Pipeline


AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::CodePipelineStartPipelineExecution.new(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.

require 'aws-cdk-lib'

pipeline = nil # AWSCDK::Sagemaker::IPipeline


AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::SageMakerStartPipelineExecution.new(pipeline, {
        pipeline_parameter_list: [
            {
                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.

require 'aws-cdk-lib'

cluster = nil # AWSCDK::ECS::ICluster
task_definition = nil # AWSCDK::ECS::FargateTaskDefinition


AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::ECSRunFargateTask.new(cluster, {
        task_definition: task_definition,
    }),
})

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.

require 'aws-cdk-lib'

cluster = nil # AWSCDK::ECS::ICluster
task_definition = nil # AWSCDK::ECS::EC2TaskDefinition


AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::ECSRunEC2Task.new(cluster, {
        task_definition: task_definition,
    }),
})

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.

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.cron({
        minute: "0",
        hour: "0",
    }),
    target: AWSCDK::SchedulerTargets::Universal.new({
        service: "rds",
        action: "stopDBCluster",
        input: AWSCDK::Scheduler::ScheduleTargetInput.from_object({
            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 policy_statements prop to override the policy:

AWSCDK::Scheduler::Schedule.new(self, "Schedule", {
    schedule: AWSCDK::Scheduler::ScheduleExpression.rate(AWSCDK::Duration.minutes(60)),
    target: AWSCDK::SchedulerTargets::Universal.new({
        service: "sqs",
        action: "sendMessage",
        policy_statements: [
            AWSCDK::IAM::PolicyStatement.new({
                actions: ["sqs:SendMessage"],
                resources: ["arn:aws:sqs:us-east-1:123456789012:my_queue"],
            }),
            AWSCDK::IAM::PolicyStatement.new({
                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.

API Reference

Classes 16

CodeBuildStartBuildUse an AWS CodeBuild as a target for AWS EventBridge Scheduler. CodePipelineStartPipelineExecutionUse an AWS CodePipeline pipeline as a target for AWS EventBridge Scheduler. ECSRunEC2TaskSchedule an ECS Task on EC2 using AWS EventBridge Scheduler. ECSRunFargateTaskSchedule an ECS Task on Fargate using AWS EventBridge Scheduler. ECSRunTaskSchedule an ECS Task using AWS EventBridge Scheduler. EventBridgePutEventsSend an event to an AWS EventBridge by AWS EventBridge Scheduler. FirehosePutRecordUse an Amazon Data Firehose as a target for AWS EventBridge Scheduler. InspectorStartAssessmentRunUse an Amazon Inspector as a target for AWS EventBridge Scheduler. KinesisStreamPutRecordUse an Amazon Kinesis Data Streams as a target for AWS EventBridge Scheduler. LambdaInvokeUse an AWS Lambda function as a target for AWS EventBridge Scheduler. SageMakerStartPipelineExecutionUse a SageMaker pipeline as a target for AWS EventBridge Scheduler. ScheduleTargetBaseBase class for Schedule Targets. SNSPublishUse an Amazon SNS topic as a target for AWS EventBridge Scheduler. SQSSendMessageUse an Amazon SQS Queue as a target for AWS EventBridge Scheduler. StepFunctionsStartExecutionUse an AWS Step function as a target for AWS EventBridge Scheduler. UniversalUse a wider set of AWS API as a target for AWS EventBridge Scheduler.

Interfaces 11

EC2TaskPropsProperties for scheduling an ECS Task on EC2. ECSRunTaskBasePropsParameters for scheduling ECS Run Task (common to EC2 and Fargate launch types). EventBridgePutEventsEntryAn entry to be sent to EventBridge. FargateTaskPropsProperties for scheduling an ECS Fargate Task. KinesisStreamPutRecordPropsProperties for a Kinesis Data Streams Target. SageMakerPipelineParameterProperties for a pipeline parameter. SageMakerStartPipelineExecutionPropsProperties for a SageMaker Target. ScheduleTargetBasePropsBase properties for a Schedule Target. SQSSendMessagePropsProperties for a SQS Queue Target. TagMetadata that you apply to a resource to help categorize and organize the resource. UniversalTargetPropsProperties for a Universal Target.