Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all nodes from an AWS CDK stack

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.

like image 872
moltar Avatar asked Oct 26 '25 06:10

moltar


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)
    }
  }
}
like image 158
moltar Avatar answered Oct 28 '25 21:10

moltar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!