Aspects Class
Aspects can be applied to CDK tree scopes and can operate on the tree before synthesis.
| Source | core/lib/aspect.ts |
|---|---|
| Type last changed |
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());
}
}
python
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())
java
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());
}
}
csharp
// 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());
}
}
go
// 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
}
Properties
The list of aspects which were directly applied on this scope.
appliedAspectApplication[]appliedtyping.List[AspectApplication]appliedList<AspectApplication>AppliedAspectApplication[]Applied*[]AspectApplication #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) Aspects static #Returns the `Aspects` object associated with a construct scope.
1 parameter
Parameters
scopescopescopescopeScope— The scope for which these aspects will apply.