Skip to content
v2.273.0

NotificationRule L2 construct ​

A new notification rule.

Example ​

ts
import * as notifications from 'aws-cdk-lib/aws-codestarnotifications';
import * as codebuild from 'aws-cdk-lib/aws-codebuild';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as chatbot from 'aws-cdk-lib/aws-chatbot';

const project = new codebuild.PipelineProject(this, 'MyProject');

const topic = new sns.Topic(this, 'MyTopic1');

const slack = new chatbot.SlackChannelConfiguration(this, 'MySlackChannel', {
  slackChannelConfigurationName: 'YOUR_CHANNEL_NAME',
  slackWorkspaceId: 'YOUR_SLACK_WORKSPACE_ID',
  slackChannelId: 'YOUR_SLACK_CHANNEL_ID',
});

const rule = new notifications.NotificationRule(this, 'NotificationRule', {
  source: project,
  events: [
    'codebuild-project-build-state-succeeded',
    'codebuild-project-build-state-failed',
  ],
  targets: [topic],
  notificationRuleName: 'MyNotificationRuleName',
  enabled: true, // The default is true
  detailType: notifications.DetailType.FULL, // The default is FULL
  createdBy: 'Jone Doe',
});
rule.addTarget(slack);
python
import aws_cdk.aws_codestarnotifications as notifications
import aws_cdk.aws_codebuild as codebuild
import aws_cdk.aws_sns as sns
import aws_cdk.aws_chatbot as chatbot


project = codebuild.PipelineProject(self, "MyProject")

topic = sns.Topic(self, "MyTopic1")

slack = chatbot.SlackChannelConfiguration(self, "MySlackChannel",
    slack_channel_configuration_name="YOUR_CHANNEL_NAME",
    slack_workspace_id="YOUR_SLACK_WORKSPACE_ID",
    slack_channel_id="YOUR_SLACK_CHANNEL_ID"
)

rule = notifications.NotificationRule(self, "NotificationRule",
    source=project,
    events=["codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed"
    ],
    targets=[topic],
    notification_rule_name="MyNotificationRuleName",
    enabled=True,  # The default is true
    detail_type=notifications.DetailType.FULL,  # The default is FULL
    created_by="Jone Doe"
)
rule.add_target(slack)
java
import software.amazon.awscdk.services.codestarnotifications.*;
import software.amazon.awscdk.services.codebuild.*;
import software.amazon.awscdk.services.sns.*;
import software.amazon.awscdk.services.chatbot.*;


PipelineProject project = new PipelineProject(this, "MyProject");

Topic topic = new Topic(this, "MyTopic1");

SlackChannelConfiguration slack = SlackChannelConfiguration.Builder.create(this, "MySlackChannel")
        .slackChannelConfigurationName("YOUR_CHANNEL_NAME")
        .slackWorkspaceId("YOUR_SLACK_WORKSPACE_ID")
        .slackChannelId("YOUR_SLACK_CHANNEL_ID")
        .build();

NotificationRule rule = NotificationRule.Builder.create(this, "NotificationRule")
        .source(project)
        .events(List.of("codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed"))
        .targets(List.of(topic))
        .notificationRuleName("MyNotificationRuleName")
        .enabled(true) // The default is true
        .detailType(DetailType.FULL) // The default is FULL
        .createdBy("Jone Doe")
        .build();
rule.addTarget(slack);
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.CodeStarNotifications;
using Amazon.CDK.AWS.CodeBuild;
using Amazon.CDK.AWS.SNS;
using Amazon.CDK.AWS.Chatbot;


var project = new PipelineProject(this, "MyProject");

var topic = new Topic(this, "MyTopic1");

var slack = new SlackChannelConfiguration(this, "MySlackChannel", new SlackChannelConfigurationProps {
    SlackChannelConfigurationName = "YOUR_CHANNEL_NAME",
    SlackWorkspaceId = "YOUR_SLACK_WORKSPACE_ID",
    SlackChannelId = "YOUR_SLACK_CHANNEL_ID"
});

var rule = new NotificationRule(this, "NotificationRule", new NotificationRuleProps {
    Source = project,
    Events = new [] { "codebuild-project-build-state-succeeded", "codebuild-project-build-state-failed" },
    Targets = new [] { topic },
    NotificationRuleName = "MyNotificationRuleName",
    Enabled = true,  // The default is true
    DetailType = DetailType.FULL,  // The default is FULL
    CreatedBy = "Jone Doe"
});
rule.AddTarget(slack);
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
import codebuild "github.com/aws/aws-cdk-go/awscdk"
import sns "github.com/aws/aws-cdk-go/awscdk"
import chatbot "github.com/aws/aws-cdk-go/awscdk"


project := codebuild.NewPipelineProject(this, jsii.String("MyProject"))

topic := sns.NewTopic(this, jsii.String("MyTopic1"))

slack := chatbot.NewSlackChannelConfiguration(this, jsii.String("MySlackChannel"), &SlackChannelConfigurationProps{
	SlackChannelConfigurationName: jsii.String("YOUR_CHANNEL_NAME"),
	SlackWorkspaceId: jsii.String("YOUR_SLACK_WORKSPACE_ID"),
	SlackChannelId: jsii.String("YOUR_SLACK_CHANNEL_ID"),
})

rule := notifications.NewNotificationRule(this, jsii.String("NotificationRule"), &NotificationRuleProps{
	Source: project,
	Events: []*string{
		jsii.String("codebuild-project-build-state-succeeded"),
		jsii.String("codebuild-project-build-state-failed"),
	},
	Targets: []INotificationRuleTarget{
		topic,
	},
	NotificationRuleName: jsii.String("MyNotificationRuleName"),
	Enabled: jsii.Boolean(true),
	 // The default is true
	DetailType: notifications.DetailType_FULL,
	 // The default is FULL
	CreatedBy: jsii.String("Jone Doe"),
})
rule.AddTarget(slack)

Initializer ​

ts
new NotificationRule(scope: Construct, id: string, props: NotificationRuleProps)
python
NotificationRule(scope: Construct, id: str, props: NotificationRuleProps)
java
new NotificationRule(Construct scope, String id, NotificationRuleProps props)
csharp
new NotificationRule(Construct scope, string id, NotificationRuleProps props)
go
NewNotificationRule(Scope Construct, Id *string, Props NotificationRuleProps)

Construct Props ​

createdBystringcreated_bystrcreatedByStringCreatedBystringCreatedBy*string= No alias provided#
The name or email alias of the person who created the notification rule.

If not specified, it means that the creator's alias is not provided.

detailTypeDetailTypedetail_typeDetailTypedetailTypeDetailTypeDetailTypeDetailTypeDetailTypeDetailType= DetailType.FULL#
The level of detail to include in the notifications for this resource.
notes

BASIC will include only the contents of the event as it would appear in AWS CloudWatch. FULL will include any supplemental information provided by AWS CodeStar Notifications and/or the service for the resource for which the notification is created.

enabledbooleanenabledboolenabledBooleanEnabledboolEnabled*bool= true#
The status of the notification rule.

If the enabled is set to DISABLED, notifications aren't sent for the notification rule.

eventsstring[]eventstyping.List[str]eventsList<String>Eventsstring[]Events*[]*stringrequired#
A list of event types associated with this notification rule.

For a complete list of event types and IDs, see Notification concepts in the Developer Tools Console User Guide.

notificationRuleNamestringnotification_rule_namestrnotificationRuleNameStringNotificationRuleNamestringNotificationRuleName*string= generated from the `id`#
The name for the notification rule.

Notification rule names must be unique in your AWS account.

The Amazon Resource Name (ARN) of the resource to associate with the notification rule.

Currently, Supported sources include pipelines in AWS CodePipeline, build projects in AWS CodeBuild, and repositories in AWS CodeCommit in this L2 constructor.

targetsINotificationRuleTarget[]targetstyping.List[INotificationRuleTarget]targetsList<INotificationRuleTarget>TargetsINotificationRuleTarget[]Targets*[]INotificationRuleTarget= No targets are added to the rule…#
The targets to register for the notification destination.
default

Default: No targets are added to the rule. Use addTarget() to add a target.

Properties ​

The environment this resource belongs to.
notes

For resources that are created and managed in a Stack (those created by creating new class instances like new Role(), new Bucket(), etc.), this is always the same as the environment of the stack they belong to.

For referenced resources (those obtained from referencing methods like Role.fromRoleArn(), Bucket.fromBucketName(), etc.), they might be different than the stack they were imported into.

nodeNodenodeNodenodeNodeNodeNodeNodeNoderequired#
The tree node.
notificationRuleArnstringnotification_rule_arnstrnotificationRuleArnStringNotificationRuleArnstringNotificationRuleArn*stringrequired#
The ARN of the notification rule (i.e. arn:aws:codestar-notifications:::notificationrule/01234abcde).
notificationRuleRefNotificationRuleReferencenotification_rule_refNotificationRuleReferencenotificationRuleRefNotificationRuleReferenceNotificationRuleRefNotificationRuleReferenceNotificationRuleRefNotificationRuleReferencerequired#
A reference to a NotificationRule resource.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringrequiredstatic#
Uniquely identifies this class.
stackStackstackStackstackStackStackStackStackStackrequired#
The stack in which this resource is defined.

Methods ​

addTarget(target: INotificationRuleTarget): booleandef add_target(self, target: INotificationRuleTarget) -> boolpublic Boolean addTarget(INotificationRuleTarget target)public bool AddTarget(INotificationRuleTarget target)func AddTarget(Target INotificationRuleTarget) *bool#
Adds target to notification rule.
1 parameter

Parameters

  • targettargettargettargetTarget — The SNS topic or AWS Chatbot Slack target.
applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#
Override the cross-stack reference strength for this resource.
notes · 1 parameter

When set, any cross-stack reference to this resource will use the specified mechanism instead of the global default determined by the @aws-cdk/core:defaultCrossStackReferences context key. This is useful for selectively weakening specific references to avoid the "deadly embrace" problem without changing the app-wide default.

Parameters

  • strengthstrengthstrengthstrengthStrength — - The reference strength to use for this resource.
applyRemovalPolicy(policy: RemovalPolicy): voiddef apply_removal_policy(self, policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy policy)public void ApplyRemovalPolicy(RemovalPolicy policy)func ApplyRemovalPolicy(Policy RemovalPolicy)#
Apply the given removal policy to this resource.
notes

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

fromNotificationRuleArn(scope: Construct, id: string, notificationRuleArn: string): INotificationRule@staticmethod def from_notification_rule_arn(scope: Construct, id: str, notification_rule_arn: str) -> INotificationRulepublic static INotificationRule fromNotificationRuleArn(Construct scope, String id, String notificationRuleArn)public static INotificationRule FromNotificationRuleArn(Construct scope, string id, string notificationRuleArn)func FromNotificationRuleArn(Scope Construct, Id *string, NotificationRuleArn *string) INotificationRulestatic#
Import an existing notification rule provided an ARN.
3 parameters

Parameters

  • scopescopescopescopeScope — The parent creating construct.
  • ididididId — The construct's name.
  • notificationRuleArnnotification_rule_arnnotificationRuleArnnotificationRuleArnNotificationRuleArn — Notification rule ARN (i.e. arn:aws:codestar-notifications:::notificationrule/01234abcde).
isConstruct(x: any): boolean@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#
Checks if `x` is a construct.
notes · 1 parameter · returns

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

  • xxxxX — Any object.

Returns: true if x is an object created from a class which extends Construct.

isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#
Returns true if the construct was created by CDK, and false otherwise.
isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#
Check whether the given construct is a Resource.
toString(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#
Returns a string representation of this construct.
with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#
Applies one or more mixins to this construct.

Mixins are applied in order. The list of constructs is captured at the start of the call, so constructs added by a mixin will not be visited. Use multiple with() calls if subsequent mixins should apply to added constructs.