AWS CDK Events (v1)
The `aws-cdk-aws-events` library provides constructs for defining Amazon EventBridge resources in AWS CDK v1. It allows developers to programmatically create event buses, rules, targets, and schema registry resources using Python. This library is part of the AWS CDK v1 ecosystem, with the current version being 1.204.0, and receives updates aligned with AWS service releases and CDK v1 maintenance.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk_aws_events'
cause Incorrect import path. CDK v1 constructs are submodules of `aws_cdk`.fixChange `import aws_cdk_aws_events` to `from aws_cdk import aws_events`. -
jsii.errors.JavaScriptError: The CDK app must be synthesized before it can be deployed.
cause The `cdk.App` object's `synth()` method was not called, or the `cdk.out` directory is missing/stale.fixEnsure `app.synth()` is called at the end of your CDK application entry point before attempting `cdk deploy`. Run `rm -rf cdk.out` if you suspect a stale output directory. -
You must use the same minor version of @aws-cdk/core and the construct libraries (e.g. aws-cdk-lib) - found @aws-cdk/core@1.100.0 and @aws-cdk/aws-events@1.204.0
cause Mismatched versions of core CDK library (`aws-cdk`) and construct libraries (e.g., `aws-cdk-aws-events`). All CDK v1 packages must be at the same major.minor version.fixUpgrade all `aws-cdk-*` packages to the same major and minor version. Use `pip install -U aws-cdk aws-cdk-aws-events aws-cdk-aws-lambda` (or other relevant packages) to ensure they are consistent.
Warnings
- breaking AWS CDK v1 is in maintenance mode; new projects should use AWS CDK v2. Migrating from v1 to v2 involves significant breaking changes to package imports and construct APIs.
- gotcha Forgetting to call `app.synth()` or run `cdk deploy` will result in no CloudFormation template generation or AWS resource deployment.
- gotcha When defining EventBridge rules for cross-account or cross-region events, ensure proper permissions are granted and event bus policies are correctly configured. A common mistake is restricting the `Detail` field too broadly, preventing events from matching.
Install
-
pip install aws-cdk-aws-events aws-cdk
Imports
- aws_events
import aws_cdk_aws_events
from aws_cdk import aws_events
Quickstart
import aws_cdk as cdk
from aws_cdk import aws_events
from aws_cdk import aws_events_targets as targets
from aws_cdk import aws_lambda as lambda_
class MyEventStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Define a simple Lambda function to be a target
my_lambda = lambda_.Function(
self, "MyEventHandler",
runtime=lambda_.Runtime.PYTHON_3_9,
handler="index.handler",
code=lambda_.Code.from_inline("import json; def handler(event, context): print(json.dumps(event)); return 'OK'")
)
# Create an EventBridge rule that triggers every 5 minutes
rule = aws_events.Rule(
self, "MyScheduleRule",
schedule=aws_events.Schedule.rate(cdk.Duration.minutes(5)),
description="Rule that triggers every 5 minutes"
)
# Add the Lambda function as a target for the rule
rule.add_target(targets.LambdaFunction(my_lambda))
app = cdk.App()
MyEventStack(app, "MyEventBridgeStack")
app.synth()