aws-cdk-lib.Aspects

class Aspects

LanguageType name
.NETAmazon.CDK.Aspects
Gogithub.com/aws/aws-cdk-go/awscdk/v2#Aspects
Javasoftware.amazon.awscdk.Aspects
Pythonaws_cdk.Aspects
TypeScript (source)aws-cdk-lib » Aspects

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

Example

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

NameTypeDescription
allIAspect[]The list of aspects which were directly applied on this scope.

all

Type: IAspect[]

The list of aspects which were directly applied on this scope.

Methods

NameDescription
add(aspect)Adds an aspect to apply this scope before synthesis.
static of(scope)Returns the Aspects object associated with a construct scope.

add(aspect)

public add(aspect: IAspect): void

Parameters

  • aspect IAspect — The aspect to add.

Adds an aspect to apply this scope before synthesis.


static of(scope)

public static of(scope: IConstruct): Aspects

Parameters

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

Returns

  • Aspects

Returns the Aspects object associated with a construct scope.