What is @aws-cdk/aws-events?
@aws-cdk/aws-events is an AWS Cloud Development Kit (CDK) library that allows you to define and manage Amazon EventBridge resources using code. EventBridge is a serverless event bus that makes it easier to build event-driven applications by connecting application data from your own applications, integrated Software-as-a-Service (SaaS) applications, and AWS services.
What are @aws-cdk/aws-events's main functionalities?
Creating an Event Bus
This code sample demonstrates how to create a new EventBridge event bus using the AWS CDK. The event bus can be used to receive and route events.
const events = require('@aws-cdk/aws-events');
const cdk = require('@aws-cdk/core');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'EventBusStack');
const eventBus = new events.EventBus(stack, 'MyEventBus', {
eventBusName: 'my-event-bus'
});
app.synth();
Creating a Rule
This code sample demonstrates how to create an EventBridge rule that triggers when an EC2 instance changes state to 'running'. The rule targets a Lambda function.
const events = require('@aws-cdk/aws-events');
const targets = require('@aws-cdk/aws-events-targets');
const cdk = require('@aws-cdk/core');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'EventRuleStack');
const rule = new events.Rule(stack, 'MyRule', {
eventPattern: {
source: ['aws.ec2'],
detailType: ['EC2 Instance State-change Notification'],
detail: {
state: ['running']
}
}
});
rule.addTarget(new targets.LambdaFunction(myLambdaFunction));
app.synth();
Scheduling Events
This code sample demonstrates how to create a scheduled EventBridge rule that triggers every 5 minutes. The rule targets a Lambda function.
const events = require('@aws-cdk/aws-events');
const targets = require('@aws-cdk/aws-events-targets');
const cdk = require('@aws-cdk/core');
const app = new cdk.App();
const stack = new cdk.Stack(app, 'ScheduledEventStack');
const rule = new events.Rule(stack, 'MyScheduledRule', {
schedule: events.Schedule.rate(cdk.Duration.minutes(5))
});
rule.addTarget(new targets.LambdaFunction(myLambdaFunction));
app.synth();
Other packages similar to @aws-cdk/aws-events
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, which provides a comprehensive set of tools for interacting with AWS services, including EventBridge. Unlike @aws-cdk/aws-events, which is used for defining infrastructure as code, aws-sdk is used for making API calls to AWS services.
serverless
The serverless framework is a popular open-source framework for building and deploying serverless applications. It supports AWS Lambda and EventBridge, among other services. While @aws-cdk/aws-events focuses on infrastructure as code, serverless provides a higher-level abstraction for deploying serverless applications.
pulumi
Pulumi is an infrastructure as code tool that supports multiple cloud providers, including AWS. It allows you to define and manage cloud resources using familiar programming languages. Pulumi can be used to manage EventBridge resources similarly to @aws-cdk/aws-events, but it offers a different approach and supports multiple clouds.
Amazon CloudWatch Events Construct Library
Amazon CloudWatch Events delivers a near real-time stream of system events that
describe changes in AWS resources. For example, an AWS CodePipeline emits the
State
Change
event when the pipeline changes it's state.
- Events: An event indicates a change in your AWS environment. AWS resources
can generate events when their state changes. For example, Amazon EC2
generates an event when the state of an EC2 instance changes from pending to
running, and Amazon EC2 Auto Scaling generates events when it launches or
terminates instances. AWS CloudTrail publishes events when you make API calls.
You can generate custom application-level events and publish them to
CloudWatch Events. You can also set up scheduled events that are generated on
a periodic basis. For a list of services that generate events, and sample
events from each service, see CloudWatch Events Event Examples From Each
Supported
Service.
- Targets: A target processes events. Targets can include Amazon EC2
instances, AWS Lambda functions, Kinesis streams, Amazon ECS tasks, Step
Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in
targets. A target receives events in JSON format.
- Rules: A rule matches incoming events and routes them to targets for
processing. A single rule can route to multiple targets, all of which are
processed in parallel. Rules are not processed in a particular order. This
enables different parts of an organization to look for and process the events
that are of interest to them. A rule can customize the JSON sent to the
target, by passing only certain parts or by overwriting it with a constant.
- EventBuses: An event bus can receive events from your own custom applications
or it can receive events from applications and services created by AWS SaaS partners.
See Creating an Event Bus.
The Rule
construct defines a CloudWatch events rule which monitors an
event based on an event
pattern
and invoke event targets when the pattern is matched against a triggered
event. Event targets are objects that implement the IRuleTarget
interface.
Normally, you will use one of the source.onXxx(name[, target[, options]]) -> Rule
methods on the event source to define an event rule associated with
the specific activity. You can targets either via props, or add targets using
rule.addTarget
.
For example, to define an rule that triggers a CodeBuild project build when a
commit is pushed to the "master" branch of a CodeCommit repository:
const onCommitRule = repo.onCommit('OnCommit', {
target: new targets.CodeBuildProject(project),
branches: ['master']
});
You can add additional targets, with optional input
transformer
using eventRule.addTarget(target[, input])
. For example, we can add a SNS
topic target which formats a human-readable message for the commit.
For example, this adds an SNS topic as a target:
onCommitRule.addTarget(new targets.SnsTopic(topic, {
message: events.RuleTargetInput.fromText(
`A commit was pushed to the repository ${codecommit.ReferenceEvent.repositoryName} on branch ${codecommit.ReferenceEvent.referenceName}`
)
}));
Event Targets
The @aws-cdk/aws-events-targets
module includes classes that implement the IRuleTarget
interface for various AWS services.
The following targets are supported:
targets.CodeBuildProject
: Start an AWS CodeBuild buildtargets.CodePipeline
: Start an AWS CodePipeline pipeline executiontargets.EcsTask
: Start a task on an Amazon ECS clustertargets.LambdaFunction
: Invoke an AWS Lambda functiontargets.SnsTopic
: Publish into an SNS topictargets.SqsQueue
: Send a message to an Amazon SQS Queuetargets.SfnStateMachine
: Trigger an AWS Step Functions state machinetargets.AwsApi
: Make an AWS API call
Cross-account targets
It's possible to have the source of the event and a target in separate AWS accounts:
import { App, Stack } from '@aws-cdk/core';
import codebuild = require('@aws-cdk/aws-codebuild');
import codecommit = require('@aws-cdk/aws-codecommit');
import targets = require('@aws-cdk/aws-events-targets');
const app = new App();
const stack1 = new Stack(app, 'Stack1', { env: { account: account1, region: 'us-east-1' } });
const repo = new codecommit.Repository(stack1, 'Repository', {
});
const stack2 = new Stack(app, 'Stack2', { env: { account: account2, region: 'us-east-1' } });
const project = new codebuild.Project(stack2, 'Project', {
});
repo.onCommit('OnCommit', {
target: new targets.CodeBuildProject(project),
});
In this situation, the CDK will wire the 2 accounts together:
- It will generate a rule in the source stack with the event bus of the target account as the target
- It will generate a rule in the target stack, with the provided target
- It will generate a separate stack that gives the source account permissions to publish events
to the event bus of the target account in the given region,
and make sure its deployed before the source stack
Note: while events can span multiple accounts, they cannot span different regions
(that is a CloudWatch, not CDK, limitation).
For more information, see the
AWS documentation on cross-account events.
1.18.0 (2019-11-25)
General Availability of AWS CDK for .NET and Java!! πππ₯π₯πΎπΎ
We are excited to announce the general availability of support for the .NET family of languages (C#,
F#, ...) as well as Java!
We want to express our gratitude to all of our early customers as well as the amazing contributors
for all the help and support in making this release possible. Thank you for all the feedback
provided during the Developer Preview of .NET and Java support, without which the product would not
be what it is today.
Special thanks go out to a handful of amazing people who have provided instrumental support in
bringing .NET and Java support to this point:
Of course, we continue to be amazed and thrilled by the community contributions we received besides
language support. The passion demonstrated by the CDK community is heartwarming and largely
contributes to making maintaining the CDK an enjoyable, enriching experience!
Features
- lambda: node12.x, python3.8 and java11 runtimes (#5107) (e62f9fb)
- lambda: unlock the lambda environment variables restriction in China regions (#5122) (cc13009)
Bug Fixes
- init/chsarp: correct README for sample-app C# template (#5144) (b2031f6)
- init/sample-app: numerous fixes and additions to the sample-app init templates (#5119) (02c3b05), closes #5130 #5130
- init/java: add -e to mvn command so errors aren't hidden (#5129) (5427106), closes #5128
- init/csharp: .NET semantic fixes for init templates (#5154) (04a1b32)
Known Issues
The following known issues were identified that specifically affect .NET and Java support in the CDK,
and which will be promptly addressed in upcoming CDK releases (in no particular order). See the
GitHub issues for more information and workarounds where applicable.
- .NET and Java: [
aws/jsii#1011
] - abstract members are not marked as such on their .NET and Java representations - .NET: [
aws/jsii#1029
] - user-defined classes implementing CDK interfaces must extend Amazon.Jsii.Runtime.Deputy.DeputyBase
- .NET: [
aws/jsii#1042
] - Parameters typed object accept only primitive types, instances of CDK types, Dictionary<string,?>
- .NET: [
aws/jsii#1044
] - Unable to pass interface instance through in a Dictionary<string,object>
- Java: [
aws/jsii#1034
] - Implementing or overriding overloaded methods in Java does not work consistently - Java: [
aws/jsii#1035
] - Returning Lazy.anyValue
from an method whose return type is java.lang.Object
may result in Resolution Errors - Java: [
aws/jsii#1005
] - property getter implementations (e.g: from an interface) may be ignored