Skip to content
v2.273.0

CfnRule L1 construct ​

The Rules that define template constraints in an AWS Service Catalog portfolio describe when end users can use the template and which values they can specify for parameters that are declared in the AWS CloudFormation template used to create the product they are attempting to use.

Rules are useful for preventing end users from inadvertently specifying an incorrect value. For example, you can add a rule to verify whether end users specified a valid subnet in a given VPC or used m1.small instance types for test environments. AWS CloudFormation uses rules to validate parameter values before it creates the resources for the product.

A rule can include a RuleCondition property and must include an Assertions property. For each rule, you can define only one rule condition; you can define one or more asserts within the Assertions property. You define a rule condition and assertions by using rule-specific intrinsic functions.

Example ​

ts
declare const cfnTemplate: cfn_inc.CfnInclude;
const rule: core.CfnRule = cfnTemplate.getRule('MyRule');

// mutating the rule
declare const myParameter: core.CfnParameter;
rule.addAssertion(core.Fn.conditionContains(['m1.small'], myParameter.valueAsString),
  'MyParameter has to be m1.small');
python
# cfn_template: cfn_inc.CfnInclude

# mutating the rule
# my_parameter: core.CfnParameter

rule = cfn_template.get_rule("MyRule")
rule.add_assertion(core.Fn.condition_contains(["m1.small"], my_parameter.value_as_string), "MyParameter has to be m1.small")
java
CfnInclude cfnTemplate;

// mutating the rule
CfnParameter myParameter;

CfnRule rule = cfnTemplate.getRule("MyRule");
rule.addAssertion(Fn.conditionContains(List.of("m1.small"), myParameter.getValueAsString()), "MyParameter has to be m1.small");
csharp
// Install: dotnet add package Amazon.CDK.Lib
CfnInclude cfnTemplate;

// mutating the rule
CfnParameter myParameter;

var rule = cfnTemplate.GetRule("MyRule");
rule.AddAssertion(Fn.ConditionContains(new [] { "m1.small" }, myParameter.ValueAsString), "MyParameter has to be m1.small");
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var cfnTemplate CfnInclude

// mutating the rule
var myParameter CfnParameter

rule := cfnTemplate.GetRule(jsii.String("MyRule"))
rule.AddAssertion(core.Fn_ConditionContains([]*string{
	jsii.String("m1.small"),
}, myParameter.valueAsString), jsii.String("MyParameter has to be m1.small"))

Initializer ​

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

Construct Props ​

assertionsCfnRuleAssertion[]assertionstyping.List[CfnRuleAssertion]assertionsList<CfnRuleAssertion>AssertionsCfnRuleAssertion[]Assertions*[]CfnRuleAssertion= No assertions for the rule.#
Assertions which define the rule.
ruleConditionICfnConditionExpressionrule_conditionICfnConditionExpressionruleConditionICfnConditionExpressionRuleConditionICfnConditionExpressionRuleConditionICfnConditionExpression= Rule's assertions will always ta…#
If the rule condition evaluates to false, the rule doesn't take effect.

If the function in the rule condition evaluates to true, expressions in each assert are evaluated and applied.

default

Default: Rule's assertions will always take effect.

Properties ​

creationStackstring[]creation_stacktyping.List[str]creationStackList<String>CreationStackstring[]CreationStack*[]*stringrequired#
logicalIdstringlogical_idstrlogicalIdStringLogicalIdstringLogicalId*stringrequired#
The logical ID for this CloudFormation stack element.

The logical ID of the element is calculated from the path of the resource node in the construct tree.

To override this value, use overrideLogicalId(newLogicalId).

nodeNodenodeNodenodeNodeNodeNodeNodeNoderequired#
The tree node.
refstringrefstrrefStringRefstringRef*stringrequired#
Return a string that will be resolved to a CloudFormation `{ Ref }` for this element.

If, by any chance, the intrinsic reference of a resource is not a string, you could coerce it to an IResolvable through Lazy.any({ produce: resource.ref }).

stackStackstackStackstackStackStackStackStackStackrequired#
The stack in which this element is defined.

CfnElements must be defined within a stack scope (directly or indirectly).

Methods ​

addAssertion(condition: ICfnConditionExpression, description: string): voiddef add_assertion(self, condition: ICfnConditionExpression, description: str) -> Nonepublic void addAssertion(ICfnConditionExpression condition, String description)public void AddAssertion(ICfnConditionExpression condition, string description)func AddAssertion(Condition ICfnConditionExpression, Description *string)#
Adds an assertion to the rule.
2 parameters

Parameters

  • conditionconditionconditionconditionCondition — The expression to evaluation.
  • descriptiondescriptiondescriptiondescriptionDescription — The description of the assertion.
isCfnElement(x: any): boolean@staticmethod def is_cfn_element(x: Any) -> boolpublic static Boolean isCfnElement(Object x)public static bool IsCfnElement(object x)func IsCfnElement(X interface{}) *boolstatic#
Returns `true` if a construct is a stack element (i.e. part of the synthesized cloudformation template).

Uses duck-typing instead of instanceof to allow stack elements from different versions of this library to be included in the same stack.

returns

Returns: The construct as a stack element or undefined if it is not a stack element.

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.

overrideLogicalId(newLogicalId: string): voiddef override_logical_id(self, new_logical_id: str) -> Nonepublic void overrideLogicalId(String newLogicalId)public void OverrideLogicalId(string newLogicalId)func OverrideLogicalId(NewLogicalId *string)#
Overrides the auto-generated logical ID with a specific ID.
1 parameter

Parameters

  • newLogicalIdnew_logical_idnewLogicalIdnewLogicalIdNewLogicalId — The new logical ID to use for this stack element.
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.