Cloud architecture
AWS, Google Cloud and Kubernetes diagrams whose zones nest themselves.
Use it for: design reviews, onboarding docs, the "how it works" slide.
- Start with the systems and the arrows between them. Leave out the groups.
- Add icons:
icon="aws:s3",icon="gcp:cloud-run",icon="k8s:pod"(finding icons). - Wrap boxes in
with d.group(...)blocks, from the outside in: account, region, VPC, zone, subnet. - Number the request path with
step=and addd.add_legend().
Start small
"""Automatic layout: no coordinates. Run `python examples/quickstart.py`, or
`spinagram render examples/quickstart.py -f svg,png,pptx -o out/`."""
from spinagram import Diagram
d = Diagram("Q01", "Web app on AWS", subtitle="No coordinates, laid out automatically", edge_route="straight")
users = d.box("Users", icon="aws:users")
with d.group("us-east-1", style="aws-region", direction="right"):
with d.group("VPC", style="aws-vpc", direction="right"):
lb = d.box("Load balancer", icon="aws:alb")
api = d.box("API", text="FastAPI on ECS", icon="terrastruct:python")
with d.group(): # no style: a light "soft" zone
db = d.box("Orders DB", text="PostgreSQL", icon="terrastruct:postgres")
cache = d.box("Cache", text="Redis", icon="terrastruct:redis")
s3 = d.box("Uploads", text="S3 bucket", icon="aws:s3")
users.to(lb, "HTTPS") >> api
api.to(db, "SQL")
api >> [cache, s3]
if __name__ == "__main__":
d.save("out/", formats=["svg", "png", "pptx"])Three tiers across two zones
Subnets per tier in two Availability Zones, built with plain Python loops, with numbered steps and a legend.
"""AWS three-tier web application, after the AWS reference architecture: an automatic legend from the CSS
classes and numbered steps along the request path."""
from spinagram import Diagram
N = {"w": 196, "h": 80} # compact cards: diagram, legend and steps share one slide
d = Diagram("AWS01", "Three-tier web application on AWS",
subtitle="Edge, web, app and data tiers across two Availability Zones",
layout={"engine": "flex", "direction": "right", "gap": 32, "route": "ortho",
"area": [56, 176, 1808, 830]})
d.stylesheet("showcase.css")
d.stylesheet("aws.css")
with d.group("Internet edge", id="edge", class_="edge", direction="down", gap=22, padding=16):
users = d.box("Users", text="web, mobile", icon="aws:users", class_="external", **N)
dns = d.box("Route 53", text="latency DNS", icon="aws:amazon-route53", class_="edge-service", **N)
cdn = d.box("CloudFront", text="CDN, TLS", icon="aws:cloudfront", class_="edge-service", w=216, h=80)
waf = d.box("AWS WAF", text="managed rules", icon="aws:awswaf", class_="security", **N)
s3 = d.box("S3", text="static origin", icon="aws:s3", class_="storage", **N)
with d.group("us-east-1", style="aws-region", padding=16):
with d.group("VPC 10.0.0.0/16", style="aws-vpc", direction="right", align="center", gap=12, padding=16):
alb = d.box("ALB", text="HTTPS :443", icon="aws:alb", class_="load-balancer", **N)
with d.group(id="azs", class_="bare", direction="down", gap=20, padding=0):
tiers = {}
for az, n in (("a", 0), ("b", 1)):
with d.group(f"Availability Zone {az.upper()}", style="aws-az", direction="right", gap=12, padding=16):
with d.group(f"Public 10.0.{n}.0/24", style="aws-subnet", class_="public", padding=14):
web = d.box("Web", text="Nginx · ASG", icon="aws:amazon-ec2", class_="web-tier",
id=f"web-{az}", **N)
with d.group(f"Private 10.0.1{n}.0/24", style="aws-subnet", class_="private", padding=14):
app = d.box("App", text="ECS Fargate", icon="aws:amazon-elastic-container-service",
class_="app-tier", id=f"app-{az}", **N)
with d.group(f"Data 10.0.2{n}.0/24", style="aws-subnet", class_="data", padding=14):
db = d.box("Aurora " + ("writer" if az == "a" else "reader"), text="PostgreSQL",
icon="aws:amazon-aurora", class_="database", id=f"db-{az}", **N)
tiers[az] = (web, app, db)
users.to(dns, "resolve", step="Users resolve the domain")
dns.to(cdn, "alias", step="Route 53 aliases to CloudFront")
cdn.to(waf, step="AWS WAF filters each request")
waf.to(alb, step="The ALB balances across zones")
for az, (web, app, db) in tiers.items():
alb >> web
if az == "a":
web.to(app, step="Web tier calls the app tier")
app.to(db, "SQL", step="App tier writes to Aurora")
else:
web >> app
app.to(db, "SQL")
d.edge(tiers["a"][2], tiers["b"][2], "replication", class_="replication")
d.add_legend()Hub-and-spoke networking
A Transit Gateway hub with centralized inspection and egress, and hybrid connectivity.
"""AWS hub-and-spoke network: Transit Gateway with centralized inspection and egress, after the AWS
reference architecture for multi-VPC networking, with an automatic legend and numbered traffic steps."""
from spinagram import Diagram
N = {"w": 200, "h": 80} # compact cards: the whole network fits one slide
d = Diagram("AWS02", "Hub-and-spoke networking on AWS",
subtitle="Transit Gateway hub · centralized inspection and egress · hybrid connectivity",
layout={"engine": "flex", "direction": "right", "gap": 32, "route": "ortho",
"area": [56, 176, 1808, 830]})
d.stylesheet("showcase.css")
d.stylesheet("aws.css")
with d.group(id="hybrid", class_="bare", direction="right", gap=32, align="center"):
with d.group("Corporate data center", id="dc", class_="onprem", direction="down", gap=24,
icon="aws:group-corporatedatacenter"):
office = d.box("Branch offices", text="SD-WAN", icon="aws:officebuilding", class_="external", **N)
cgw = d.box("Customer gateway", text="BGP ASN 65010", icon="aws:vpc-customer-gateway",
class_="on-premises", **N)
with d.group(id="links", class_="bare", direction="down", gap=24):
dx = d.box("Direct Connect", text="2 × 10 Gbps", icon="aws:direct-connect", class_="hybrid-link", **N)
vpn = d.box("VPN", text="IPsec backup", icon="aws:aws-siteto-site-vpn", class_="hybrid-link", **N)
with d.group("us-east-1 · network account", style="aws-region", direction="down", gap=28, padding=16):
with d.group(id="services", class_="bare", direction="right", gap=24):
with d.group("Inspection VPC", style="aws-vpc"):
nfw = d.box("Network Firewall", text="stateful IPS", icon="aws:network-firewall", class_="security", **N)
with d.group("Egress VPC", style="aws-vpc", direction="down", gap=20):
nat = d.box("NAT gateway", text="per AZ", icon="aws:vpcnat-gateway", class_="egress", **N)
igw = d.box("Internet gateway", text="to 0.0.0.0/0", icon="aws:vpc-internet-gateway", class_="egress", **N)
with d.group(id="core", class_="bare", direction="right", gap=60, align="center"):
tgw = d.box("Transit Gateway", text="routing hub", icon="aws:aws-transit-gateway",
class_="transit-hub", **N)
with d.group(id="spokes", class_="bare", direction="down", gap=14):
spokes = [d.box(name, text=cidr, icon="aws:group-virtualprivatecloud-vpc", class_="workload", **N)
for name, cidr, env in (("Prod VPC", "10.1.0.0/16", "prod"),
("Staging VPC", "10.2.0.0/16", "staging"),
("Shared services", "10.3.0.0/16", "shared"))]
office >> cgw
cgw.to(dx, "primary", class_="private-circuit")
cgw.to(vpn, "backup", class_="backup-path")
dx.to(tgw, "DX gateway", class_="private-circuit", step="Branch traffic reaches AWS over DX")
vpn.to(tgw, class_="backup-path")
for s in spokes:
tgw.to(s, arrow="both")
tgw.to(nfw, "inspect", class_="inspection", step="TGW sends traffic to inspection")
nfw.to(nat, step="Allowed flows leave via NAT")
nat.to(igw, step="The internet gateway egresses")
d.add_legend()Google Cloud
Anycast load balancing, GKE in two regions, Spanner and streaming analytics.
"""A global web application on Google Cloud: one anycast load balancer in front of GKE in two
regions, Spanner for multi-region consistency and a streaming analytics path to BigQuery."""
from spinagram import Diagram
d = Diagram("GCP01", "Global web application on Google Cloud", node_size=(230, 84),
subtitle="Anycast load balancing · GKE in two regions · Spanner · streaming analytics",
layout={"engine": "flex", "direction": "right", "gap": 48, "route": "ortho",
"area": [56, 176, 1808, 830]})
d.stylesheet("showcase.css")
d.stylesheet("gcp.css")
with d.group("Global edge", id="edge", class_="edge", direction="down", gap=22, padding=16):
users = d.box("Users", text="worldwide", icon="k8s:user", class_="external")
dns = d.box("Cloud DNS", text="anycast, DNSSEC", icon="gcp:cloud-dns", class_="networking")
lb = d.box("Load Balancing", text="global L7 + CDN", icon="gcp:cloud-load-balancing",
class_="networking global-entry")
armor = d.box("Cloud Armor", text="WAF, DDoS", icon="gcp:cloud-armor", class_="security")
with d.group("Project · shop-prod", id="project", class_="project", direction="right", gap=56, align="center"):
with d.group("VPC · shared", id="vpc", class_="vpc", direction="down", gap=28):
regions = []
for rid, name in (("us", "us-central1"), ("eu", "europe-west1")):
with d.group(name, id=f"r-{rid}", class_="region", direction="down", gap=24):
gke = d.box("GKE Autopilot", text="web + api pods", icon="gcp:gke", class_="compute",
id=f"gke-{rid}")
cache = d.box("Memorystore", text="Redis, sessions", icon="gcp:memorystore", class_="data-store",
id=f"cache-{rid}")
regions.append((gke, cache))
with d.group("Managed data", id="data", class_="services", direction="down", gap=28):
spanner = d.box("Cloud Spanner", text="nam-eur-asia1", icon="gcp:cloud-spanner",
class_="data-store primary-database")
pubsub = d.box("Pub/Sub", text="order events", icon="gcp:pubsub", class_="analytics")
flow = d.box("Dataflow", text="streaming ETL", icon="gcp:dataflow", class_="analytics")
bq = d.box("BigQuery", text="warehouse", icon="gcp:bigquery", class_="analytics")
users.to(dns, "resolve", step="Users resolve the anycast IP")
dns.to(lb, step="One global L7 load balancer")
armor.to(lb, class_="security-policy")
for gke, cache in regions:
lb.to(gke, "HTTPS", step="Nearest healthy region serves" if gke.id == "gke-us" else None)
gke.to(cache, class_="session-cache")
for gke, _ in regions:
gke.to(spanner, "SQL", class_="database-traffic")
regions[1][0].to(pubsub, "events", class_="event-stream", step="Orders stream to analytics")
pubsub >> flow >> bq
d.callout(bq, "Streaming inserts are queryable in seconds; dashboards read live data.", title="Real-time",
kind="tip")
d.add_legend()Kubernetes
A dark cluster canvas built only with CSS: the anatomy of a workload, and service networking guarded by
NetworkPolicy. Both slides share k8s.css.
"""A Kubernetes application platform: control plane, a namespaced web app with autoscaling and a
stateful database on persistent volumes."""
from spinagram import Diagram
WIDE = {"w": 230, "h": 84}
POD = {"w": 150, "h": 60}
d = Diagram("K8S01", "Anatomy of a Kubernetes workload", node_size=(210, 84),
subtitle="Ingress to pods, autoscaling, config and stateful storage in one namespace",
layout={"engine": "flex", "direction": "right", "gap": 32, "route": "ortho",
"area": [56, 176, 1808, 830]})
d.stylesheet("showcase.css")
d.stylesheet("k8s.css")
with d.group("Cluster · prod-eu-1", id="cluster", class_="cluster", direction="down", gap=28, padding=16):
with d.group("Control plane", id="cp", class_="cp", direction="right", gap=64):
api = d.box("API server", text="REST, admission", icon="k8s:api-labeled", class_="control-plane", **WIDE)
etcd = d.box("etcd", text="3 members, Raft", icon="k8s:etcd", class_="control-plane")
sched = d.box("Scheduler", text="binds pods", icon="k8s:sched-labeled", class_="control-plane")
cm = d.box("Controllers", text="reconcile", icon="k8s:c-m-labeled", class_="control-plane", **WIDE)
with d.group(id="workloads", class_="bare", direction="right", gap=16, align="center"):
with d.group("namespace: shop", id="ns", class_="ns", direction="right", gap=16, align="center"):
with d.group(id="entry", class_="bare", direction="down", gap=40):
ing = d.box("Ingress", text="from clients, TLS", icon="k8s:ing", class_="networking")
svc = d.box("Service", text="ClusterIP :80", icon="k8s:svc", class_="networking")
with d.group(id="control", class_="bare", direction="down", gap=40):
hpa = d.box("HPA", text="2–10 replicas", icon="k8s:hpa", class_="autoscaling")
dep = d.box("Deployment", text="rolling update", icon="k8s:deploy", class_="workload", **WIDE)
cfg = d.box("ConfigMap", text="+ Secret", icon="k8s:cm", class_="configuration")
with d.group("ReplicaSet", id="rs", class_="pods", direction="down", gap=12):
pods = [d.box(f"web-{s}", text="ready", icon="k8s:pod", class_="pod", id=f"pod-{s}", **POD)
for s in "abc"]
with d.group(id="data", class_="bare", direction="down", gap=64):
sts = d.box("StatefulSet", text="postgres ×3", icon="k8s:sts", class_="workload", **WIDE)
pvc = d.box("PVC", text="50 Gi, RWO", icon="k8s:pvc", class_="storage")
pv = d.box("PV", text="gp3 StorageClass", icon="k8s:pv", class_="storage")
ing >> svc
svc.to(dep, "selects")
for p in pods:
d.edge(dep, p)
hpa.to(dep, "scales", class_="control-loop")
cfg.to(dep, "env", class_="injects-config")
pods[0].to(sts, "SQL")
sts.to(pvc, "claims")
pvc.to(pv, "bound", class_="binds-volume")
api.to(etcd, "watch", arrow="both")
d.callout(pv, "Snapshots every 6 h to object storage", title="Backups", kind="tip")
d.add_legend()Editable on the canvas
A hand-placed YAML diagram in the AWS group conventions: AWS Cloud, AWS account, Region, VPC and public and private
subnets, each group with its own icon and outline (the aws-* zone styles), and Users outside the cloud. Open it with
spinagram serve examples/showcase/editable_platform.yaml, press c, and drag nodes between subnets, drag a zone to
move its children, group nodes as a row or column, drop icons or provider zones from the palette, change a zone's
style from the bar, and undo with Ctrl/Cmd+Z.
# A hand-placed diagram made to be edited on the canvas (`spinagram serve`, Canvas), drawn with the AWS group
# conventions: AWS Cloud > AWS account > Region > VPC > public and private subnets, each group with its own icon and
# outline (the `aws-*` zone styles of the theme; Users sit outside the cloud). Two columns, nine icon nodes and
# labelled edges. Drag nodes between subnets, drag a zone to move its children, change a zone's style from the bar,
# drop a VPC or a subnet from the palette's Zones. Render: spinagram render examples/showcase/editable_platform.yaml
id: EDIT01
title: Editable platform
subtitle: Hand-placed on a grid, so every node, zone and group can be moved on the canvas
stylesheets: [showcase.css, aws.css]
zones:
- {id: cloud, label: "AWS Cloud", color: aws-cloud, x: 330, y: 175, w: 1550, h: 830, children: [account]}
- {id: account, label: "AWS account · production", color: aws-account, x: 360, y: 235, w: 1490, h: 740, children: [cdn, region]}
- {id: region, label: "Region · eu-west-1", color: aws-region, x: 390, y: 410, w: 1430, h: 540, children: [vpc, bucket, monitor]}
- {id: vpc, label: "VPC · 10.0.0.0/16", color: aws-vpc, x: 420, y: 470, w: 1070, h: 470, children: [public, app, data]}
- {id: public, label: "Public subnet", color: aws-public-subnet, x: 450, y: 530, w: 300, h: 190, children: [alb]}
- {id: app, label: "Private subnet · app", color: aws-private-subnet, x: 780, y: 530, w: 340, h: 380, children: [api, worker]}
- {id: data, label: "Private subnet · data", color: aws-private-subnet, x: 1150, y: 530, w: 310, h: 380, children: [db, cache]}
nodes:
- {id: users, title: Users, text: Browsers and apps, at: [80, 560, 220, 96], icon: "network:internet", class: external}
- {id: cdn, title: CloudFront, text: Edge cache and TLS, at: [400, 292, 220, 96], icon: "aws:cloudfront", class: edge-service}
- {id: alb, title: Load balancer, text: Application LB, at: [490, 600, 220, 96], icon: "aws:elastic-load-balancing", class: load-balancer}
- {id: api, title: Orders API, text: ECS service, icon: "aws:amazon-elastic-container-service", class: app-tier}
- {id: worker, title: Billing worker, text: Lambda, icon: "aws:aws-lambda", class: app-tier}
- {id: db, title: Orders DB, text: Aurora PostgreSQL, icon: "aws:amazon-aurora", class: database}
- {id: cache, title: Session cache, text: ElastiCache, icon: "aws:elasti-cache", class: database}
- {id: bucket, title: Reports, text: S3, at: [1540, 600, 220, 96], icon: "aws:amazon-simple-storage-service", class: storage}
- {id: monitor, title: Monitoring, text: CloudWatch, at: [1540, 736, 220, 96], icon: "aws:cloud-watch", class: edge-service}
columns:
- {x: 820, y: 600, gap: 40, nodes: [api, worker]}
- {x: 1175, y: 600, gap: 40, nodes: [db, cache]}
edges:
- {from: users, to: cdn, label: HTTPS, route: ortho}
- {from: cdn.bottom, to: alb.top, label: origin, route: ortho}
- {from: alb, to: api, label: forward, route: ortho}
- {from: api, to: db, label: SQL, route: ortho}
- {from: worker, to: cache, label: jobs, route: ortho}
- {from: db, to: bucket, label: export, route: ortho}
- {from: cache, to: monitor, label: metrics, route: ortho}More: Python guide · Automatic layout · Annotations