![]() |
Diagrams is an open-source Python library that lets you create cloud infrastructure diagrams programmatically. Instead of manually drawing architectures on tools like Lucidchart, Draw.io, or Miro, with Diagrams you can write Python code to automatically generate clear, updatable, and versionable visual diagrams via Git.
π Official website: https://diagrams.mingrammer.com/
✅ Why use Diagrams
- Automatable: You can dynamically generate diagrams based on your environments.
- Versionable: Diagrams are created from code, making them perfect for Git.
- Multi-Cloud Support: AWS, Azure, GCP, Kubernetes, On-prem, SaaS, and more.
- Easy to Use: Readable and intuitive syntax.
- Perfect for DevOps/SRE/Cloud Architects.
π Installation
# Install Graphviz (Ubuntu/Debian)
sudo apt install graphviz
# Or on macOS
brew install graphviz
# Or on Windows by downloading the installer
https://graphviz.gitlab.io/download/
# Then install Diagrams
pip install diagrams
π ️ First example: Simple AWS diagram
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=True):
ELB("lb") >> EC2("web") >> RDS("userdb")
π This will generate a diagram with a Load Balancer connected to an EC2 instance, which is connected to an RDS database.
source https://diagrams.mingrammer.com/docs/getting-started/installation |
π Examples for Cloud Providers
π§± Clusters and Groups
from diagrams import Cluster, Diagram
from diagrams.k8s.compute import Pod
from diagrams.k8s.network import Input
from diagrams.k8s.group import Namespace
with Diagram("Kubernetes Namespace", show=False):
ns = Namespace("Prod")
with Cluster("Input Controller"):
ingress = Ingress("nginx")
with Cluster("Namespace: Prod"):
pod1 = Pod("backend")
pod2 = Pod("frontend")
input >> [pod1, pod2]
π‘ AWS Microservices
from diagrams import Cluster, Diagram
from diagrams.aws.compute import ECS
from diagrams.aws.network import ELB
from diagrams.aws.database import Dynamodb
from diagrams.aws.general import Client
with Diagram("AWS Microservices", show=False):
client = Client("user")
with Cluster("VPC"):
lb = ELB("load balancer")
with Cluster("ECS Cluster"):
service1 = ECS("service 1")
service2 = ECS("service 2")
db = Dynamodb("dynamo")
client >> lb >> [service1, service2]
[service1, service2] >> db
π΅ GCP Data Pipeline
from diagrams import Diagram
from diagrams.gcp.analytics import BigQuery
from diagrams.gcp.storage import GCS
from diagrams.gcp.compute import Functions
with Diagram("GCP Data Pipeline", show=False):
storage = GCS("Raw Data")
fn = Functions("Processing")
bq = BigQuery("Data Warehouse")
storage >> fn >> bq
π£ Azure Web App
from diagrams import Diagram
from diagrams.azure.compute import FunctionApps
from diagrams.azure.web import WebApps
from diagrams.azure.database import CosmosDb
with Diagram("Web App Azure", show=False):
web = WebApps("Web Frontend")
functions = FunctionApps("API")
db = CosmosDb("DB")
web >> functions >> db
π¨ Customization & Advanced Options
show=False
: Does not open automatically the image.direction="LR"
: Changes the direction of the diagram (LR, TB, RL, etc.).outformat="png"
: Specifies the output format.filename="output_name"
: Sets the file name.
with Diagram("Custom", direction="LR", outformat="svg", filename="custom", show=False):
...
π§© Extensive Provider Support
Diagrams supports a wide range of providers and services:
- AWS, Azure, GCP
- Kubernetes
- Alibaba Cloud
- On-prem (server, storage, router)
- SaaS (GitHub, Jenkins, Slack, etc.)
- Custom Icons (you can use custom images!)
See the complete list:
π https://diagrams.mingrammer.com/docs/nodes
✨ Example with icons personalized
from diagrams import Diagram, Node
class CustomNode(Node):
_provider = "custom"
_icon_dir = "./custom_icons"
fontcolor = "#ffffff"
def __init__(self, label: str, icon: str):
super().__init__(label, icon)
with Diagram("Custom Icons", show=False):
CustomNode("Tool", "mylogo.png")
π Automation
You can integrate Diagrams into:
- CI/CD script to generate updated diagrams
- Technical documentation automatic
- GitHub Actions Pipeline or Jenkins
π¦ Recommended Project Structure
infra-diagrams/
├── diagrams/
│ ├── aws_arch.py
│ ├── gcp_data_pipeline.py
├── custom_icons/
│ └── mylogo.png
├── README.md
π Conclusion
Diagrams is a powerful tool for those working with cloud infrastructures and wanting to keep documentation aligned, clear, and automatic. It's one of the most elegant solutions for bridging the gap between code and visual representation.
π Useful Resources
- π Official Documentation
- π GitHub Repo
- π¬ Discussions and issues
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment