Aspects
Aspects allow you to apply an operation to all constructs within a given scope. You may want to use them in your CDK for Terraform (CDKTF) application to mutate elements (e.g., add tags to cloud resources) or for validation (e.g., ensure all S3 Buckets are encrypted).
Define Aspects
To create an aspect, you must import the Aspects
class and the IAspect
interface and implement one or more methods for IAspect
. Then, you can call the aspect one or more times on any construct within your application.
Everything within a CDKTF application descends from the Construct
class, so you could call the construct on any instantiated element. This includes the entire application, a particular stack, or all of the resources for a specific provider. When you call the aspect, CDKTF applies its methods to all of the the constructs that fall within the specified scope.
The TypeScript example below defines an aspect to add tags to resources.
You can also use aspects for validation. The TypeScript example below defines an aspect that checks whether all S3 Buckets start with the correct prefix.