Skip to main content

Command Palette

Search for a command to run...

Architecture Diagrams as Code (DAC)

Updated
4 min read
Architecture Diagrams as Code (DAC)
R

An experienced DevOps Engineer understands the integration of operations and development in order to deliver code to customers quickly. Has Cloud and monitoring process experience, as well as DevOps development in Windows, Mac, and Linux systems.

Introduction

Traditional methods of creating architecture diagrams often involve manual drafting, which can be time-consuming, error-prone, and difficult to maintain. In recent years, there has been a growing trend towards automating this process through "Diagram as Code" (DaC). This approach involves using code to define and generate architectural diagrams, offering several advantages over manual methods.

What is Diagram as Code?

Diagram as Code is a methodology where architectural diagrams are defined using code rather than graphical tools. This approach leverages the power of programming languages to create, modify, and version control diagrams, ensuring consistency and accuracy.

Diagram as Code in DevOps & Cloud Engineering

In the dynamic world of contemporary DevOps and cloud engineering, Diagram as Code (DaC) enhances the upkeep, scalability, and cooperative efforts surrounding system documentation and architecture. This makes it an indispensable method for the current landscape of software development and infrastructure management.

Benefits of Diagram as Code

  • Efficiency: Code-based diagrams can be generated quickly and easily, saving time and effort.

  • Accuracy: By using code, the risk of human error is reduced, leading to more accurate diagrams.

  • Consistency: Diagrams can be kept consistent across different teams and projects.

  • Version Control: Changes to diagrams can be tracked and managed using version control systems.

  • Automation: Diagrams can be automatically generated based on code changes, reducing manual effort.

  • Collaboration: Multiple team members can contribute to diagram creation and modification.

Quick start with Diagrams

We’ll use Python to create architecture diagrams. Being able to codify diagrams is a huge asset, and could potentially save you and your team long hours. To get started all you need is the diagrams library, and to use this diagrams library, you need the following:

# using pip (pip3)
$ pip install diagrams

# using pipenv
$ pipenv install diagrams

# using poetry
$ poetry add diagrams

Let’s create a three-tier diagram on AWS

The script below generates a three-tier architecture diagram with Users, Route53, an Elastic Load Balancer (ELB), an AutoScaling group (ASG) with three EC2 instances, and a Database Tier with ElastiCache and RDS. The edges between the nodes represent connections, with color and label attributes to indicate the type of connection. Note comments are provided for readability.

# three_tier.py

# Import necessary modules from the diagrams library
from diagrams import Cluster, Diagram, Edge
from diagrams.aws.network import Route53, ELB
from diagrams.aws.compute import EC2, AutoScaling
from diagrams.aws.database import RDS, ElastiCache
from diagrams.onprem.client import Users

# Create a new Diagram object
with Diagram("Stateful Web Application", show=False, graph_attr={"splines": "ortho", "fontsize": "20"}):
    # Define nodes for Users, Route53, and ELB
    users = Users("Users")
    route53 = Route53("Route53")
    elb = ELB("ELB")

    # Create a cluster for the Web Tier
    with Cluster("Web Tier", graph_attr={"fontsize": "20"}):

        # Define an cluster Autoscaling group node
        asg = AutoScaling("ASG")
        # Create a separate cluster for each EC2 instance
        with Cluster("Availability Zone 1"):
            ec2_instance1 = EC2("EC2")
        with Cluster("Availability Zone 2"):
            ec2_instance2 = EC2("EC2")
        with Cluster("Availability Zone 3"):
            ec2_instance3 = EC2("EC2")
        # Connect the AutoScaling group to each EC2 instance
        asg - [ec2_instance1, ec2_instance2, ec2_instance3]

    # Create a cluster for the Database Tier
    with Cluster("Database Tier", graph_attr={"fontsize": "20"}):
        # Define nodes for ElastiCache and RDS
        elasticache = ElastiCache("ElastiCache")
        rds = RDS("RDS")


    # Define edges between nodes
    users >> Edge(color="blue")\
    << route53
    users >> Edge(color="green", label="HTTP/HTTPS to 0.0.0.0/0")\
    >> elb >> Edge(color="black") << asg
    ec2_instance1 >> Edge(color="purple", label="Read/Write")\
    << elasticache
    ec2_instance2 >> Edge(color="red", label="Read/Write")\
    << rds

Run the script using Python:

python three_tier.py

Conclusion

Diagram as Code is a powerful approach to creating and managing architectural diagrams. By leveraging the benefits of code-based generation, teams can improve efficiency, accuracy, and collaboration. While there are challenges to consider, the overall advantages make it a compelling option for modern software development.

References

To further learn and explore diagram as code, you can refer to these valuable resources:

Diagrams documentation

Code2Diagram

ByteByteGo

medium.com

dev.to

Thanks for reading! I hope you found the article helpful, leave a few claps!👏 to recommend this post so that others can see and benefit as well.

I look forward to connecting with you on LinkedIn

More from this blog

DevOps Simplified

39 posts

An experienced DevOps Engineer understands the integration of operations and development in order to deliver code to customers quickly. Has Cloud and monitoring process experience, as well as DevOps