Cloud Native 9 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automate Cloud Architecture Diagrams with Python’s Diagrams Library

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 notebooks

The empty diagram appears as shown below.

Empty diagram
Empty diagram

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")]
    diag

The diagram now shows all nodes:

Diagram with nodes
Diagram with 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:

Diagram with clusters
Diagram with clusters

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
    diag

The final diagram shows the logical flow between components:

Final diagram
Final diagram

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.

ArchitecturePythonAutomationcloudGraphvizDiagrams
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.