Skip navigation links

Package software.amazon.awscdk.services.events.targets

Event Targets for Amazon EventBridge

See: Description

Package software.amazon.awscdk.services.events.targets Description

Event Targets for Amazon EventBridge

---

cdk-constructs: Stable


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 and LogGroup 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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_lambda;
 
 
 Function fn = new Function(this, "MyFunc", new FunctionProps()
         .runtime(lambda.Runtime.getNODEJS_12_X())
         .handler("index.handler")
         .code(lambda.Code.fromInline("exports.handler = handler.toString()")));
 
 Object rule = Rule.Builder.create(this, "rule")
         .eventPattern(Map.of(
                 "source", asList("aws.ec2")))
         .build();
 
 Queue queue = new Queue(this, "Queue");
 
 rule.addTarget(LambdaFunction.Builder.create(fn)
         .deadLetterQueue(queue)// Optional: add a dead letter queue
         .maxEventAge(cdk.Duration.hours(2))// Otional: set the maxEventAge retry policy
         .retryAttempts(2)
         .build());
 

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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_logs;
 
 
 LogGroup logGroup = new LogGroup(this, "MyLogGroup", new LogGroupProps()
         .logGroupName("MyLogGroup"));
 
 Object rule = Rule.Builder.create(this, "rule")
         .eventPattern(Map.of(
                 "source", asList("aws.ec2")))
         .build();
 
 rule.addTarget(new 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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_codebuild;
 import software.amazon.awscdk.aws_codecommit;
 
 
 Repository repo = new Repository(this, "MyRepo", new RepositoryProps()
         .repositoryName("aws-cdk-codebuild-events"));
 
 Project project = new Project(this, "MyProject", new ProjectProps()
         .source(codebuild.Source.codeCommit(new CodeCommitSourceProps().repository(repo))));
 
 Queue deadLetterQueue = new Queue(this, "DeadLetterQueue");
 
 // trigger a build when a commit is pushed to the repo
 Rule onCommitRule = repo.onCommit("OnCommit", new OnCommitOptions()
         .target(CodeBuildProject.Builder.create(project)
                 .deadLetterQueue(deadLetterQueue)
                 .build())
         .branches(asList("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

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_codepipeline;
 
 
 Pipeline pipeline = new Pipeline(this, "Pipeline");
 
 Object rule = Rule.Builder.create(this, "Rule")
         .schedule(events.Schedule.expression("rate(1 hour)"))
         .build();
 
 rule.addTarget(new 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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_iam;
 import software.amazon.awscdk.aws_stepfunctions;
 
 
 Object rule = Rule.Builder.create(this, "Rule")
         .schedule(events.Schedule.rate(cdk.Duration.minutes(1)))
         .build();
 
 Queue dlq = new Queue(this, "DeadLetterQueue");
 
 Role role = new Role(this, "Role", new RoleProps()
         .assumedBy(new ServicePrincipal("events.amazonaws.com")));
 StateMachine stateMachine = new StateMachine(this, "SM", new StateMachineProps()
         .definition(new Wait(this, "Hello", new WaitProps().time(sfn.WaitTime.duration(cdk.Duration.seconds(10)))))
         .role(role));
 
 rule.addTarget(SfnStateMachine.Builder.create(stateMachine)
         .input(events.RuleTargetInput.fromObject(Map.of("SomeParam", "SomeValue")))
         .deadLetterQueue(dlq)
         .build());
 

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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_batch;
 import aws.cdk.lib.aws.ecs.ContainerImage;
 
 
 Object jobQueue = JobQueue.Builder.create(this, "MyQueue")
         .computeEnvironments(asList(Map.of(
                 "computeEnvironment", ComputeEnvironment.Builder.create(this, "ComputeEnvironment")
                         .managed(false)
                         .build(),
                 "order", 1)))
         .build();
 
 Object jobDefinition = JobDefinition.Builder.create(this, "MyJob")
         .container(Map.of(
                 "image", ContainerImage.fromRegistry("test-repo")))
         .build();
 
 Queue queue = new Queue(this, "Queue");
 
 Object rule = Rule.Builder.create(this, "Rule")
         .schedule(events.Schedule.rate(cdk.Duration.hours(1)))
         .build();
 
 rule.addTarget(BatchJob.Builder.create(jobQueue.getJobQueueArn(), jobQueue, jobDefinition.getJobDefinitionArn(), jobDefinition)
         .deadLetterQueue(queue)
         .event(events.RuleTargetInput.fromObject(Map.of("SomeParam", "SomeValue")))
         .retryAttempts(2)
         .maxEventAge(cdk.Duration.hours(2))
         .build());
 

Invoke a 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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.aws_apigateway;
 import software.amazon.awscdk.aws_lambda;
 
 
 Object rule = Rule.Builder.create(this, "Rule")
         .schedule(events.Schedule.rate(cdk.Duration.minutes(1)))
         .build();
 
 Function fn = new Function(this, "MyFunc", new FunctionProps()
         .handler("index.handler")
         .runtime(lambda.Runtime.getNODEJS_12_X())
         .code(lambda.Code.fromInline("exports.handler = e => {}")));
 
 LambdaRestApi restApi = new LambdaRestApi(this, "MyRestAPI", new LambdaRestApiProps().handler(fn));
 
 Queue dlq = new Queue(this, "DeadLetterQueue");
 
 rule.addTarget(
 ApiGateway.Builder.create(restApi)
         .path("/* /test")
         .method("GET")
         .stage("prod")
         .pathParameterValues(asList("path-value"))
         .headerParameters(Map.of(
                 "Header1", "header1"))
         .queryStringParameters(Map.of(
                 "QueryParam1", "query-param-1"))
         .deadLetterQueue(dlq)
         .build());
 
Skip navigation links

Copyright © 2021. All rights reserved.