Draw your diagram with python

Thao Luong
5 min readAug 16, 2020

--

Document maintenance is not easy part when we works on the project and operation. Drawing a topology for the infra setup is not a “sweet task” for the people who currently work on that. It’s quite boring and some time the system admin missed to update the topology when they changed some thing on the infra. It’s can come from various reason but how we made it become sweet task and all people during the team can contribute to update the topology when they change some thing on the system.

The traditional way to draw a topology is use visio (licenses) or draw.io. People familiar with using the draw tool and save the file .drawio for update when they has changed the infra setup. It can be good but we will face the version maintence if you work with a lot of team and the infra change will be happen frequently.

Some time i’m asking my self about should we have another way to help system admin draw a topology more easy ?

Today i will come with some tool can genarate diagram as a code:D which can help to draw the topology with some line of python code and we can store it on git for the other people on the team can update that by commit to this file.

Let’s start to see what is this and how it’s make will make the system admin life become so easy during the daily task.

What is the diagram as the code?

Diagrams lets 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.

There is 4 object we need to understand on diagram is:

Diagram: is a primary object representing a diagram

Nodes: Represent object or system compoment

Cluster: allow you group of nodes on the isolate group

Edge: is an object representing a connection between Nodes with some additional properties.

How we use the diagram as the code:

The install instruction below :

  • Install Graphviz as this will use to render the image. For window, please download packet from this url and add the “C:\Program Files (x86)\Graphviz2.38\bin” on your path enviroment.
  • Install the diagrams lib by pip:

pip install diagrams

Let’s start to deep dive to create my webapp topology on AWS.

My webapp topology has some compoment below:

1 CDN Cloudfare: use to caching the static content on webserver.

2 Load balancer on AWS: one to load balace between 2 webserver and one to load balancer between 3 application server.

2 webserver: get the request and proxy to the application server.

3 Application: handle request from web and connect to the redis to get some infomation and update on DB.

1 Redis: memcached storaged

2 Server Databases: Storage the data of application ( replicate master — master)

The workflow is user -> CDN -> LB ->web -> LB -> app -> DB.

Let’s go to build this topology by the diagram python code.

First thing, we will create a diagram object name Topology. Diagram with need some parameter (name of topology, show and graph_attr). The value show=True is will show the topology when we run the code. On the same way, it will create the file png on the current directory.

from diagrams import Cluster, Diagram, Edgefrom diagrams.onprem.client import Client, Usersfrom diagrams.aws.compute import EC2from diagrams.aws.database import ElastiCache, RDSfrom diagrams.aws.network import ELB, CFfrom diagrams.onprem.network import Pfsensefrom diagrams.onprem.compute import Serverfrom diagrams.saas.cdn import Cloudflaregraph_attr = {"fontsize": "45","bgcolor": "transparent"}with Diagram("Topology ", show=True, graph_attr=graph_attr):

Second, we will create node on diagram base on our topo. We will need to create a cluster AWS which contain a lot of nodes below:

CDN service: Cloudfare CDN service

2 ELB node (AWS ELB service).

Group node for web, app and databases(we have 2 EC2 web ,3 EC2 app and 2 EC2 DB so we group it by the function web services ,app services ,DB cluster) .

Redis node(Elaticache redis AWS service).

Until here , we already has one diagram with a lot of node inside.

Finally, we will connected all node together follow the workflow of the topology. it’s quite simple with this data flow below:

# dataflowusers >> cdn >> lb >> svc_web >> lb2 >> svc_app >> db_mastersvc_app >> redis
Code create node+dataflow

Ta da, we finish the topo and run that to see the result.

python.exe aws.py
AWS topology example

Options: I’m adding another on prem block with conect to the AWS topology

References:

Find me at Thao Vinh LUONG — Senior Devop Engineer.

--

--