AWS CDK CodeStarNotifications (v1)
The `aws-cdk-aws-codestarnotifications` package is part of the AWS Cloud Development Kit (CDK) v1, providing constructs for managing AWS CodeStar Notifications. These constructs allow you to create rules to send notifications about events from various AWS services (like CodeCommit, CodeBuild, CodePipeline) to targets such as Amazon SNS topics or AWS Chatbot. As of version `1.204.0`, AWS CDK v1 is in maintenance mode, with active development focused on CDK v2. New projects are encouraged to use CDK v2.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_codestarnotifications'
cause The package `aws-cdk-aws-codestarnotifications` is not installed, or `aws-cdk.core` is not installed, or Python environment path issues.fixEnsure the package is installed: `pip install aws-cdk-aws-codestarnotifications==1.204.0`. Also, ensure `aws-cdk.core` of the matching version is installed: `pip install aws-cdk.core==1.204.0`. -
TypeError: Argument 'source' must be an instance of <class 'aws_cdk.aws_codecommit.Repository'>
cause This usually happens when `source` is an object from a different CDK version (e.g., v2 `aws_cdk_lib.aws_codecommit.Repository`) or a different type entirely, incompatible with the v1 `NotificationRule` expecting a v1 `Repository`.fixVerify that all related CDK constructs (source, target, and the notification rule itself) are from the same CDK major version (v1 in this case). Ensure all `aws-cdk.aws-*` packages are consistently installed at the same v1.x.x version. -
AttributeError: module 'aws_cdk.aws_codestarnotifications' has no attribute 'NotificationRule'
cause This error often indicates an attempt to use a v2 import pattern or construct name with a v1 installation, or a typo in the construct name.fixDouble-check your import statement and construct name. For CDK v1, the correct import for this module is `from aws_cdk import aws_codestarnotifications as notifications` and the class is `notifications.NotificationRule`. Ensure `aws-cdk-aws-codestarnotifications` is installed and matches your `aws-cdk.core` version.
Warnings
- breaking AWS CDK v1 (`aws-cdk.aws-*`) is fundamentally different from AWS CDK v2 (`aws-cdk-lib`). Imports, module structure, and even some construct names have changed. This entry focuses on v1.204.0.
- deprecated AWS CDK v1 is in maintenance mode. Active development, new features, and most bug fixes are focused on AWS CDK v2. Users are strongly encouraged to migrate to v2.
- gotcha Mixing different minor versions of `aws-cdk.core` and `aws-cdk.aws-codestarnotifications` (or other `aws-cdk.aws-*` packages) can lead to runtime errors or unexpected behavior. All CDK v1 packages used in a project should typically be at the same version.
Install
-
pip install aws-cdk-aws-codestarnotifications==1.204.0 -
pip install aws-cdk.core==1.204.0 aws-cdk.aws-codecommit==1.204.0 aws-cdk.aws-sns==1.204.0 aws-cdk-aws-codestarnotifications==1.204.0
Imports
- NotificationRule
from aws_cdk import aws_codestarnotifications as notifications
- DetailType
from aws_cdk import aws_codestarnotifications as notifications
Quickstart
import os
from aws_cdk import (
aws_codecommit as codecommit,
aws_sns as sns,
aws_codestarnotifications as notifications,
core as cdk
)
class CodeStarNotificationsExampleStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create a CodeCommit repository as the source for notifications
repo = codecommit.Repository(self, "MyCodeCommitRepo",
repository_name="my-app-repository-for-notifications"
)
# Create an SNS topic as the target for notifications
# In a real scenario, you'd likely subscribe an email or Lambda to this topic.
notification_topic = sns.Topic(self, "MyNotificationTopic")
# Create a Notification Rule
# This rule will send notifications to the SNS topic for specific CodeCommit events.
notifications.NotificationRule(self, "RepoNotificationRule",
source=repo,
events=[
"codecommit-repository-comments-on-commits",
"codecommit-repository-pull-request-created",
"codecommit-repository-pull-request-merged"
],
targets=[notification_topic],
detail_type=notifications.DetailType.FULL # Or DetailType.BASIC
)
# Example of how to synthesize this stack (usually in app.py)
# app = cdk.App()
# CodeStarNotificationsExampleStack(app, "CodeStarNotificationsExampleStack")
# app.synth()