How to Draw AWS diagrams with Python and the Diagrams Module
Evgenii Iatcenko
Product Owner | DevOps & Cloud | SAFe | Agile Leadership | 2x AWS, 3x Azure | Remote & UAE Relocation
In this article, I will explain how to use Python and the Diagrams module to create AWS architecture diagrams. Before we begin, make sure to install all the necessary software, including Python, the Diagrams module, and the Graphviz module, and add their paths to the PATH environment of your operating system.
For Windows:
1. Download Python from https://www.python.org/downloads/windows/.
2. To install pip, run the following commands:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
pip help
3. To install the Diagrams module, run the following command:
pip install diagrams
4. To install Graphviz, run the following command:
pip install graphviz
5. Add the paths to the Python scripts and site-packages to the PATH environment variable by following these steps:
C:\Users\Your_Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts
C:\Users\Your_Name\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\
Next, create a Python script and add the following code:
领英推荐
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", direction="TB"):
? ? ELB("lb") >> [EC2("worker1"),
? ? ? ? ? ? ? ? ? EC2("worker2"),
? ? ? ? ? ? ? ? ? EC2("worker3"),
? ? ? ? ? ? ? ? ? EC2("worker4"),
? ? ? ? ? ? ? ? ? EC2("worker5")] >> RDS("events")
Run the script and check that all modules work as expected. The diagram will appear in a separate window.
3-tier AWS VPC environment with EKS cluster, public and private node groups
Let's discuss how to draw the structure of a 3-tier AWS VPC environment with an EKS cluster and public and private node groups using Python. To begin, create a Python script and name it awsinfra.py.
The code is a Python script that uses the Diagrams module to create an AWS architecture diagram for an EKS environment. The script imports various components from the Diagrams module, such as clusters, edges, and AWS-related modules like compute, network, and security.
The main diagram is defined using the Diagram class with the title "AWS EKS environment", and the output format is set to PNG. The direction of the diagram is set to top-to-bottom (TB).
The code defines two VPC area, "VPC Cloud environment" and "VPC Amazon EKS", using the Cluster class. Within the "VPC Amazon EKS" cluster, there is an EKS control plane represented by the EKS class.
The script defines two availability zones within the "VPC Cloud environment" cluster, each with a public subnet and a private subnet. The NATGateway class represents the NAT gateway in the public subnet, and the VPCElasticNetworkInterface class represents the EKS elastic network interface. The EC2Instance class represents the EKS worker nodes, and the EC2Instance class with the label "Bastion EC2 Instance" represents the bastion host in the public subnet of the second availability zone. An InternetGateway class represents the internet gateway.
The script also defines User with the label "Admin", ECR for the Elastic Container Registry, IAM for identity management, and two IAMRole for the EKS cluster and EKS node groups.
Finally, the script connects the components using the Edge and the ">>" and "<<" operators. For example, the EKS control plane is connected to the NAT gateway using the ">>" operator, and the EKS worker nodes are connected to the NAT gateway using the "<<" operator. The Edge class allows for customization of the edges, such as setting the color, style, and label.
from diagrams import Cluster, Diagram, Edge
from diagrams.aws.compute import EKS, EC2Instance, ECR
from diagrams.aws.network import NATGateway, VPCElasticNetworkInterface, InternetGateway
from diagrams.aws.security import IAM, IAMRole
from diagrams.k8s.rbac import User
with Diagram("AWS EKS environment", outformat="png", direction="TB"):
? with Cluster("AWS Cloud"):
? ? with Cluster("VPC Amazon EKS"):
? ? ? ?ekscp = EKS("EKS Control plane")
? ? with Cluster("VPC Cloud environment"):
? ? ? with Cluster("Availability Zone 1"):
? ? ? ? with Cluster("Public Subnet"):
? ? ? ? ? ?nat = NATGateway("NAT GATEWAY")
? ? ? ? ? ?eniavz1 = VPCElasticNetworkInterface("EKS Elastic Network Interface")
? ? ? ? ? ?ec2pubavz1 = EC2Instance("EKS Worker Node")
? ? ? ? with Cluster("Private Subnet"):
? ? ? ? ? ?ec2privavz1 = EC2Instance("EKS Worker Node")
? ?
? ? ? with Cluster("Availability Zone 2"):
? ? ? ? with Cluster("Public Subnet"):
? ? ? ? ? ?ec2bastpubavz2 = EC2Instance("Bastion EC2 Instance")
? ? ? ? ? ?eniavz2 = VPCElasticNetworkInterface("EKS Elastic Network Interface")
? ? ? ? ? ?ec2pubavz2 = EC2Instance("EKS Worker Node")
? ? ? ? with Cluster("Private Subnet"):
? ? ? ? ? ?ec2privavz2 = EC2Instance("EKS Worker Node")
? ? ? InternetGateway("Internet Gateway")
? ? usr = User("Admin")
? ? ECR("Elastic Container Registry")
? ? iamserv = IAM("Identity Management")
? ? additroles = [IAMRole("Role for EKS Cluster"), IAMRole("Role for EKS Node Groups")]
? ? eniavz1 - Edge(color="blue", style="dashed", label="EKS Cluster SG") - eniavz2
? ? pubngsg = ec2pubavz1 - Edge(color="orange", style="dashed", label="Public Node Group SG") - ec2pubavz2
? ? ec2privavz1 - Edge(color="orange", style="dashed", label="Private NG SG") - ec2privavz2
? ? ec2privavz1 >> nat << ec2privavz2
? ? nat >> ekscp
? ? eniavz1 >> ekscp << eniavz2
? ? ec2pubavz1 >> ekscp << ec2pubavz2
? ? usr >> ec2bastpubavz2
? ? iamserv >> additroles
In my opinion, while the method described for drawing AWS diagrams using Python and the Diagrams module may be useful, it may not be the most user-friendly or flexible approach. I think that the best way to evaluate any tool is to use it as much as possible, especially when working on real-world tasks. If I needed to create AWS infrastructure diagrams to present to a customer or include in documentation, I would consider other options that may be more user-friendly and offer greater flexibility.
BMO | Data Engineer | LLM | Generative AI
4 个月winget install graphviz and adding graphviz\bin in environment path worked!
BMO | Data Engineer | LLM | Generative AI
4 个月I have already added \Python39\Scripts and Python39\site-packages\ in environmental variable
BMO | Data Engineer | LLM | Generative AI
4 个月?raise ExecutableNotFound(cmd) from e graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH
BMO | Data Engineer | LLM | Generative AI
4 个月The above exception was the direct cause of the following exception: Traceback (most recent call last): ?File "C:\Users\srajp04\GenAI\DevOps\create_diagram.py", line 7, in <module> ??with Diagram("Grouped Workers", direction="TB"): ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\diagrams\__init__.py", line 164, in __exit__ ??self.render() ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\diagrams\__init__.py", line 198, in render ??self.dot.render(format=self.outformat, view=self.show, quiet=True) ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\_tools.py", line 171, in wrapper ??return func(*args, **kwargs) ??????^^^^^^^^^^^^^^^^^^^^^ ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\rendering.py", line 122, in render ??rendered = self._render(*args, **kwargs) ????????^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\_tools.py", line 171, in wrapper ??return func(*args, **kwargs) ??????^^^^^^^^^^^^^^^^^^^^^ ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\backend\rendering.py", line 326, in render ??execute.run_check(cmd, ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\backend\execute.py", line 81, in run_check ?
BMO | Data Engineer | LLM | Generative AI
4 个月Hi, when i am running the code, it is giving me error FileNotFoundError: [WinError 2] The system cannot find the file specified. The trace back is ........................... Traceback (most recent call last): ?File "C:\Users\srajp04\GenAI\venv\Lib\site-packages\graphviz\backend\execute.py", line 78, in run_check ??proc = subprocess.run(cmd, **kwargs) ??????^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?File "C:\Program Files\Python\Python39\Lib\subprocess.py", line 548, in run ??with Popen(*popenargs, **kwargs) as process: ?????^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?File "C:\Program Files\Python\Python39\Lib\subprocess.py", line 1026, in __init__ ??self._execute_child(args, executable, preexec_fn, close_fds, ?File "C:\Program Files\Python\Python39\Lib\subprocess.py", line 1538, in _execute_child ??hp, ht, pid, tid = _winapi.CreateProcess(executable, args, ????????????^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [WinError 2] The system cannot find the file specified