从AWS CDK堆栈获取所有节点

时间:2020-07-27 14:07:48

标签: amazon-web-services aws-cdk cdk

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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

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