Skip to content
v2.273.0

Aspects Class ​

Aspects can be applied to CDK tree scopes and can operate on the tree before synthesis.

Example ​

ts
import * as cdk from 'aws-cdk-lib';
import { Construct, IConstruct } from 'constructs';

class MyAspect implements cdk.IAspect {
  public visit(node: IConstruct): void {
    if (node instanceof cdk.CfnResource && node.cfnResourceType === 'Foo::Bar') {
      this.error(node, 'we do not want a Foo::Bar resource');
    }
  }

  protected error(node: IConstruct, message: string): void {
    cdk.Annotations.of(node).addError(message);
  }
}

class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    const stack = new cdk.Stack();
    new cdk.CfnResource(stack, 'Foo', {
      type: 'Foo::Bar',
      properties: {
        Fred: 'Thud',
      },
    });
    cdk.Aspects.of(stack).add(new MyAspect());
  }
}

Properties ​

allIAspect[]alltyping.List[IAspect]allList<IAspect>AllIAspect[]All*[]IAspectrequired#
The list of aspects which were directly applied on this scope.
appliedAspectApplication[]appliedtyping.List[AspectApplication]appliedList<AspectApplication>AppliedAspectApplication[]Applied*[]AspectApplicationrequired#
The list of aspects with priority which were directly applied on this scope.

Also returns inherited Aspects of this node.

Methods ​

add(aspect: IAspect, options?: AspectOptions): voiddef add(self, aspect: IAspect, options: typing.Optional[AspectOptions] = None) -> Nonepublic void add(IAspect aspect, AspectOptions options)public void Add(IAspect aspect, AspectOptions? options = null)func Add(Aspect IAspect, Options AspectOptions)#
Adds an aspect to apply this scope before synthesis.
2 parameters

Parameters

  • aspectaspectaspectaspectAspect — The aspect to add.
  • optionsoptionsoptionsoptionsOptions — Options to apply on this aspect.
of(scope: IConstruct): Aspects // chainable@staticmethod def of(scope: IConstruct) -> Aspectspublic static Aspects of(IConstruct scope)public static Aspects Of(IConstruct scope)func Of(Scope IConstruct) Aspectsstatic#
Returns the `Aspects` object associated with a construct scope.
1 parameter

Parameters

  • scopescopescopescopeScope — The scope for which these aspects will apply.