Rule Class
Represents a rule in AWS CodePipeline that can be used to add conditions and controls to pipeline execution.
Example
ts
declare const sourceAction: codepipeline_actions.CodeStarConnectionsSourceAction;
declare const buildAction: codepipeline_actions.CodeBuildAction;
new codepipeline.Pipeline(this, 'Pipeline', {
pipelineType: codepipeline.PipelineType.V2,
stages: [
{
stageName: 'Source',
actions: [sourceAction],
},
{
stageName: 'Build',
actions: [buildAction],
// BeforeEntry condition - checks before entering the stage
beforeEntry: {
conditions: [{
rules: [ new codepipeline.Rule({
name: 'LambdaCheck',
provider: 'LambdaInvoke',
version: '1',
configuration: {
FunctionName: 'LambdaFunctionName',
},
})],
result: codepipeline.Result.FAIL,
}],
},
// OnSuccess condition - checks after successful stage completion
onSuccess: {
conditions: [{
result: codepipeline.Result.FAIL,
rules: [new codepipeline.Rule({
name: 'CloudWatchCheck',
provider: 'LambdaInvoke',
version: '1',
configuration: {
AlarmName: 'AlarmName1',
WaitTime: '300', // 5 minutes
FunctionName: 'funcName2'
},
})],
}],
},
// OnFailure condition - handles stage failure
onFailure: {
conditions: [{
result: codepipeline.Result.ROLLBACK,
rules: [new codepipeline.Rule({
name: 'RollBackOnFailure',
provider: 'LambdaInvoke',
version: '1',
configuration: {
AlarmName: 'Alarm',
WaitTime: '300', // 5 minutes
FunctionName: 'funcName1'
},
})],
}],
},
},
],
});
python
# source_action: codepipeline_actions.CodeStarConnectionsSourceAction
# build_action: codepipeline_actions.CodeBuildAction
codepipeline.Pipeline(self, "Pipeline",
pipeline_type=codepipeline.PipelineType.V2,
stages=[codepipeline.StageProps(
stage_name="Source",
actions=[source_action]
), codepipeline.StageProps(
stage_name="Build",
actions=[build_action],
# BeforeEntry condition - checks before entering the stage
before_entry=codepipeline.Conditions(
conditions=[codepipeline.Condition(
rules=[codepipeline.Rule(
name="LambdaCheck",
provider="LambdaInvoke",
version="1",
configuration={
"FunctionName": "LambdaFunctionName"
}
)],
result=codepipeline.Result.FAIL
)]
),
# OnSuccess condition - checks after successful stage completion
on_success=codepipeline.Conditions(
conditions=[codepipeline.Condition(
result=codepipeline.Result.FAIL,
rules=[codepipeline.Rule(
name="CloudWatchCheck",
provider="LambdaInvoke",
version="1",
configuration={
"AlarmName": "AlarmName1",
"WaitTime": "300", # 5 minutes
"FunctionName": "funcName2"
}
)]
)]
),
# OnFailure condition - handles stage failure
on_failure=codepipeline.FailureConditions(
conditions=[codepipeline.Condition(
result=codepipeline.Result.ROLLBACK,
rules=[codepipeline.Rule(
name="RollBackOnFailure",
provider="LambdaInvoke",
version="1",
configuration={
"AlarmName": "Alarm",
"WaitTime": "300", # 5 minutes
"FunctionName": "funcName1"
}
)]
)]
)
)
]
)
java
CodeStarConnectionsSourceAction sourceAction;
CodeBuildAction buildAction;
Pipeline.Builder.create(this, "Pipeline")
.pipelineType(PipelineType.V2)
.stages(List.of(StageProps.builder()
.stageName("Source")
.actions(List.of(sourceAction))
.build(), StageProps.builder()
.stageName("Build")
.actions(List.of(buildAction))
// BeforeEntry condition - checks before entering the stage
.beforeEntry(Conditions.builder()
.conditions(List.of(Condition.builder()
.rules(List.of(Rule.Builder.create()
.name("LambdaCheck")
.provider("LambdaInvoke")
.version("1")
.configuration(Map.of(
"FunctionName", "LambdaFunctionName"))
.build()))
.result(Result.FAIL)
.build()))
.build())
// OnSuccess condition - checks after successful stage completion
.onSuccess(Conditions.builder()
.conditions(List.of(Condition.builder()
.result(Result.FAIL)
.rules(List.of(Rule.Builder.create()
.name("CloudWatchCheck")
.provider("LambdaInvoke")
.version("1")
.configuration(Map.of(
"AlarmName", "AlarmName1",
"WaitTime", "300", // 5 minutes
"FunctionName", "funcName2"))
.build()))
.build()))
.build())
// OnFailure condition - handles stage failure
.onFailure(FailureConditions.builder()
.conditions(List.of(Condition.builder()
.result(Result.ROLLBACK)
.rules(List.of(Rule.Builder.create()
.name("RollBackOnFailure")
.provider("LambdaInvoke")
.version("1")
.configuration(Map.of(
"AlarmName", "Alarm",
"WaitTime", "300", // 5 minutes
"FunctionName", "funcName1"))
.build()))
.build()))
.build())
.build()))
.build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
CodeStarConnectionsSourceAction sourceAction;
CodeBuildAction buildAction;
new Pipeline(this, "Pipeline", new PipelineProps {
PipelineType = PipelineType.V2,
Stages = new [] { new StageProps {
StageName = "Source",
Actions = new [] { sourceAction }
}, new StageProps {
StageName = "Build",
Actions = new [] { buildAction },
// BeforeEntry condition - checks before entering the stage
BeforeEntry = new Conditions {
Conditions = new [] { new Condition {
Rules = new [] { new Rule(new RuleProps {
Name = "LambdaCheck",
Provider = "LambdaInvoke",
Version = "1",
Configuration = new Dictionary<string, string> {
{ "FunctionName", "LambdaFunctionName" }
}
}) },
Result = Result.FAIL
} }
},
// OnSuccess condition - checks after successful stage completion
OnSuccess = new Conditions {
Conditions = new [] { new Condition {
Result = Result.FAIL,
Rules = new [] { new Rule(new RuleProps {
Name = "CloudWatchCheck",
Provider = "LambdaInvoke",
Version = "1",
Configuration = new Dictionary<string, string> {
{ "AlarmName", "AlarmName1" },
{ "WaitTime", "300" }, // 5 minutes
{ "FunctionName", "funcName2" }
}
}) }
} }
},
// OnFailure condition - handles stage failure
OnFailure = new FailureConditions {
Conditions = new [] { new Condition {
Result = Result.ROLLBACK,
Rules = new [] { new Rule(new RuleProps {
Name = "RollBackOnFailure",
Provider = "LambdaInvoke",
Version = "1",
Configuration = new Dictionary<string, string> {
{ "AlarmName", "Alarm" },
{ "WaitTime", "300" }, // 5 minutes
{ "FunctionName", "funcName1" }
}
}) }
} }
}
} }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var sourceAction CodeStarConnectionsSourceAction
var buildAction CodeBuildAction
codepipeline.NewPipeline(this, jsii.String("Pipeline"), &PipelineProps{
PipelineType: codepipeline.PipelineType_V2,
Stages: []StageProps{
&StageProps{
StageName: jsii.String("Source"),
Actions: []IAction{
sourceAction,
},
},
&StageProps{
StageName: jsii.String("Build"),
Actions: []IAction{
buildAction,
},
// BeforeEntry condition - checks before entering the stage
BeforeEntry: &Conditions{
Conditions: []Condition{
&Condition{
Rules: []Rule{
codepipeline.NewRule(&RuleProps{
Name: jsii.String("LambdaCheck"),
Provider: jsii.String("LambdaInvoke"),
Version: jsii.String("1"),
Configuration: map[string]*string{
"FunctionName": jsii.String("LambdaFunctionName"),
},
}),
},
Result: codepipeline.Result_FAIL,
},
},
},
// OnSuccess condition - checks after successful stage completion
OnSuccess: &Conditions{
Conditions: []Condition{
&Condition{
Result: codepipeline.Result_FAIL,
Rules: []Rule{
codepipeline.NewRule(&RuleProps{
Name: jsii.String("CloudWatchCheck"),
Provider: jsii.String("LambdaInvoke"),
Version: jsii.String("1"),
Configuration: map[string]*string{
"AlarmName": jsii.String("AlarmName1"),
"WaitTime": jsii.String("300"),
// 5 minutes
"FunctionName": jsii.String("funcName2"),
},
}),
},
},
},
},
// OnFailure condition - handles stage failure
OnFailure: &FailureConditions{
Conditions: []Condition{
&Condition{
Result: codepipeline.Result_ROLLBACK,
Rules: []Rule{
codepipeline.NewRule(&RuleProps{
Name: jsii.String("RollBackOnFailure"),
Provider: jsii.String("LambdaInvoke"),
Version: jsii.String("1"),
Configuration: map[string]*string{
"AlarmName": jsii.String("Alarm"),
"WaitTime": jsii.String("300"),
// 5 minutes
"FunctionName": jsii.String("funcName1"),
},
}),
},
},
},
},
},
},
})
Initializer
Properties
ruleNamemay be undefinedstringrule_namemay be undefinedstrruleNamemay be undefinedStringRuleNamemay be undefinedstringRuleNamemay be undefined*string#The name of the rule, if specified in the properties.
Methods
reference(): stringdef reference(self, ) -> strpublic String reference()public string Reference()func Reference() *string#Returns a reference to the rule that can be used in pipeline stage conditions.
returns
Returns: A string in the format "#{rule.ruleName}" that can be used to reference this rule