Event targets for Amazon EventBridge that enable routing events to various AWS services
Targets for continuous integration and deployment services that can be triggered by EventBridge events.
Trigger AWS CodeBuild projects in response to EventBridge events.
/**
* Use a CodeBuild project as a target for Amazon EventBridge rules
*/
class CodeBuildProject implements events.IRuleTarget {
constructor(project: codebuild.IProject, props?: CodeBuildProjectProps);
/**
* Returns a RuleTarget that can be used to trigger this CodeBuild project
* as a result from an EventBridge event
*/
bind(rule: events.IRule, id?: string): events.RuleTargetConfig;
}
interface CodeBuildProjectProps extends TargetBaseProps {
/**
* The role to assume before invoking the target
* @default a new role will be created
*/
readonly eventRole?: iam.IRole;
/**
* The event to send to CodeBuild
* This will be available as environment variables in the build
* @default the entire EventBridge event
*/
readonly event?: events.RuleTargetInput;
}Usage Example:
import * as codebuild from "@aws-cdk/aws-codebuild";
import * as codecommit from "@aws-cdk/aws-codecommit";
import * as events from "@aws-cdk/aws-events";
import * as targets from "@aws-cdk/aws-events-targets";
import * as sqs from "@aws-cdk/aws-sqs";
// Create CodeCommit repository
const repo = new codecommit.Repository(this, "MyRepo", {
repositoryName: "my-application",
description: "Application source code repository",
});
// Create CodeBuild project
const project = new codebuild.Project(this, "BuildProject", {
projectName: "my-app-build",
source: codebuild.Source.codeCommit({ repository: repo }),
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
computeType: codebuild.ComputeType.SMALL,
},
buildSpec: codebuild.BuildSpec.fromObject({
version: "0.2",
phases: {
pre_build: {
commands: [
"echo Logging in to Amazon ECR...",
"aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com",
],
},
build: {
commands: [
"echo Build started on `date`",
"echo Building the Docker image...",
"docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .",
"docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG",
],
},
post_build: {
commands: [
"echo Build completed on `date`",
"echo Pushing the Docker image...",
"docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG",
],
},
},
}),
});
// Create dead letter queue for failed builds
const buildDlq = new sqs.Queue(this, "BuildDeadLetterQueue");
// Trigger build on repository commits to master branch
const commitRule = repo.onCommit("OnCommitToMaster", {
target: new targets.CodeBuildProject(project, {
deadLetterQueue: buildDlq,
retryAttempts: 2,
maxEventAge: Duration.hours(1),
event: events.RuleTargetInput.fromObject({
commitId: events.EventField.fromPath("$.detail.commitId"),
repositoryName: events.EventField.fromPath("$.detail.repositoryName"),
referenceName: events.EventField.fromPath("$.detail.referenceName"),
author: events.EventField.fromPath("$.detail.author"),
}),
}),
branches: ["master", "main"],
});
// Trigger build on S3 artifact updates
const s3Rule = new events.Rule(this, "S3ArtifactRule", {
eventPattern: {
source: ["aws.s3"],
detailType: ["Object Created"],
detail: {
bucket: { name: ["my-artifacts-bucket"] },
object: { key: [{ prefix: "source-code/" }] },
},
},
});
s3Rule.addTarget(new targets.CodeBuildProject(project, {
event: events.RuleTargetInput.fromObject({
buildReason: "S3 artifact updated",
s3Bucket: events.EventField.fromPath("$.detail.bucket.name"),
s3Key: events.EventField.fromPath("$.detail.object.key"),
eventTime: events.EventField.fromPath("$.time"),
}),
}));Start AWS CodePipeline pipelines in response to EventBridge events.
/**
* Use a CodePipeline pipeline as a target for Amazon EventBridge rules
*/
class CodePipeline implements events.IRuleTarget {
constructor(pipeline: codepipeline.IPipeline, options?: CodePipelineTargetOptions);
/**
* Returns a RuleTarget that can be used to trigger this CodePipeline
* as a result from an EventBridge event
*/
bind(rule: events.IRule, id?: string): events.RuleTargetConfig;
}
interface CodePipelineTargetOptions extends TargetBaseProps {
/**
* The role to assume before invoking the target
* @default a new role will be created
*/
readonly eventRole?: iam.IRole;
}Usage Example:
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipelineActions from "@aws-cdk/aws-codepipeline-actions";
import * as codecommit from "@aws-cdk/aws-codecommit";
import * as codebuild from "@aws-cdk/aws-codebuild";
import * as s3 from "@aws-cdk/aws-s3";
import * as events from "@aws-cdk/aws-events";
import * as targets from "@aws-cdk/aws-events-targets";
// Create source repository and artifacts bucket
const sourceRepo = new codecommit.Repository(this, "SourceRepo", {
repositoryName: "my-app-source",
});
const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket");
// Create CodeBuild project for the pipeline
const buildProject = new codebuild.Project(this, "PipelineBuild", {
source: codebuild.Source.codeCommit({ repository: sourceRepo }),
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
},
});
// Create CodePipeline
const pipeline = new codepipeline.Pipeline(this, "DeploymentPipeline", {
pipelineName: "my-app-pipeline",
artifactBucket: artifactsBucket,
stages: [
{
stageName: "Source",
actions: [
new codepipelineActions.CodeCommitSourceAction({
actionName: "Source",
repository: sourceRepo,
output: new codepipeline.Artifact("SourceOutput"),
}),
],
},
{
stageName: "Build",
actions: [
new codepipelineActions.CodeBuildAction({
actionName: "Build",
project: buildProject,
input: new codepipeline.Artifact("SourceOutput"),
outputs: [new codepipeline.Artifact("BuildOutput")],
}),
],
},
{
stageName: "Deploy",
actions: [
new codepipelineActions.S3DeployAction({
actionName: "Deploy",
bucket: artifactsBucket,
input: new codepipeline.Artifact("BuildOutput"),
}),
],
},
],
});
// Schedule pipeline execution
const scheduleRule = new events.Rule(this, "ScheduledDeployment", {
description: "Trigger deployment pipeline daily at 2 AM",
schedule: events.Schedule.cron({
hour: "2",
minute: "0",
}),
});
scheduleRule.addTarget(new targets.CodePipeline(pipeline, {
retryAttempts: 1,
maxEventAge: Duration.hours(2),
}));
// Trigger pipeline on external events
const releaseRule = new events.Rule(this, "ReleaseRule", {
eventPattern: {
source: ["myapp.releases"],
detailType: ["Release Approved"],
detail: {
environment: ["production"],
status: ["approved"],
},
},
});
releaseRule.addTarget(new targets.CodePipeline(pipeline));
// Cross-region pipeline trigger
const crossRegionRule = new events.Rule(this, "CrossRegionRule", {
eventPattern: {
source: ["aws.codepipeline"],
detailType: ["CodePipeline Pipeline Execution State Change"],
detail: {
state: ["SUCCEEDED"],
pipeline: ["upstream-pipeline"],
},
},
});
crossRegionRule.addTarget(new targets.CodePipeline(pipeline));// Multi-branch build triggers
const multiBranchRule = new events.Rule(this, "MultiBranchRule", {
eventPattern: {
source: ["aws.codecommit"],
detailType: ["CodeCommit Repository State Change"],
detail: {
event: ["referenceCreated", "referenceUpdated"],
referenceName: [
{ prefix: "refs/heads/feature/" },
{ prefix: "refs/heads/release/" },
"refs/heads/develop",
],
},
},
});
multiBranchRule.addTarget(new targets.CodeBuildProject(buildProject, {
event: events.RuleTargetInput.fromObject({
branchName: events.EventField.fromPath("$.detail.referenceName"),
commitId: events.EventField.fromPath("$.detail.commitId"),
buildType: "feature-build",
}),
}));// Trigger builds from external webhook events via custom EventBridge events
const webhookRule = new events.Rule(this, "WebhookRule", {
eventPattern: {
source: ["myapp.webhooks"],
detailType: ["GitHub Push", "GitLab Push"],
detail: {
repository: ["my/repository"],
ref: [{ prefix: "refs/heads/" }],
},
},
});
webhookRule.addTarget(new targets.CodeBuildProject(project, {
event: events.RuleTargetInput.fromObject({
repository: events.EventField.fromPath("$.detail.repository"),
branch: events.EventField.fromPath("$.detail.ref"),
commitSha: events.EventField.fromPath("$.detail.after"),
pusher: events.EventField.fromPath("$.detail.pusher.name"),
buildReason: "External webhook trigger",
}),
}));// React to build completion events
const buildCompleteRule = new events.Rule(this, "BuildCompleteRule", {
eventPattern: {
source: ["aws.codebuild"],
detailType: ["CodeBuild Build State Change"],
detail: {
"build-status": ["SUCCEEDED", "FAILED"],
"project-name": [project.projectName],
},
},
});
// Trigger downstream pipeline on successful build
buildCompleteRule.addTarget(new targets.CodePipeline(pipeline));Install with Tessl CLI
npx tessl i tessl/npm-aws-cdk--aws-events-targets