# CfnRule

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.

Extends: Construct › CfnElement › CfnRefElement › CfnRule
Implements: IConstruct, IDependable

- Source: [`core/lib/cfn-rule.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/core/lib/cfn-rule.ts#L59)

## 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');
```

## Initializer

```ts
new CfnRule(scope: Construct, id: string, props?: CfnRuleProps)
```

## Construct Props

- `assertions`: [CfnRuleAssertion](/cdk/api/v2/docs/aws-cdk-lib.CfnRuleAssertion.html)[] = No assertions for the rule. — Assertions which define the rule.

- `ruleCondition`: [ICfnConditionExpression](/cdk/api/v2/docs/aws-cdk-lib.ICfnConditionExpression.html) = Rule's assertions will always take effect. — 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:** Rule's assertions will always take effect.

## Properties

- `creationStack`: string[] (required)

- `logicalId`: string (required) — 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)`.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) (required) — The tree node.

- `ref`: string (required) — 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 })`.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) (required) — 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): void` — Adds an assertion to the rule.

  **Parameters**

  - `condition` — The expression to evaluation.
  - `description` — The description of the assertion.

- `isCfnElement(x: any): boolean` (static) — 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:** The construct as a stack element or undefined if it is not a stack element.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  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**

  - `x` — Any object.

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

- `overrideLogicalId(newLogicalId: string): void` — Overrides the auto-generated logical ID with a specific ID.

  **Parameters**

  - `newLogicalId` — The new logical ID to use for this stack element.

- `toString(): string` — Returns a string representation of this construct.

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