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.
You can install it with the following command.
#Installation with pip
$ pip install diagrams
#Installation in pipenv environment
$ pipenv install diagrams
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.
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
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
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
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
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
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
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?
Recommended Posts