Architecture Diagrams using Python

Rishi Raj Singh
FAUN — Developer Community 🐾
4 min readMar 16, 2021

--

Creating architecture diagrams is always a challenge due to all pasting images, finding the correct icon, and more so when you must update some part of it. Aligning arrows or the diagram can be a daunting and time-consuming task. I found an interesting library Diagram, which as the name suggests assists in creating diagrams.

Diagrams let you draw the cloud system architecture in Python code. It was born for prototyping a new system architecture design without any design tools. You can also describe or visualize the existing system architecture as well. Diagrams currently supports main major providers including AWS, Azure, GCP, Kubernetes, Alibaba Cloud, Oracle Cloud etc… It also supports On-Premise nodes, SaaS, and major Programming frameworks and languages.

Getting Started

Python 3.6 is needed, once installed setup GraphViz as this is what renders the diagrams. The Github repository actually has a pretty decent “Getting Started” section as well so if you need help installing anything feel free to refer to that here.

pip install diagrams

Diagrams — A diagram is a primary object representing a diagram.

Nodes — An abstract concept that represents a single system component.

Clusters — This allows you to organize the nodes into groups (or clusters) instead of isolated components.

Edges — Represents a linkage between Nodes.

The example diagram that we will build is going to be a simple load-balanced website on AWS that uses a PostgreSQL database and a Redis cache.

from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.database import PostgreSQL # Would typically use RDS from aws.database
from diagrams.onprem.inmemory import Redis # Would typically use ElastiCache from aws.database

with Diagram("Simple Website Diagram") as diag:
dns = Route53("dns")
load_balancer = ELB("Load Balancer")
database = PostgreSQL("User Database")
cache = Redis("Cache")
svc_group = [EC2("Webserver 1"),
EC2("Webserver 2"),
EC2("Webserver 3")]
diag # This will illustrate the diagram if you are using a Google Colab or Jypiter notebook.

This will generate the below diagram:

Now we have all the components that are needed, we will next link them all together. With this example, we will just link the nodes without labels, but if you take a look at the documentation applying labels to your links is a pretty easy task.

from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.database import PostgreSQL # Would typically use RDS from aws.database
from diagrams.onprem.inmemory import Redis # Would typically use ElastiCache from aws.database

with Diagram("Simple Website Diagram", direction='LR') as diag: # It's LR by default, but you have a few options with the orientation
dns = Route53("dns")
load_balancer = ELB("Load Balancer")
database = PostgreSQL("User Database")
cache = Redis("Cache")
with Cluster("Webserver Cluster"):
svc_group = [EC2("Webserver 1"),
EC2("Webserver 2"),
EC2("Webserver 3")]
dns >> load_balancer >> svc_group
svc_group >> cache
svc_group >> database
diag # This will illustrate the diagram if you are using a Google Colab or Jypiter notebook.

This will now show a logical flow between each component in the diagram. This flow can be reversed by changing the order in which you define the nodes. In addition to tweaking the flow, you can change a number of things as an edge object contains three attributes: label, color, and style.

Conclusion

This was just to get started on building diagrams with code, there is a lot of potential with the library which can help in automating diagram creation. Happy Diagramming.

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--