[PYTHON] I tried Diagram as Code (Diagrams) which can manage the configuration diagram code

Try Diagram as Code (Diagrams)

https://diagrams.mingrammer.com/

I usually use authoring tools such as Cacoo for configuration diagrams, I tried to see how much I could write using a tool that can draw a configuration diagram with Python code.

Conclusion, impression

――Since it can be written simply, it is good to be able to quickly prototype a new system as it is touted. ――The learning cost is low, and you can start writing as soon as you are familiar with Python. --There was a font size change attribute, but I didn't know how to write annotations.

About the environment

Operating requirements

--Python 3.6 and above --Requires Graphviz

Advance preparation

Create a new virtual environment and install packages in it. This procedure is optional.

python -m venv ~/envs/diagrams
source ~/envs/diagrams/bin/activate

Installation

https://diagrams.mingrammer.com/docs/getting-started/installation

#Install with Homebrew
brew install graphviz
#Install with pip
pip install diagrams

example

For the time being, I tried to output using an example.

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

with Diagram("Web Service", show=False):
    ELB("lb") >> EC2("web") >> RDS("db")
python diagram.py
2020-05-29 13:50:05.901 +[__NSCFConstantString length]: unrecognized selector sent to class 0x
2020-05-29 13:50:05.905 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[__NSCFConstantString length]: unrecognized selector sent to class 0x'

Immediately, I encountered an error. I didn't know the cause at all, so I decided to upgrade graphviz, which was originally installed, while catching issues.

brew upgrade graphviz

You have successfully upgraded the version to 2.40.1-> 2.44.0.

python diagram.py

I was able to output safely.

Trial configuration

from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2, ElasticBeanstalk
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB, Route53
from diagrams.onprem.client import Client

graph_attr = {
}

def draw():
    with Diagram("web_service", show=False, graph_attr=graph_attr, direction="TB"):
        route53 = Route53('route53')
        client = Client('client')

        client >> route53

        with Cluster("ElasticBeanstalk"):
            with Cluster("WEB"):

                with Cluster("Subnet1"):
                    web1 = EC2("web")
                    lb1 = ELB("lb1")
                    lb1 >> web1

                with Cluster("Subnet2"):
                    web2 = EC2("web")
                    lb2 = ELB("lb2")
                    lb2 >> web2

                route53 - [lb1, lb2]

            with Cluster("DB"):
                db_master = RDS("master")

                with Cluster("Subnet3"):
                    rds1 = RDS("slave1")

                with Cluster("Subnet4"):
                    rds2 = RDS("slave2")

                db_master - [rds1, rds2]

                [web1, web2] >> db_master

if __name__ == '__main__':
    draw()

Output image

web_service.png

Recommended Posts

I tried Diagram as Code (Diagrams) which can manage the configuration diagram code
I tried to draw a configuration diagram using Diagrams
I tried "Streamlit" which turns the Python code into a web application as it is
I tried to draw a system configuration diagram with Diagrams on Docker
I tried using "Streamlit" which can do the Web only with Python
I tried "Lobe" which can easily train the machine learning model published by Microsoft.
I tried running the sample code of the Ansible module
I tried to introduce the block diagram generation tool blockdiag
I tried porting the code written for TensorFlow to Theano
I tried to summarize the code often used in Pandas
I tried the changefinder library!
[Flask] I tried to summarize the "docker-compose configuration" that can be created quickly for web applications
I tried using "Asciichart Py" which can draw a beautiful graph on the console with Python.
[Updated as appropriate] I tried to organize the basic visualization methods
I tried using the COTOHA API (there is code on GitHub)
Continuation: I tried to introduce the block diagram generation tool blockdiag
The diagrams were interesting so I tried wrapping them with flask
From "drawing" to "writing" the configuration diagram: Try drawing the AWS configuration diagram with Diagrams
I tried the TensorFlow tutorial 1st
I tried the Naruro novel API
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to get the authentication code of Qiita API with Python.