Skip to content
v2.274.0

Rule L2 construct ​

Defines an EventBridge Rule in this stack.

Example ​

ts
import * as redshiftserverless from 'aws-cdk-lib/aws-redshiftserverless'

declare const workgroup: redshiftserverless.CfnWorkgroup;

const rule = new events.Rule(this, 'Rule', {
  schedule: events.Schedule.rate(cdk.Duration.hours(1)),
});

const dlq = new sqs.Queue(this, 'DeadLetterQueue');

rule.addTarget(new targets.RedshiftQuery(workgroup.attrWorkgroupWorkgroupArn, {
  database: 'dev',
  deadLetterQueue: dlq,
  sql: ['SELECT * FROM foo','SELECT * FROM baz'],
}));
python
import aws_cdk.aws_redshiftserverless as redshiftserverless

# workgroup: redshiftserverless.CfnWorkgroup


rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.hours(1))
)

dlq = sqs.Queue(self, "DeadLetterQueue")

rule.add_target(targets.RedshiftQuery(workgroup.attr_workgroup_workgroup_arn,
    database="dev",
    dead_letter_queue=dlq,
    sql=["SELECT * FROM foo", "SELECT * FROM baz"]
))
java
import software.amazon.awscdk.services.redshiftserverless.*;

CfnWorkgroup workgroup;


Rule rule = Rule.Builder.create(this, "Rule")
        .schedule(Schedule.rate(Duration.hours(1)))
        .build();

Queue dlq = new Queue(this, "DeadLetterQueue");

rule.addTarget(RedshiftQuery.Builder.create(workgroup.getAttrWorkgroupWorkgroupArn())
        .database("dev")
        .deadLetterQueue(dlq)
        .sql(List.of("SELECT * FROM foo", "SELECT * FROM baz"))
        .build());
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.RedshiftServerless;

CfnWorkgroup workgroup;


var rule = new Rule(this, "Rule", new RuleProps {
    Schedule = Schedule.Rate(Duration.Hours(1))
});

var dlq = new Queue(this, "DeadLetterQueue");

rule.AddTarget(new RedshiftQuery(workgroup.AttrWorkgroupWorkgroupArn, new RedshiftQueryProps {
    Database = "dev",
    DeadLetterQueue = dlq,
    Sql = new [] { "SELECT * FROM foo", "SELECT * FROM baz" }
}));
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import redshiftserverless "github.com/aws/aws-cdk-go/awscdk"

var workgroup CfnWorkgroup


rule := events.NewRule(this, jsii.String("Rule"), &RuleProps{
	Schedule: events.Schedule_Rate(cdk.Duration_Hours(jsii.Number(1))),
})

dlq := sqs.NewQueue(this, jsii.String("DeadLetterQueue"))

rule.AddTarget(targets.NewRedshiftQuery(workgroup.attrWorkgroupWorkgroupArn, &RedshiftQueryProps{
	Database: jsii.String("dev"),
	DeadLetterQueue: dlq,
	Sql: []*string{
		jsii.String("SELECT * FROM foo"),
		jsii.String("SELECT * FROM baz"),
	},
}))

Initializer ​

ts
new Rule(scope: Construct, id: string, props?: RuleProps)
python
Rule(scope: Construct, id: str, props: typing.Optional[RuleProps] = None)
java
new Rule(Construct scope, String id, RuleProps props)
csharp
new Rule(Construct scope, string id, RuleProps? props = null)
go
NewRule(Scope Construct, Id *string, Props RuleProps)

Construct Props ​

crossStackScopeConstructcross_stack_scopeConstructcrossStackScopeConstructCrossStackScopeConstructCrossStackScopeConstruct= none (the main scope will be use…#
The scope to use if the source of the rule and its target are in different Stacks (but in the same account & region).

This helps dealing with cycles that often arise in these situations.

default

Default: none (the main scope will be used, even for cross-stack Events)

descriptionstringdescriptionstrdescriptionStringDescriptionstringDescription*string= No description#
A description of the rule's purpose.
enabledbooleanenabledboolenabledBooleanEnabledboolEnabled*bool= true#
Indicates whether the rule is enabled.
eventBusIEventBusRefevent_busIEventBusRefeventBusIEventBusRefEventBusIEventBusRefEventBusIEventBusRef= The default event bus.#
The event bus to associate with this rule.
eventPatternEventPatternevent_patternEventPatterneventPatternEventPatternEventPatternEventPatternEventPatternEventPattern= No additional filtering based on…#
Additional restrictions for the event to route to the specified target.

The method that generates the rule probably imposes some type of event filtering. The filtering implied by what you pass here is added on top of that filtering.

default

Default: No additional filtering based on an event pattern.

roleIRoleRefroleIRoleRefroleIRoleRefRoleIRoleRefRoleIRoleRef= No role associated#
The role that is used for target invocation.

Must be assumable by principal events.amazonaws.com.

ruleNamestringrule_namestrruleNameStringRuleNamestringRuleName*string= AWS CloudFormation generates a u…#
A name for the rule.
default

Default: AWS CloudFormation generates a unique physical ID.

scheduleSchedulescheduleSchedulescheduleScheduleScheduleScheduleScheduleSchedule= None.#
The schedule or rate (frequency) that determines when EventBridge runs the rule.

You must specify this property, the eventPattern property, or both.

For more information, see Schedule Expression Syntax for Rules in the Amazon EventBridge User Guide.

targetsIRuleTarget[]targetstyping.List[IRuleTarget]targetsList<IRuleTarget>TargetsIRuleTarget[]Targets*[]IRuleTarget= No targets.#
Targets to invoke when this rule matches an event.

Input will be the full matched event. If you wish to specify custom target input, use addTarget(target[, inputOptions]).

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.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringrequiredstatic#
Uniquely identifies this class.
ruleArnstringrule_arnstrruleArnStringRuleArnstringRuleArn*stringrequired#
The value of the event rule Amazon Resource Name (ARN), such as arn:aws:events:us-east-2:123456789012:rule/example.
ruleNamestringrule_namestrruleNameStringRuleNamestringRuleName*stringrequired#
The name event rule.
ruleRefRuleReferencerule_refRuleReferenceruleRefRuleReferenceRuleRefRuleReferenceRuleRefRuleReferencerequired#
A reference to a Rule resource.
stackStackstackStackstackStackStackStackStackStackrequired#
The stack in which this resource is defined.

Methods ​

addEventPattern(eventPattern?: EventPattern): voiddef add_event_pattern(self, event_pattern: typing.Optional[EventPattern] = None) -> Nonepublic void addEventPattern(EventPattern eventPattern)public void AddEventPattern(EventPattern? eventPattern = null)func AddEventPattern(EventPattern EventPattern)#
Adds an event pattern filter to this rule.
notes

If a pattern was already specified, these values are merged into the existing pattern.

For example, if the rule already contains the pattern:

{ "resources": [ "r1" ], "detail": { "hello": [ 1 ] } }

And addEventPattern is called with the pattern:

{ "resources": [ "r2" ], "detail": { "foo": [ "bar" ] } }

The resulting event pattern will be:

{ "resources": [ "r1", "r2" ], "detail": { "hello": [ 1 ], "foo": [ "bar" ] } }

addTarget(target?: IRuleTarget): voiddef add_target(self, target: typing.Optional[IRuleTarget] = None) -> Nonepublic void addTarget(IRuleTarget target)public void AddTarget(IRuleTarget? target = null)func AddTarget(Target IRuleTarget)#
Adds a target to the rule. The abstract class RuleTarget can be extended to define new targets.

No-op if target is undefined.

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).

fromEventRuleArn(scope: Construct, id: string, eventRuleArn: string): IRule@staticmethod def from_event_rule_arn(scope: Construct, id: str, event_rule_arn: str) -> IRulepublic static IRule fromEventRuleArn(Construct scope, String id, String eventRuleArn)public static IRule FromEventRuleArn(Construct scope, string id, string eventRuleArn)func FromEventRuleArn(Scope Construct, Id *string, EventRuleArn *string) IRulestatic#
Import an existing EventBridge Rule provided an ARN.
3 parameters

Parameters

  • scopescopescopescopeScope — The parent creating construct (usually this).
  • ididididId — The construct's name.
  • eventRuleArnevent_rule_arneventRuleArneventRuleArnEventRuleArn — Event Rule ARN (i.e. arn:aws:events:<region>:<account-id>:rule/MyScheduledRule).
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.