Skip to main content
Aspects are a powerful tool for applying operations to all constructs within a given scope. They follow the Visitor pattern and are commonly used for cross-cutting concerns such as tagging, compliance checks, and validation.

The IAspect Interface

An Aspect is a class that implements the IAspect interface. This interface requires a single method: visit.
The visit method is called for every construct in the scope where the Aspect is applied, including the scope itself and all its children.

Applying Aspects

You apply an Aspect to a scope (such as an App or a Stack) using the Aspects class.

Use Cases

Tagging

One of the most common use cases for Aspects is applying a consistent set of tags to all resources in a stack.

Compliance and Validation

Aspects can be used to enforce security policies or organizational standards.

Execution Flow

Aspects are invoked during the synthesis process. When app.synth() is called, the framework triggers invokeAspects(). This method performs a depth-first traversal of the construct tree, applying all registered Aspects to each node. Because Aspects run during synthesis, they can modify the properties of resources before the final Terraform HCL is generated. This allows for dynamic adjustments based on the state of the entire construct tree.