avatarPark Sehun

Summary

Diagram as Code is a method for managing architecture diagrams as version-controlled code, offering benefits such as collaboration, reproducibility, automation, and extensibility.

Abstract

Diagram as Code is an innovative approach that treats architecture diagrams as code, allowing for version control, collaboration, and review within teams. This method ensures that diagrams can be consistently reproduced and maintained alongside the codebase, simplifying the tracking of architectural changes. It also enables the integration of diagram creation with CI/CD tools, automating the review and approval processes. By leveraging programming languages, diagram creation becomes more efficient, with the ability to reuse code snippets, functions, and libraries, facilitating the generation of complex diagrams. The Python library 'Diagrams' exemplifies this concept, providing a simple syntax to programmatically create and render various types of diagrams, including multi-cloud architecture diagrams that incorporate components from AWS, GCP, and on-premises infrastructure.

Opinions

  • Diagram as Code is considered superior to traditional methods such as maintaining diagrams in PNG or Visio formats, as it ensures consistency and reproducibility.
  • The integration of Di

Diagram as Code

Diagram as code allows you to track the architecture diagram changes in any version control system. Furthermore, using the code to maintain the diagrams has more benefits.

Version control and collaboration

Diagrams created as code can be stored in a version control system like Git, allowing teams to track changes, collaborate, and review diagrams. This will enable the multiple teams to review and follow the architectural changes together.

Reproducibility

Did you ever ask the team “whether they have original files of PNG like VISIO or Drawio file”? Regardless of the size of the company, you will be able to find many pieces of PNG or PowerPoint files when you refer to the diagram. With Diagram-as-code, you can generate diagrams from code, ensuring consistency and reproducibility.

Automation and Integration

There are some visualization tools of Infrastructure-as-code however, I didn’t hear any diagram-as-code is directly integrated with IaaC like Terraform. Then you can ask ‘Why are you only using IaaC and use the visualisation tool to make the diagram’? For example, there is Driftctl.

However, many visualisation tools of IaaC are neither easy-to-read nor used for the architectural view because IaaC tells about the relationships between resources and dependencies during the provisioning rather than the high-level views of the architecture.

If you use the code instead of the drawing tools, you can integrate with CICD tools and automate the process of reviewing and approval.

Code reuse and extensibility

You may have experience using the old VISIO files to create the new diagram for the new applications. Diagram-as-code can leverage the power of programming languages. You can reuse the code snippets, functions and libraries. This facilitates the creation of complex and dynamic diagrams.

Example: Diagrams

Diagrams is a Python library that allows you to create diagrams programmatically. It provides a simple and intuitive syntax for defining and rendering various types of diagrams, including architecture diagrams, network diagrams, flowcharts, and more.

Diagrams (Diagram as Code) https://diagrams.mingrammer.com/

Sample Code: create a multi-cloud architecture diagram that includes components from AWS, GCP, and on-premises infrastructure.

from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB
from diagrams.aws.storage import S3
from diagrams.gcp.compute import KubernetesEngine
from diagrams.gcp.database import SQL
from diagrams.gcp.network import LoadBalancing
from diagrams.onprem.analytics import Spark
from diagrams.onprem.compute import Server
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.network import Internet

with Diagram("Multi-Cloud Architecture", show=False):
    with Cluster("AWS"):
        lb = ELB("Load Balancer")
        web = EC2("Web Server")
        db = RDS("Database")
        storage = S3("Object Storage")

        lb >> web >> db
        web >> storage

    with Cluster("GCP"):
        gcp_lb = LoadBalancing("Load Balancer")
        gke = KubernetesEngine("Kubernetes Engine")
        gcp_db = SQL("Cloud SQL")

        gcp_lb >> gke >> gcp_db

    with Cluster("On-Premises"):
        spark = Spark("Data Analytics")
        server = Server("On-Prem Server")
        onprem_db = PostgreSQL("On-Prem DB")

        server >> spark
        server >> onprem_db

    internet = Internet("Internet")

    lb >> internet
    gcp_lb >> internet
    internet >> onprem_db
Diagrams
Python
Diagramascode
Recommended from ReadMedium