Automate Cloud Architecture Diagrams with Python’s Diagrams Library
Learn how to install GraphViz and the Python Diagrams library, create and organize cloud components like AWS services, on‑prem databases, and clusters, and generate clear architecture diagrams programmatically, with step‑by‑step code examples for building a simple load‑balanced website diagram.
Background
Earlier I discovered the Python library diagrams , which can generate architecture diagrams directly from code, avoiding the tedious manual alignment of images in tools like draw.io.
Getting Started
The first requirement is Python 3.6+ and GraphViz. After installing the diagrams package with your preferred Python package manager, you can start creating diagrams.
from diagrams import Diagram
with Diagram("Simple Website Diagram") as diag:
pass # Shows an empty diagram in notebooksThe empty diagram appears as shown below.
Component Types
The library provides components for many providers. The most relevant for typical use cases are:
AWS / GCP / Azure – official cloud service assets.
Generic and On‑Prem – for cloud‑agnostic components.
Frameworks – to illustrate programming language elements.
SaaS – for services like Slack.
Diagram Concepts
Graph – the main object representing the diagram. Node – an abstract representation of a system component. Cluster – groups nodes together for readability. Edge – connects nodes.
Creating Your First Diagram
Step 1: Create a Workspace
from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB, Route53
from diagrams.onprem.database import PostgreSQL
from diagrams.onprem.inmemory import Redis
with Diagram("Simple Website Diagram") as diag:
dns = Route53("dns")
load_balancer = ELB("Load Balancer")
database = PostgreSQL("User Database")
cache = Redis("Cache")
svc_group = [EC2("Webserver 1"), EC2("Webserver 2"), EC2("Webserver 3")]
diagThe diagram now shows all nodes:
Step 2: Add Nodes
We add AWS and on‑prem nodes for a load‑balanced website, using Route53, ELB, EC2 instances, PostgreSQL, and Redis.
Step 3: Group Nodes (Optional)
with Cluster("Webserver Cluster"):
svc_group = [EC2("Webserver 1"), EC2("Webserver 2"), EC2("Webserver 3")]Grouping improves readability:
Step 4: Connect Everything
with Diagram("Simple Website Diagram", direction='LR') as diag:
dns = Route53("dns")
load_balancer = ELB("Load Balancer")
database = PostgreSQL("User Database")
cache = Redis("Cache")
with Cluster("Webserver Cluster"):
svc_group = [EC2("Webserver 1"), EC2("Webserver 2"), EC2("Webserver 3")]
dns >> load_balancer >> svc_group
svc_group >> cache
svc_group >> database
diagThe final diagram shows the logical flow between components:
Conclusion
Using the diagrams library lets you generate clear, maintainable architecture diagrams programmatically, saving time on manual adjustments and making it easy to update designs as your infrastructure evolves.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
