Get all nodes from an AWS CDK stack

Viewed 702

Is it possible to get all nodes (cdk.ConstructNode) from a stack?

I want to be able to iterate all of the nodes recursively and check their metadata.

1 Answers

The solution is to use IAspect

import { IAspect, IConstruct } from '@aws-cdk/core'

import { LambdaWebpack } from './lambda-webpack'

/**
 * Class that uses a visitor pattern to find all our Lambda functions and aggregate them in
 * the `lambdas` property for access later.
 */
export class LambdaAggregator implements IAspect {
  /**
   * Gathers all lambdas from the stack in this property.
   */
  public readonly lambdas: LambdaWebpack[] = []

  public visit(node: IConstruct): void {
    if (node instanceof LambdaWebpack) {
      this.lambdas.push(node)
    }
  }
}
Related