aws-cdk-lib.Annotations

class Annotations

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

Includes API for attaching annotations such as warning messages to constructs.

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());
  }
}

Methods

NameDescription
addDeprecation(api, message)Adds a deprecation warning for a specific API.
addError(message)Adds an { "error": } metadata entry to this construct.
addInfo(message)Adds an info metadata entry to this construct.
addWarning(message)Adds a warning metadata entry to this construct.
static of(scope)Returns the annotations API for a construct scope.

addDeprecation(api, message)

public addDeprecation(api: string, message: string): void

Parameters

  • api string — The API being deprecated in the format module.Class.property (e.g. @aws-cdk/core.Construct.node).
  • message string — The deprecation message to display, with information about alternatives.

Adds a deprecation warning for a specific API.

Deprecations will be added only once per construct as a warning and will be deduplicated based on the api.

If the environment variable CDK_BLOCK_DEPRECATIONS is set, this method will throw an error instead with the deprecation message.


addError(message)

public addError(message: string): void

Parameters

  • message string — The error message.

Adds an { "error": } metadata entry to this construct.

The toolkit will fail deployment of any stack that has errors reported against it.


addInfo(message)

public addInfo(message: string): void

Parameters

  • message string — The info message.

Adds an info metadata entry to this construct.

The CLI will display the info message when apps are synthesized.


addWarning(message)

public addWarning(message: string): void

Parameters

  • message string — The warning message.

Adds a warning metadata entry to this construct.

The CLI will display the warning when an app is synthesized, or fail if run in --strict mode.


static of(scope)

public static of(scope: IConstruct): Annotations

Parameters

  • scope IConstruct — The scope.

Returns

  • Annotations

Returns the annotations API for a construct scope.