This package has been deprecated

Author message:

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2. For more information on how to migrate, see https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html

@aws-cdk/aws-events-targets
TypeScript icon, indicating that this package has built-in type declarations

1.204.0 • Public • Published

Event Targets for Amazon EventBridge


End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


This library contains integration classes to send Amazon EventBridge to any number of supported AWS Services. Instances of these classes should be passed to the rule.addTarget() method.

Currently supported are:

See the README of the @aws-cdk/aws-events library for more information on EventBridge.

Event retry policy and using dead-letter queues

The Codebuild, CodePipeline, Lambda, StepFunctions, LogGroup and SQSQueue targets support attaching a dead letter queue and setting retry policies. See the lambda example. Use escape hatches for the other target types.

Invoke a Lambda function

Use the LambdaFunction target to invoke a lambda function.

The code snippet below creates an event rule with a Lambda function as a target triggered for every events from aws.ec2 source. You can optionally attach a dead letter queue.

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

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

const rule = new events.Rule(this, 'rule', {
  eventPattern: {
    source: ["aws.ec2"],
  },
});

const queue = new sqs.Queue(this, 'Queue');

rule.addTarget(new targets.LambdaFunction(fn, {
  deadLetterQueue: queue, // Optional: add a dead letter queue
  maxEventAge: cdk.Duration.hours(2), // Optional: set the maxEventAge retry policy
  retryAttempts: 2, // Optional: set the max number of retry attempts
}));

Log an event into a LogGroup

Use the LogGroup target to log your events in a CloudWatch LogGroup.

For example, the following code snippet creates an event rule with a CloudWatch LogGroup as a target. Every events sent from the aws.ec2 source will be sent to the CloudWatch LogGroup.

import * as logs from '@aws-cdk/aws-logs';

const logGroup = new logs.LogGroup(this, 'MyLogGroup', {
  logGroupName: 'MyLogGroup',
});

const rule = new events.Rule(this, 'rule', {
  eventPattern: {
    source: ["aws.ec2"],
  },
});

rule.addTarget(new targets.CloudWatchLogGroup(logGroup));

Start a CodeBuild build

Use the CodeBuildProject target to trigger a CodeBuild project.

The code snippet below creates a CodeCommit repository that triggers a CodeBuild project on commit to the master branch. You can optionally attach a dead letter queue.

import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codecommit from '@aws-cdk/aws-codecommit';

const repo = new codecommit.Repository(this, 'MyRepo', {
  repositoryName: 'aws-cdk-codebuild-events',
});

const project = new codebuild.Project(this, 'MyProject', {
  source: codebuild.Source.codeCommit({ repository: repo }),
});

const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue');

// trigger a build when a commit is pushed to the repo
const onCommitRule = repo.onCommit('OnCommit', {
  target: new targets.CodeBuildProject(project, {
    deadLetterQueue: deadLetterQueue,
  }),
  branches: ['master'],
});

Start a CodePipeline pipeline

Use the CodePipeline target to trigger a CodePipeline pipeline.

The code snippet below creates a CodePipeline pipeline that is triggered every hour

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

const pipeline = new codepipeline.Pipeline(this, 'Pipeline');

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.expression('rate(1 hour)'),
});

rule.addTarget(new targets.CodePipeline(pipeline));

Start a StepFunctions state machine

Use the SfnStateMachine target to trigger a State Machine.

The code snippet below creates a Simple StateMachine that is triggered every minute with a dummy object as input. You can optionally attach a dead letter queue to the target.

import * as iam from '@aws-cdk/aws-iam';
import * as sfn from '@aws-cdk/aws-stepfunctions';

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

const dlq = new sqs.Queue(this, 'DeadLetterQueue');

const role = new iam.Role(this, 'Role', {
  assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
});
const stateMachine = new sfn.StateMachine(this, 'SM', {
  definition: new sfn.Wait(this, 'Hello', { time: sfn.WaitTime.duration(cdk.Duration.seconds(10)) })
});

rule.addTarget(new targets.SfnStateMachine(stateMachine, {
  input: events.RuleTargetInput.fromObject({ SomeParam: 'SomeValue' }),
  deadLetterQueue: dlq,
  role: role
}));

Queue a Batch job

Use the BatchJob target to queue a Batch job.

The code snippet below creates a Simple JobQueue that is triggered every hour with a dummy object as input. You can optionally attach a dead letter queue to the target.

import * as batch from '@aws-cdk/aws-batch';
import { ContainerImage } from '@aws-cdk/aws-ecs';

const jobQueue = new batch.JobQueue(this, 'MyQueue', {
  computeEnvironments: [
    {
      computeEnvironment: new batch.ComputeEnvironment(this, 'ComputeEnvironment', {
        managed: false,
      }),
      order: 1,
    },
  ],
});

const jobDefinition = new batch.JobDefinition(this, 'MyJob', {
  container: {
    image: ContainerImage.fromRegistry('test-repo'),
  },
});

const queue = new sqs.Queue(this, 'Queue');

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

rule.addTarget(new targets.BatchJob(
  jobQueue.jobQueueArn,
  jobQueue,
  jobDefinition.jobDefinitionArn,
  jobDefinition, {
    deadLetterQueue: queue,
    event: events.RuleTargetInput.fromObject({ SomeParam: 'SomeValue' }),
    retryAttempts: 2,
    maxEventAge: cdk.Duration.hours(2),
  },
));

Invoke an API Gateway REST API

Use the ApiGateway target to trigger a REST API.

The code snippet below creates a Api Gateway REST API that is invoked every hour.

import * as api from '@aws-cdk/aws-apigateway';
import * as lambda from '@aws-cdk/aws-lambda';

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
});

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

const restApi = new api.LambdaRestApi( this, 'MyRestAPI', { handler: fn } );

const dlq = new sqs.Queue(this, 'DeadLetterQueue');

rule.addTarget(
  new targets.ApiGateway( restApi, {
    path: '/*/test',
    method: 'GET',
    stage:  'prod',
    pathParameterValues: ['path-value'],
    headerParameters: {
      Header1: 'header1',
    },
    queryStringParameters: {
      QueryParam1: 'query-param-1',
    },
    deadLetterQueue: dlq
  } ),
)

Invoke an API Destination

Use the targets.ApiDestination target to trigger an external API. You need to create an events.Connection and events.ApiDestination as well.

The code snippet below creates an external destination that is invoked every hour.

const connection = new events.Connection(this, 'Connection', {
  authorization: events.Authorization.apiKey('x-api-key', SecretValue.secretsManager('ApiSecretName')),
  description: 'Connection with API Key x-api-key',
});

const destination = new events.ApiDestination(this, 'Destination', {
  connection,
  endpoint: 'https://example.com',
  description: 'Calling example.com with API key x-api-key',
});

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.minutes(1)),
  targets: [new targets.ApiDestination(destination)],
});

Put an event on an EventBridge bus

Use the EventBus target to route event to a different EventBus.

The code snippet below creates the scheduled event rule that route events to an imported event bus.

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.expression('rate(1 minute)'),
});

rule.addTarget(new targets.EventBus(
  events.EventBus.fromEventBusArn(
    this,
    'External',
    `arn:aws:events:eu-west-1:999999999999:event-bus/test-bus`,
  ),
));

Versions

Current Tags

VersionDownloads (Last 7 Days)Published

Version History

VersionDownloads (Last 7 Days)Published
1.203.03,818
1.202.038
1.201.0488
1.200.040
1.199.04
1.198.128
1.198.05
1.197.043
1.196.012
1.195.057
1.194.08
1.193.036
1.192.010
1.191.014
1.190.016
1.189.02
1.188.015
1.187.00
1.186.14
1.186.02
1.185.00
1.184.13
1.184.02
1.183.032
1.182.010
1.181.117
1.181.0170
1.180.032
1.179.01
1.178.010
1.177.03
1.176.065
1.175.09
1.174.032
1.173.02
1.172.046
1.171.04
1.170.10
1.170.09
1.169.069
1.168.059
1.167.068
1.166.16
1.165.020
1.164.014
1.163.233
1.163.166
1.163.01
1.162.05
1.161.099
1.160.074
1.159.011
1.158.03
1.157.04
1.156.173
1.156.02
1.155.04
1.154.053
1.153.132
1.153.00
1.152.030
1.151.05
1.150.05
1.149.014
1.148.04
1.147.021
1.146.057
1.145.023
1.144.0195
1.143.027
1.142.02
1.141.01
1.140.06
1.139.020
1.138.22
1.138.10
1.138.05
1.137.09
1.136.07
1.135.05
1.134.013
1.133.013
1.132.012
1.131.010
1.130.09
1.129.018
1.128.018
1.127.02
1.126.052
1.125.0545
1.124.014,456
1.123.015
1.122.019
1.121.0150
1.120.03
1.119.076
1.118.042
1.117.010
1.116.09
1.115.02
1.114.08
1.113.03
1.112.00
1.111.087
1.110.112
1.110.01,651
1.109.09
1.108.12
1.108.03
1.107.02
1.106.154
1.106.08
1.105.079
1.104.029
1.103.096
1.102.017
1.101.091
1.100.049
1.99.06
1.98.04
1.97.02
1.96.0160
1.95.23
1.95.188
1.95.01
1.94.139
1.94.02
1.93.022
1.92.02
1.91.04,865
1.90.123
1.90.03
1.89.018
1.88.010
1.87.112
1.87.0182
1.86.033
1.85.039
1.84.02
1.83.04
1.82.01
1.81.02
1.80.010
1.79.02
1.78.055
1.77.04
1.76.024
1.75.03
1.74.02
1.73.03
1.72.0547
1.71.036
1.70.03
1.69.013
1.68.02
1.67.0107
1.66.00
1.65.02
1.64.12
1.64.04
1.63.045
1.62.06
1.61.12
1.61.00
1.60.02
1.59.02
1.58.02
1.57.03
1.56.025
1.55.02
1.54.015
1.53.02
1.52.00
1.51.02
1.50.02
1.49.12
1.49.00
1.48.03
1.47.12
1.47.02
1.46.09
1.45.05
1.44.02
1.43.00
1.42.12
1.42.016
1.41.02
1.40.00
1.39.07
1.38.020
1.37.06
1.36.12
1.36.02
1.35.00
1.34.15
1.34.00
1.33.10
1.33.02
1.32.24
1.32.14
1.32.02
1.31.083
1.30.02
1.29.00
1.28.00
1.27.03
1.26.02
1.25.02
1.24.00
1.23.02
1.22.03
1.21.122
1.21.00
1.20.02
1.19.04
1.18.02
1.17.19
1.17.00
1.16.39
1.16.23
1.16.10
1.16.00
1.15.02
1.14.02
1.13.12
1.13.00
1.12.00
1.11.02
1.10.10
1.10.00
1.9.00
1.8.00
1.7.00
1.6.12
1.6.00
1.5.00
1.4.02
1.3.00
1.2.00
1.1.00
1.0.00
0.39.00
0.38.00
0.37.00
0.36.20
0.36.10
0.36.00
0.35.00
0.34.02
0.33.00
0.32.00
0.31.00
0.30.00
0.29.00
0.28.00

Package Sidebar

Install

npm i @aws-cdk/aws-events-targets

Weekly Downloads

33,917

Version

1.204.0

License

Apache-2.0

Unpacked Size

687 kB

Total Files

56

Last publish

Collaborators

  • amzn-oss
  • aws-cdk-team