从 AWS CDK 堆栈获取所有节点

Get all nodes from an AWS CDK stack

是否可以从堆栈中获取所有节点(cdk.ConstructNode)?

我希望能够递归地迭代所有节点并检查它们的元数据。

解决方案是使用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)
    }
  }
}