[Python] Create an infrastructure diagram in 3 minutes using diagrams

Introduction

image.png

Quoted from the official diagrams

The above cloud system architecture can be drawn by using a library called diagrams of python. The systems that can be used with diagrams are AWS, Azure, GCP, Kubernetes, Alibaba Cloud, Oracle Cloud, 6 major providers, as well as various programming languages ​​and frameworks. This time I would like to write down the basic implementation method as a memo.

Installation

You can install it with the following command.

#Installation with pip
$ pip install diagrams

#Installation in pipenv environment
$ pipenv install diagrams

How to use

image.png

diagrams consist of the following four elements.

・ Diagrams ・ Nodes ・ Clusters ・ Edges

Diagrams

Diagrams are like canvases for infrastructure diagrams.

Node is one architecture as above, and you can create a basic infrastructure configuration diagram by installing Node on Diagrams.

Below is the simplest configuration diagram that is officially used.

#Diagrams import
from diagrams import Diagrams
#Node installation(This time EC2)
from diagrams.aws.compute import EC2

with Diagram("Simple Diagram") as diag:
    EC2("web")
diag

With this code, the following infrastructure diagram is created. Also, when you run the code, the infrastructure diagram will be downloaded to the directory where you are using jupyter notebook. image.png

Commentary

The basic grammar is written as follows.

with Diagram("The name you want to give to the canvas",
             outformat="(png, jpg, svg, and pdf)Select from",
             filename="The name you want to give as the file name",
            show=False,
            direction=(TB, BT, LR and RL)Select from):
Write an infrastructure diagram

outformat, filename, show can be omitted.

outformat: Select file format from png, jpg, svg, and pdf filename: Enter the name of the file to download in the directory show: Not displayed outside of python direction: You can select the direction of progress of the infrastructure configuration diagram. TB: top2bottom, BT: bottom2top, LR: left2right, RL: right2left

Nodes image.png Node is an icon like the one above. Node objects can be imported by provider.resource type.name. Example: aws-> provider compute-> resource type name-> EC2 import diagrams.aws.compute.EC2 You can check what kind of Node there is from the official page.

An example of an infrastructure diagram using Node is shown below.

from diagrams import 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

with Diagram("Web Services", show=False) as di:
    (ELB("lb") >> EC2("web")) - EC2("web") >> RDS("userdb")
di

image.png

Commentary

You can create one Node icon with Node ("name you want to give"). In addition, the following two symbols are mainly used to connect Nodes.

>> : >> Connect with arrows in the direction facing -: Show direct connection

If you want to combine from one Node to multiple Nodes, put the Nodes you want to combine in a list as follows.

from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

from diagrams import Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

with Diagram("Grouped Workers", show=False, direction="TB") as dd:
    #Collect in list type
    ELB("lb") >> [EC2("worker1"),
                  EC2("worker2"),
                  EC2("worker3"),
                  EC2("worker4"),
                  EC2("worker5")] >> RDS("events")
dd

image.png

Cluster By using Cluster, you can group some Nodes you want to put together. An example is shown below.

from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.database import RDS
from diagrams.aws.network import Route53

with Diagram("Simple Web Service with DB Cluster", show=False) as dino:
    dns = Route53("dns")
    web = ECS("service")

    with Cluster("DB Cluster"):
        db_master = RDS("master")
        db_master - [RDS("slave1"),
                     RDS("slave2")]

    dns >> web >> db_master
dino

image.png

Commentary

Summarize the parts you want to summarize in Cluster as follows.

with Cluster("Cluster name"):
Write the configuration of the Cluster

You can also write a Cluster inside the Cluster and nest it.

Edges Edges can have an effect on joining Nodes together.

from diagrams import 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

with Diagram("Web Services", show=False) as di:
    (ELB("lb") >>Edge(color="red", style="dotted",label="collect")>> EC2("web")) -Edge(color="brown", style="dashed")- EC2("web") >> RDS("userdb")
di

image.png

Commentary

Edges can be placed between the Node join symbols (-or >>).

#-Example put in
Node - Edge(color="Bond color", style="Join style",label="Label name you want to attach to the join") - Node

#>>Also-the same as
Node >> Edge(color="Bond color", style="Join style",label="Label name you want to attach to the join") >> Node

At the end

It turned out that it is easy to create an infrastructure configuration diagram by using Python. Why don't you make an infrastructure diagram after practicing Python?

References

Diagrams Official

Recommended Posts

[Python] Create an infrastructure diagram in 3 minutes using diagrams
Build and try an OpenCV & Python environment in minutes using Docker
[Hyperledger Iroha] Create an account using Python library
Create a GIF file using Pillow in Python
Create an image with characters in python (Japanese)
Create a MIDI file in Python using pretty_midi
Solve simultaneous equations in an instant using Python
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create SpatiaLite in Python
Create an elliptical scatter plot in Python without using a multivariate normal distribution
Create a data collection bot in Python using Selenium
Create an image file using PIL (Python Imaging Library).
Create your first GDSII file in Python using gdspy
Create a function in Python
Create a dictionary in Python
Create JIRA tickets using Python
Translate using googletrans in Python
Using Python mode in Processing
[Python] Create an event-driven web crawler using AWS's serverless architecture
Create an easy-to-use follow model in Django using ManyToManyField through
How to create an instance of a particular class from dict using __new__ () in python
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
GUI programming in Python using Appjar
Precautions when using pit in Python
Create a python GUI using tkinter
Write an HTTP / 2 server in Python
Try using LevelDB in Python (plyvel)
Create an Excel file with Python3
Create a binary file in Python
How to create a heatmap with an arbitrary domain in Python
Create Gmail in Python without API
Using global variables in python functions
Create Python project documentation in Sphinx
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Various ways to create an array of numbers from 1 to 10 in Python.
Python in is also an operator
Draw a CNN diagram in Python
Deploy Django in 3 minutes using docker-compose
Handwriting recognition using KNN in Python
Create a random string in Python
Try using Leap Motion in Python
Depth-first search using stack in Python
An alternative to `pause` in Python
When using regular expressions in Python
Easily create homemade RPA using Python
Create and read messagepacks in Python
GUI creation in python using tkinter 2
Build an application with Clean Architecture while using DI + mock in Python
Mouse operation using Windows API in Python
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
Create ScriptableObject in Python when building ADX2
[LLDB] Create your own command in Python
CSS environment created in 10 minutes using Django