Diagram as Code Example


I came cross a nice Diagram as Code tool, it allows me to draw Cloud system architecture in Python code.

To use it, you need to install Graphviz and diagrams python module.

brew install graphviz
pip install diagrams

Here is a sample code of using the tool to create the diagram of AWS transit gateway network architecture.

"""
Diagram as code sample – TGW network
"""
from diagrams import Diagram, Edge
from diagrams.aws.network import (
TransitGateway,
VPC,
VPCCustomerGateway,
SiteToSiteVpn,
DirectConnect,
VPCCustomerGateway,
)
from diagrams.aws.general import OfficeBuilding
with Diagram("AWS TGW Network Infrastructure", show=False):
tgw = TransitGateway("tgw")
VPC("workload vpc 01") – tgw
VPC("workload vpc 02") – tgw
VPC("workload vpc 03") – tgw
(
tgw
– Edge(label="inter region peering")
– TransitGateway("tgw")
– VPC("workload vpc 04")
)
tgw – VPC("shared service vpc")
tgw – DirectConnect("direct connect") – OfficeBuilding("on-premise")
tgw – SiteToSiteVpn("vpn connection") – VPCCustomerGateway("customer gateway")
view raw tgw-diagram.py hosted with ❤ by GitHub

Run the script python tgw-diagram.py, then you will see the nice diagram 😉

Leave a comment