Annotations Class
Includes API for attaching annotations such as warning messages to constructs.
| Source | core/lib/annotations.ts |
|---|---|
| Type last changed |
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());
}
}
import aws_cdk as cdk
from constructs import Construct, IConstruct
@jsii.implements(cdk.IAspect)
class MyAspect:
def visit(self, node):
if node instanceof cdk.CfnResource && node.cfn_resource_type == "Foo::Bar":
self.error(node, "we do not want a Foo::Bar resource")
def error(self, node, message):
cdk.Annotations.of(node).add_error(message)
class MyStack(cdk.Stack):
def __init__(self, scope, id):
super().__init__(scope, id)
stack = cdk.Stack()
cdk.CfnResource(stack, "Foo",
type="Foo::Bar",
properties={
"Fred": "Thud"
}
)
cdk.Aspects.of(stack).add(MyAspect())
import software.amazon.awscdk.*;
import software.constructs.Construct;
import software.constructs.IConstruct;
public class MyAspect implements IAspect {
public void visit(IConstruct node) {
if (node instanceof CfnResource && node.getCfnResourceType() == "Foo::Bar") {
this.error(node, "we do not want a Foo::Bar resource");
}
}
public void error(IConstruct node, String message) {
Annotations.of(node).addError(message);
}
}
public class MyStack extends Stack {
public MyStack(Construct scope, String id) {
super(scope, id);
Stack stack = new Stack();
CfnResource.Builder.create(stack, "Foo")
.type("Foo::Bar")
.properties(Map.of(
"Fred", "Thud"))
.build();
Aspects.of(stack).add(new MyAspect());
}
}
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK;
using Constructs;
class MyAspect : IAspect
{
public void Visit(IConstruct node)
{
if (node instanceof CfnResource && node.CfnResourceType == "Foo::Bar")
{
Error(node, "we do not want a Foo::Bar resource");
}
}
protected void Error(IConstruct node, string message)
{
Annotations.Of(node).AddError(message);
}
}
class MyStack : Stack
{
public MyStack(Construct scope, string id) : base(scope, id)
{
var stack = new Stack();
new CfnResource(stack, "Foo", new CfnResourceProps {
Type = "Foo::Bar",
Properties = new Dictionary<string, object> {
{ "Fred", "Thud" }
}
});
Aspects.Of(stack).Add(new MyAspect());
}
}
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/constructs-go/constructs"
type myAspect struct {
}
func (this *myAspect) visit(node IConstruct) {
if *node instanceof cdk.CfnResource && *node.CfnResourceType == "Foo::Bar" {
this.error(*node, jsii.String("we do not want a Foo::Bar resource"))
}
}
func (this *myAspect) error(node IConstruct, message *string) {
cdk.Annotations_Of(*node).AddError(*message)
}
type myStack struct {
Stack
}
func newMyStack(scope Construct, id *string) *myStack {
this := &myStack{}
cdk.NewStack_Override(this, scope, id)
stack := cdk.NewStack()
cdk.NewCfnResource(stack, jsii.String("Foo"), &CfnResourceProps{
Type: jsii.String("Foo::Bar"),
Properties: map[string]interface{}{
"Fred": jsii.String("Thud"),
},
})
cdk.Aspects_Of(stack).Add(newMyAspect())
return this
}
Methods
acknowledgeInfo(id: string, message?: string): voiddef acknowledge_info(self, id: str, message: typing.Optional[str] = None) -> Nonepublic void acknowledgeInfo(String id, String message)public void AcknowledgeInfo(string id, string? message = null)func AcknowledgeInfo(Id *string, Message *string) #The acknowledgement will apply to all child scopes
2 parameters · example
Parameters
ididididId— - the id of the info message to acknowledge.messagemessagemessagemessageMessage— optional message to explain the reason for acknowledgement.
declare const myConstruct: Construct;
Annotations.of(myConstruct).acknowledgeInfo('SomeInfoId', 'This info can be ignored because...');
acknowledgeWarning(id: string, message?: string): voiddef acknowledge_warning(self, id: str, message: typing.Optional[str] = None) -> Nonepublic void acknowledgeWarning(String id, String message)public void AcknowledgeWarning(string id, string? message = null)func AcknowledgeWarning(Id *string, Message *string) #The acknowledgement will apply to all child scopes
2 parameters · example
Parameters
ididididId— - the id of the warning message to acknowledge.messagemessagemessagemessageMessage— optional message to explain the reason for acknowledgement.
declare const myConstruct: Construct;
Annotations.of(myConstruct).acknowledgeWarning('SomeWarningId', 'This warning can be ignored because...');
addDeprecation(api: string, message: string): voiddef add_deprecation(self, api: str, message: str) -> Nonepublic void addDeprecation(String api, String message)public void AddDeprecation(string api, string message)func AddDeprecation(Api *string, Message *string) #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.
2 parameters
Parameters
apiapiapiapiApi— The API being deprecated in the formatmodule.Class.property(e.g.@aws-cdk/core.Construct.node).messagemessagemessagemessageMessage— The deprecation message to display, with information about alternatives.
addError(message: string): voiddef add_error(self, message: str) -> Nonepublic void addError(String message)public void AddError(string message)func AddError(Message *string) #The toolkit will fail deployment of any stack that has errors reported against it. Prefer using Validations.of(scope).addError() instead.
1 parameter
Parameters
messagemessagemessagemessageMessage— The error message.
addInfo(message: string): voiddef add_info(self, message: str) -> Nonepublic void addInfo(String message)public void AddInfo(string message)func AddInfo(Message *string) #The CLI will display the info message when apps are synthesized.
1 parameter
Parameters
messagemessagemessagemessageMessage— The info message.
addInfoV2(id: string, message: string): voiddef add_info_v2(self, id: str, message: str) -> Nonepublic void addInfoV2(String id, String message)public void AddInfoV2(string id, string message)func AddInfoV2(Id *string, Message *string) #The CLI will display the info when an app is synthesized.
If the info is acknowledged using acknowledgeInfo(), it will not be shown by the CLI.
2 parameters · example
Parameters
ididididId— the unique identifier for the info.messagemessagemessagemessageMessage— The info message.
declare const myConstruct: Construct;
Annotations.of(myConstruct).addInfoV2('my-library:Construct.someInfo', 'Some message explaining the info');
addWarning(message: string): voiddef add_warning(self, message: str) -> Nonepublic void addWarning(String message)public void AddWarning(string message)func AddWarning(Message *string) #notes · 1 parameter
The CLI will display the warning when an app is synthesized, or fail if run in --strict mode.
Warnings added by this call cannot be acknowledged. This will block users from running in --strict mode until the deal with the warning, which makes it effectively not very different from addError. Prefer using addWarningV2 instead.
Parameters
messagemessagemessagemessageMessage— The warning message.
addWarningV2(id: string, message: string, options?: WarningOptions): voiddef add_warning_v2(self, id: str, message: str, options: typing.Optional[WarningOptions] = None) -> Nonepublic void addWarningV2(String id, String message, WarningOptions options)public void AddWarningV2(string id, string message, WarningOptions? options = null)func AddWarningV2(Id *string, Message *string, Options WarningOptions) #notes · 3 parameters · example
The CLI will display the warning when an app is synthesized, or fail if run in --strict mode.
If the warning is acknowledged using acknowledgeWarning(), it will not be shown by the CLI, and will not cause --strict mode to fail synthesis.
Prefer using Validations.of(scope).addWarning() instead.
Parameters
ididididId— the unique identifier for the warning.messagemessagemessagemessageMessage— The warning message.optionsoptionsoptionsoptionsOptions— optional settings, including structuredcontextdescribing this occurrence.
declare const myConstruct: Construct;
Annotations.of(myConstruct).addWarningV2('my-library:Construct.someWarning', 'Some message explaining the warning');
of(scope: IConstruct): Annotations // chainable@staticmethod def of(scope: IConstruct) -> Annotationspublic static Annotations of(IConstruct scope)public static Annotations Of(IConstruct scope)func Of(Scope IConstruct) Annotations static #1 parameter
Parameters
scopescopescopescopeScope— The scope.