In this tutorial, we’ll show how to automatically generate a visualisation of your AWS stack during deployment. This approach eliminates the need for manual diagramming by producing up-to-date, accurate visual representations of complex infrastructure directly from your code. Such visualizations enhance collaboration, streamline troubleshooting, and help teams better understand resource relationships. Moving from “diagrams as a manual task” to “diagrams as code” saves time and ensures your visuals always reflect the live infrastructure.
Prerequisites
You should have a stack defined as described in Get started with CDK4J and be familiar with basic Maven usage.
Adding Diagram Generation to the Build
We will use the CDK4J diagram] tool integrated into the Maven build process. Add the following plugin configuration to your pom.xml:
<!-- Generate AWS infrastructure diagram -->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<installDirectory>target</installDirectory>
<nodeVersion>v16.13.1</nodeVersion>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>install cdk4j-diagram</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install @datapith/cdk4j-diagram</arguments>
</configuration>
</execution>
<execution>
<id>generate infrastructure diagram</id>
<goals>
<goal>npx</goal>
</goals>
<!-- Make sure the generation of infrastructure diagram happens after synth -->
<phase>deploy</phase>
<configuration>
<arguments>cdk4j-diagram html --ci-mode -t ${project.basedir}/target/cdk.out/GettingStartedStack.template.json -o ${project.basedir}/target/aws-diagram</arguments>
</configuration>
</execution>
</executions>
</plugin>Within this snippet we assure the proper node.js version is used (install node and npm) and we automatically install cdk4j-diagram (install cdk4j-diagram).
Get Mark Schenk’s stories in your inbox
Join Medium for free to get updates from this writer.
With the execution part generate infrastructure diagram we tell cdk4j-diagram to generate a diagram based on the CloudFormation template GettingStartedStack.template.json and persist it at /target/aws-diagram.
NOTE: CloudFormation template
GettingStartedStack.template.jsonwill be generated when the stack is synthesized by the CDK4J maven plugin.
Generate diagram
As CDK4J diagram is integrated within the maven build process, the diagram will be automatically generated when we deploy the stack by executing: mvn deploy
Along with deploying the stack to AWS (as shown in Get started with CDK4J, a visualization of the stack is saved in the target/aws-diagram`directory. Open the `index.html` file to view an interactive diagram similar to the image below:
The checkboxes in the blue sidebar allow you to hide specific resources, and clicking any resource reveals more detailed information.
Conclusion
Within this tutorial we have shown how we can automatically generate a diagram regarding the deployed stack. By this we
- Provide a clear and concise overview of resources, and their dependencies.
- Facilitate better communication among team members and external auditors by providing an easy-to-understand representation of the stack.
- Assure (new) employees or external contractors quickly understand the stack setup.







