@Stability(Experimental)
Package software.aws.awsprototypingsdk.cdkgraph
CDK Graph (@aws-prototyping-sdk/cdk-graph)
More comprehensive documentation to come as this package stabilizes
This package is the core framework for supporting additional cdk based automation and tooling, such as diagraming, cost modeling, security and compliance, in a holistic and comprehensive way.
This package provides the following functionality:
- Synthesizes a serialized graph (nodes and edges) from CDK source code.
- Provides runtime interface for interacting with the graph (in-memory database-like graph store).
- Provides plugin framework for additional tooling to utilize and extend the graph.
The goal of this framework is to enable bespoke tooling to be built without having to first traverse the CDK Tree and Metadata to build a graph. Projects like cdk-dia generate a bespoke in-memory graph that is then utilized to generate diagrams; while the diagram generation is the core value it must first have a graph to act upon and currently is required to generate this undifferentiated graph to provide its diagrams. By standardizing on the graph interface necessary to build complex tooling, we can more rapidly build new tooling that focuses on its core value.
Available Plugins
| Name | Description | Screenshot | Links |
|--- | --- | --- | --- |
| Diagram | Generate cloud infrastructure diagrams from cdk graph |
|
|
Quick Start
Instrument CDK App with CdkGraph
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import { MyStack } from "../lib/my-stack";
import { CdkGraph } from "@aws-prototyping-sdk/cdk-graph";
const app = new cdk.App();
new MyStack(app, "MyStack");
// Add CdkGraph after other construct added to app
new CdkGraph(app);
Using Plugins
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import { MyStack } from "../lib/my-stack";
import { CdkGraph } from "@aws-prototyping-sdk/cdk-graph";
import { ExamplePlugin } from "@aws-prototyping-sdk/cdk-graph-plugin-example"; // does not exist, just example
const app = new cdk.App();
new MyStack(app, "MyStack");
// Add CdkGraph after other construct added to app
new CdkGraph(app, {
plugins: [new ExamplePlugin()],
});
Config
Configuration is supported through the .cdkgraphrc.js and depending on the plugin, through passing config to the plugin instance.
Config precedence follows 1) defaults, 2) cdkgraphrc, 3) instance.
// .cdkgraphrc.js
module.exports = {
// Defaults to "<cdk.out>/cdkgraph"
outdir: "reports/graph",
// plugin configuration
example: {
verbose: true,
reportType: "csv",
},
};
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import { MyStack } from "../lib/my-stack";
import { CdkGraph } from "@aws-prototyping-sdk/cdk-graph";
import { ExamplePlugin } from "@aws-prototyping-sdk/cdk-graph-plugin-example"; // does not exist, just example
const app = new cdk.App();
new MyStack(app, "MyStack");
// Add CdkGraph after other construct added to app
new CdkGraph(app, {
plugins: [
new ExamplePlugin({
// Will override .cdkgraphrc.js value
verbose: false,
}),
],
});
Plugin Interface
/** CdkGraph **Plugin** interface */
export interface ICdkGraphPlugin {
/** Unique identifier for this plugin */
readonly id: string;
/** Plugin version */
readonly version: Version;
/** List of plugins this plugin depends on, including optional semver version (eg: ["foo", "bar@1.2"]) */
readonly dependencies?: string[];
/**
* Binds the plugin to the CdkGraph instance. Enables plugins to receive base configs.
*/
bind: IGraphPluginBindCallback;
/**
* Node visitor callback for construct tree traversal. This follows IAspect.visit pattern, but the order
* of visitor traversal in managed by the CdkGraph.
*/
inspect?: IGraphVisitorCallback;
/**
* Called during CDK synthesize to generate synchronous artifacts based on the in-memory graph passed
* to the plugin. This is called in fifo order of plugins.
*/
synthesize?: IGraphSynthesizeCallback;
/**
* Generate asynchronous reports based on the graph. This is not automatically called when synthesizing CDK.
* Developer must explicitly add `await graphInstance.report()` to the CDK bin or invoke this outside
* of the CDK synth. In either case, the plugin receives the in-memory graph interface when invoked, as the
* CdkGraph will deserialize the graph prior to invoking the plugin report.
*/
report?: IGraphReportCallback;
}
Plugin operations are automatically invoked by CdkGraph in the order they are defined in the plugins property. The invocation flow of plugins follows: 1) bind, 2) inspect, 3) synthesize, 4) async report.
Asynchronous Plugins
Some plugins may requiring performing asynchronous requests, or may make expensive operations that are best left outside of the synthesis process.
CdkGraph support asynchronous operations through the async report() method of plugins. However, since CDK does not support asynchronous operations during synthesis, this must be wired up a bit differently.
#!/usr/bin/env node
import * as cdk from "aws-cdk-lib";
import { MyStack } from "../lib/my-stack";
import { CdkGraph } from "@aws-prototyping-sdk/cdk-graph";
import { ExampleAsyncPlugin } from "@aws-prototyping-sdk/cdk-graph-plugin-async-example"; // does not exist, just example
(async () => {
const app = new cdk.App();
new MyStack(app, "MyStack");
// Add CdkGraph after other construct added to app
const graph = new CdkGraph(app, {
plugins: [new ExampleAsyncPlugin()],
});
// invokes all plugin `report()` operations asynchronously (in order they are defined in `plugins` property)
await graph.report();
})();
Example Plugin Implementation
Very basic example of implementing a plugin. Once the first actual plugins have been published this will be updated to reference those as examples.
import {
CdkGraph,
CdkGraphContext,
ICdkGraphPlugin,
} from "@aws-prototyping-sdk/cdk-graph";
export class CdkGraphExamplePlugin implements ICdkGraphPlugin {
static readonly ARTIFACT_NS = "EXAMPLE";
static readonly ID = "example";
static readonly VERSION = "0.0.0";
get id(): string {
return CdkGraphDiagramPlugin.ID;
}
get version(): string {
return CdkGraphDiagramPlugin.VERSION;
}
readonly dependencies?: string[] = [];
/** @internal */
private _graph?: CdkGraph;
bind(graph: CdkGraph): void {
this._graph = graph;
}
synthesize(context: CdkGraphContext): void {
const pluginConfig = this.config as Required<IPluginConfig>;
// Get counts of all resources
const cfnResourceCounts = context.store.counts.cfnResources;
// Write plugin artifact
context.writeArtifact(
this,
"EXAMPLE",
"example.json",
JSON.stringify(cfnResourceCounts, null, 2)
);
}
async report(context: CdkGraphContext): void {
// perform async operation here utilizing graph store
const cfnResourceCounts = context.store.counts.cfnResources;
const fetchedData = await fetch("https://example.com/data", {
method: "POST",
body: JSON.stringify(cfnResourceCounts),
});
// Write plugin artifact for fetched data
context.writeArtifact(
this,
"EXAMPLE:FETCHED",
"example-fetched.json",
JSON.stringify(fetchedData, null, 2)
);
}
}
Path to Stability
The below is a rough checklist of task necessary to elevate this from experimental to stable.
- [ ] Dynamic versioning and Semver enforcement (store, plugins, etc)
- [ ] Support running
async report()method outside of CDK synthesis process - [ ] Find alternative synthesis solution that doesn't utilize CDK internals
- [ ] Support custom Nodes and Edges
- [ ] Improve logging, bookkeeping, and debugging
- [ ] Implement store upgrade solution
- [ ] Battle test the implementation against several plugins
- [ ] Battle test the implementation in all target languages (currently tested in Typescript, but vended in all PDK supported languages)
- [ ] Receive community feedback to validate approach
-
Interface Summary Interface Description CdkGraphArtifact (experimental) CdkGraph artifact definition.ConstructInfo (experimental) Source information on a construct (class fqn and version).IAppNodeProps (experimental)AppNodeprops.IAppNodeProps.Jsii$Default Internal default implementation forIAppNodeProps.IAttributeReferenceProps (experimental) Attribute type reference props.IAttributeReferenceProps.Jsii$Default Internal default implementation forIAttributeReferenceProps.IBaseEntityDataProps (experimental) Base interface for all store entities data props.IBaseEntityDataProps.Jsii$Default Internal default implementation forIBaseEntityDataProps.IBaseEntityProps (experimental) Base interface for all store entities props.IBaseEntityProps.Jsii$Default Internal default implementation forIBaseEntityProps.ICdkGraphPlugin (experimental) CdkGraph Plugin interface.ICdkGraphPlugin.Jsii$Default Internal default implementation forICdkGraphPlugin.ICdkGraphProps (experimental)CdkGraphprops.ICfnResourceNodeProps (experimental) CfnResourceNode props.ICfnResourceNodeProps.Jsii$Default Internal default implementation forICfnResourceNodeProps.IEdgePredicate (experimental) Predicate to match edge.IEdgePredicate.Jsii$Default Internal default implementation forIEdgePredicate.IEdgeProps (experimental) Edge props interface.IEdgeProps.Jsii$Default Internal default implementation forIEdgeProps.IFilterFocusCallback (experimental) Determines focus node of filter plan.IFilterFocusCallback.Jsii$Default Internal default implementation forIFilterFocusCallback.IFindEdgeOptions (experimental) Options for edge based search operations.IFindEdgeOptions.Jsii$Default Internal default implementation forIFindEdgeOptions.IFindNodeOptions (experimental) Options for node based search operations.IFindNodeOptions.Jsii$Default Internal default implementation forIFindNodeOptions.IGraphFilter (experimental) Graph filter.IGraphFilter.Jsii$Default Internal default implementation forIGraphFilter.IGraphFilterPlan (experimental) Graph filter plan.IGraphFilterPlan.Jsii$Default Internal default implementation forIGraphFilterPlan.IGraphFilterPlanFocusConfig IGraphPluginBindCallback (experimental) Callback signature for graphPlugin.bindoperation.IGraphPluginBindCallback.Jsii$Default Internal default implementation forIGraphPluginBindCallback.IGraphReportCallback (experimental) Callback signature for graphPlugin.reportoperation.IGraphReportCallback.Jsii$Default Internal default implementation forIGraphReportCallback.IGraphStoreFilter (experimental) Store filter callback interface used to perform filtering operations directly against the store, as opposed to usingIGraphFilterdefinitions.IGraphStoreFilter.Jsii$Default Internal default implementation forIGraphStoreFilter.IGraphSynthesizeCallback (experimental) Callback signature for graphPlugin.synthesizeoperation.IGraphSynthesizeCallback.Jsii$Default Internal default implementation forIGraphSynthesizeCallback.IGraphVisitorCallback (experimental) Callback signature for graphPlugin.inspectoperation.IGraphVisitorCallback.Jsii$Default Internal default implementation forIGraphVisitorCallback.INestedStackNodeProps (experimental)NestedStackNodeprops.INestedStackNodeProps.Jsii$Default Internal default implementation forINestedStackNodeProps.InferredNodeProps (experimental) Inferred node props.INodePredicate (experimental) Predicate to match node.INodePredicate.Jsii$Default Internal default implementation forINodePredicate.INodeProps (experimental) Node props.INodeProps.Jsii$Default Internal default implementation forINodeProps.IOutputNodeProps (experimental) OutputNode props.IOutputNodeProps.Jsii$Default Internal default implementation forIOutputNodeProps.IParameterNodeProps (experimental)ParameterNodeprops.IParameterNodeProps.Jsii$Default Internal default implementation forIParameterNodeProps.IReferenceProps (experimental) Reference edge props.IReferenceProps.Jsii$Default Internal default implementation forIReferenceProps.IResourceNodeProps (experimental) ResourceNode props.IResourceNodeProps.Jsii$Default Internal default implementation forIResourceNodeProps.ISerializableEdge (experimental) Interface for serializable graph edge entity.ISerializableEdge.Jsii$Default Internal default implementation forISerializableEdge.ISerializableEntity (experimental) Interface for serializable graph entities.ISerializableEntity.Jsii$Default Internal default implementation forISerializableEntity.ISerializableGraphStore (experimental) Interface for serializable graph store.ISerializableGraphStore.Jsii$Default Internal default implementation forISerializableGraphStore.ISerializableNode (experimental) Interface for serializable graph node entity.ISerializableNode.Jsii$Default Internal default implementation forISerializableNode.IStackNodeProps (experimental)StackNodeprops.IStackNodeProps.Jsii$Default Internal default implementation forIStackNodeProps.IStoreCounts (experimental) Interface for store counts.IStoreCounts.Jsii$Default Internal default implementation forIStoreCounts.ITypedEdgeProps (experimental) Base edge props agnostic to edge type.ITypedEdgeProps.Jsii$Default Internal default implementation forITypedEdgeProps.ITypedNodeProps (experimental) Base node props agnostic to node type.ITypedNodeProps.Jsii$Default Internal default implementation forITypedNodeProps.PlainObject (experimental) Serializable plain object value (JSII supported).SGEdge (experimental) Serializable graph edge entity.SGEntity (experimental) Serializable graph entity.SGGraphStore (experimental) Serializable graph store.SGNode (experimental) Serializable graph node entity.SGUnresolvedReference (experimental) Unresolved reference struct. -
Class Summary Class Description AppNode (experimental) AppNode defines a cdk App.AttributeReference (experimental) Attribute type reference edge.BaseEntity (experimental) Base class for all store entities (Node and Edges).CdkGraph (experimental) CdkGraph construct is the cdk-graph framework controller that is responsible for computing the graph, storing serialized graph, and instrumenting plugins per the plugin contract.CdkGraph.Builder (experimental) A fluent builder forCdkGraph.CdkGraphArtifact.Builder A builder forCdkGraphArtifactCdkGraphArtifact.Jsii$Proxy An implementation forCdkGraphArtifactCdkGraphContext (experimental) CdkGraph context.CfnResourceNode (experimental) CfnResourceNode defines an L1 cdk resource.ConstructInfo.Builder A builder forConstructInfoConstructInfo.Jsii$Proxy An implementation forConstructInfoDependency (experimental) Dependency edge class defines CloudFormation dependency between resources.Edge (experimental) Edge class defines a link (relationship) between nodes, as in standard graph theory.IAppNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IAttributeReferenceProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IBaseEntityDataProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IBaseEntityProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ICdkGraphPlugin.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ICdkGraphProps.Builder A builder forICdkGraphPropsICdkGraphProps.Jsii$Proxy An implementation forICdkGraphPropsICfnResourceNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IEdgePredicate.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IEdgeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IFilterFocusCallback.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IFindEdgeOptions.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IFindNodeOptions.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphFilter.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphFilterPlan.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphFilterPlanFocusConfig.Builder A builder forIGraphFilterPlanFocusConfigIGraphFilterPlanFocusConfig.Jsii$Proxy An implementation forIGraphFilterPlanFocusConfigIGraphPluginBindCallback.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphReportCallback.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphStoreFilter.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphSynthesizeCallback.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IGraphVisitorCallback.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ImportReference (experimental) Import reference defines Fn::ImportValue type reference edge.INestedStackNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.InferredNodeProps.Builder A builder forInferredNodePropsInferredNodeProps.Jsii$Proxy An implementation forInferredNodePropsINodePredicate.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.INodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IOutputNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IParameterNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IReferenceProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IResourceNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ISerializableEdge.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ISerializableEntity.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ISerializableGraphStore.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ISerializableNode.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IStackNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.IStoreCounts.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ITypedEdgeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.ITypedNodeProps.Jsii$Proxy A proxy class which represents a concrete javascript instance of this type.NestedStackNode (experimental) NestedStackNode defines a cdk NestedStack.Node (experimental) Node class is the base definition of node entities in the graph, as in standard graph theory.OutputNode (experimental) OutputNode defines a cdk CfnOutput resources.ParameterNode (experimental) ParameterNode defines a CfnParameter node.PlainObject.Builder A builder forPlainObjectPlainObject.Jsii$Proxy An implementation forPlainObjectReference (experimental) Reference edge class defines a directed relationship between nodes.ResourceNode (experimental) ResourceNode class defines a L2 cdk resource construct.RootNode (experimental) RootNode represents the root of the store tree.SGEdge.Builder A builder forSGEdgeSGEdge.Jsii$Proxy An implementation forSGEdgeSGEntity.Builder A builder forSGEntitySGEntity.Jsii$Proxy An implementation forSGEntitySGGraphStore.Builder A builder forSGGraphStoreSGGraphStore.Jsii$Proxy An implementation forSGGraphStoreSGNode.Builder A builder forSGNodeSGNode.Jsii$Proxy An implementation forSGNodeSGUnresolvedReference.Builder A builder forSGUnresolvedReferenceSGUnresolvedReference.Jsii$Proxy An implementation forSGUnresolvedReferenceStackNode (experimental) StackNode defines a cdk Stack.StageNode (experimental) StageNode defines a cdk Stage.Store (experimental) Store class provides the in-memory database-like interface for managing all entities in the graph. -
Enum Summary Enum Description CdkConstructIds (experimental) Common cdk construct ids.CdkGraphArtifacts (experimental) CdkGraph core artifacts.CfnAttributesEnum (experimental) Common cfn attribute keys.ConstructInfoFqnEnum (experimental) Commonly used cdk construct info fqn (jsii fully-qualified ids).EdgeDirectionEnum (experimental) EdgeDirection specifies in which direction the edge is directed or if it is undirected.EdgeTypeEnum (experimental) Edge types handles by the graph.FilterPreset (experimental) Filter presets.FilterStrategy (experimental) Filter strategy to apply to filter matches.FlagEnum (experimental) Graph flags.MetadataTypeEnum (experimental) Common cdk metadata types.NodeTypeEnum (experimental) Node types handled by the graph.ReferenceTypeEnum (experimental) Reference edge types.