# Aspects

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

- Source: [`core/lib/aspect.ts`](https://github.com/aws/aws-cdk/blob/v2.274.0/packages/aws-cdk-lib/core/lib/aspect.ts#L57)
- Type last changed: v2.172.0 · 7 Dec 2024

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

- `all`: [IAspect](/cdk/api/v2/docs/aws-cdk-lib.IAspect.html)[] (required) — The list of aspects which were directly applied on this scope.

- `applied`: [AspectApplication](/cdk/api/v2/docs/aws-cdk-lib.AspectApplication.html)[] (required) — 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): void` — Adds an aspect to apply this scope before synthesis.

  **Parameters**

  - `aspect` — The aspect to add.
  - `options` — Options to apply on this aspect.

- `of(scope: IConstruct): Aspects // chainable` (static) — Returns the `Aspects` object associated with a construct scope.

  **Parameters**

  - `scope` — The scope for which these aspects will apply.
