# Google Cloud architecture diagram from Terraform
> Let an agent draw a team's Google Cloud architecture from its Terraform, as Google's reference diagrams draw it, and record each infrastructure pull request as a revision reviewers see before terraform apply.
## Metadata
- **Diagram family**: `architecture`
- **Source**: Terraform configuration / infrastructure pull requests
- **Workflow types**: scan, generate, update, diff, render
- **Audience**: platform, developer, agent
- **MCP tools**: `diagram_find`, `diagram_create`, `diagram_get`, `diagram_edit`, `diagram_history`, `diagram_render`
- **HTTP endpoints**:
  - `POST /v1/diagrams`
  - `GET /v1/diagrams/{id}`
  - `POST /v1/diagrams/{id}/revisions`
  - `GET /v1/diagrams/{id}/compare`
  - `GET /v1/diagrams/{id}/render`
  - `GET /v1/images/{id}@{revision}.svg`
---
---

## What this example shows

The production architecture of a used-car marketplace's listings platform, drawn by an agent from the team's Terraform as Google Cloud reference diagrams draw it. Each resource that runs or stores something is a Google product card. The buckets share one, drawn as a stack, the way Google draws several of one product. The agent wrote no coordinates.

The VPC runs across two Regions, each with its web, app and database tiers in rows and every VM on its zone's colored strip. Each Region's Memorystore cache sits between its app and database tiers, beside the app tier's line to the database. The primary database has a standby in a second zone, drawn on Google's gray, and a read replica in the other Region. The global load balancer sits above the Regions between Cloud Armor and Cloud CDN, and the buckets sit below the Regions.

Revision 2 records pull request 587, which adds a Pub/Sub topic and a Cloud Run function that makes thumbnails of listing photos. The **Revision diff** panel shows both revisions, before and after, with the compare response from `diagram_history`. It lists the topic, the function and their three edges as added. As changed, it lists the Region they joined and the cards the engine moved to make room.

## When to use it

When the infrastructure is defined in Terraform and the team wants an architecture diagram drawn from it, with each infrastructure pull request showing what it changes before `terraform apply`. A plan lists resources; the diagram shows where they run and what talks to what.

## What the agent does

On the first run the agent reads the configuration on the default branch. It makes a node with its Google Cloud icon for each resource that runs or stores something, the dual-region buckets sharing one stacked card. It puts each node in the group it runs in, and spans each zone over the nodes in it.

It adds an edge for each connection the configuration makes, such as a load balancer's backends or a database's replica, and saves the diagram with `diagram_create`. The engine places each group as Google's reference diagrams do and routes the edges.

For each pull request that changes the infrastructure, the agent turns the new and removed resources into typed operations. A `createNode` names the group a resource joins, an `updateFrame` adds a new VM to its zone, and `createEdge` and `deleteElement` add connections and remove what's gone. It saves them with `diagram_edit` as a revision whose message names the pull request. `diagram_history` with `compare` lists what changed, and over REST the image endpoint draws the new revision with the additions in green, ready to post on the pull request.

In pull request 587 the topic and the function take a row of their own in us-central1, below its subnet. The buckets and the Logging and Monitoring card move down to make room. The app tier's line to the topic drops past the database tier between two zone columns, the topic standing under it. That gap opens in both Regions, and what lies right of it moves right.

Each internal load balancer moves to stay centered on its tiers. Each cache moves left of the app tier's line to its database. The new row widens us-central1, so us-east1 moves right. The load balancer moves to stay over the gap between the Regions, and Cloud Armor and Cloud CDN beside it and the users above it move with it.

## What the output includes

- One architecture diagram per environment, in Google Cloud's groups and product cards on a light canvas, that every later run edits rather than redraws.
- A revision for each infrastructure pull request, its message starting with the pull request's number.
- A compare JSON listing the elements each pull request adds, changes and removes.
- A colored image of each pull request's revision, drawn by the REST image endpoint, with additions in green, removals in red and changes in amber. A card that only moved, or a group that only gained members, is drawn as unchanged.
- For a pull request closed without merging, a revision that restores the one before it, so the latest revision shows what the default branch deploys.
## Agent workflow
Keep a team's Google Cloud diagram in step with its Terraform: drawn once from the default branch, then one revision per infrastructure pull request, posted on the pull request before terraform apply.
**Inputs**

- The Terraform configuration on the default branch (infra/production)
- The pull request's diff of that configuration, or its plan as JSON (terraform show -json) when CI publishes one
- The diagram's Zindex key, listings-gcp-production, or its ID from .zindex.json (long-lived, never recreated)
- A Zindex API key (the colored diff image of a private diagram needs one), or an MCP client signed in to Zindex
**Outputs**

- Revision 1: the production architecture drawn from the default branch, in Google Cloud's groups and product cards, laid out by the engine
- One revision per infrastructure pull request, its message starting with the pull request's number
- Compare JSON listing the elements the pull request adds, changes and removes
- Before and after images, or one image with the changes colored, posted on the pull request
**Steps**

1. **Find the architecture diagram** - Call `diagram_find` with the diagram's key, `listings-gcp-production`, or read its ID from `.zindex.json`. If it exists, go to `read_current`. Over REST, `GET /v1/diagrams/{key}` answers 404 `NOT_FOUND` when there is none yet.
   _MCP: `diagram_find` · HTTP: `GET /v1/diagrams/${SCENE_ID}`_

2. **Read the Terraform on the default branch** - Read `infra/production` on the default branch and the conventions in its README. Each resource that runs or stores something becomes a node with its `gcp:` icon, in the innermost group the README places it in. Logging and Monitoring are one card, and the NAM4 buckets one stacked card. Health checks, backend services, URL maps, certificates, service accounts and role bindings aren't drawn; they become edges or nothing.

3. **Save the first drawing** - Call `diagram_create` with the key, a message naming the commit, a brief that states these conventions, and the scene with the nodes and edges from `read_terraform`. Write `diagramFamily: "architecture"` and a frame per Google Cloud group with its `containerType`, each subnet listing its web tier, internal load balancer, app tier, Memorystore instance and database tier in that order. A `gcp.vpc` spans both subnets and a `gcp.zone` spans each zone's nodes. Leave `layout`, `shape` and `fill` off, except the standby's gray `style.fill` (`#DADCE0`), and give the buckets card `style.multiple: true`. The engine arranges the groups and draws each node as Google's card. If the save's `warnings` carry `GCP_GROUP_UNTYPED` or `GCP_GROUP_NESTING`, act on the group each one names, as its fix says.
   _MCP: `diagram_create` · HTTP: `POST /v1/diagrams`_

4. **Read the diagram's current revision** - For each pull request that changes `infra/`, call `diagram_get`. The outline's first line gives the current revision number (MAIN_REVISION), the 'before' state for the compare, and its lines list each element inside its group, so you can see which IDs already exist and which group a new resource joins.
   _MCP: `diagram_get` · HTTP: `GET /v1/diagrams/${SCENE_ID}`_

5. **Read the pull request's change** - Read the pull request's diff of `infra/production`, or the `resource_changes` of its plan when CI publishes `terraform show -json`. List the resources it creates and destroys, and the connections it adds or removes: a topic an app tier's metadata names, a function's trigger, a role on a bucket, a backend service's instance groups.

6. **Compute the operation batch** - A new resource becomes a `createNode` with `frame` set to its group (PR 587's topic and function join `region_c1`), except a bucket in NAM4, which the buckets card stands for. A new VM also needs an `updateFrame` on its `gcp.zone` with the whole `spans`, or, for a new zone, a `createFrame` with `containerType` `gcp.zone`, its name as `title` and `spans` listing the VM. New connections are `createEdge`, destroyed resources `deleteElement` and renamed ones `updateNode`. Reuse the diagram's IDs and write no `layout`. A change that adds, removes or connects no drawn resource (machine types, resource labels, retention) isn't architectural.

7. **Save the pull request as a revision** - Call `diagram_edit` with the ops, `baseRevision` = MAIN_REVISION and a message that starts with the pull request's number; the new revision is PR_REVISION. A batch that changes nothing answers `noop: true` and makes no revision. In PR 587 the topic and the function take a row below us-central1's subnet, and the app tier's line to the topic opens a gap between two zone columns in both Regions. What lies past each growth moves by it, and the compare lists what moved.
   _MCP: `diagram_edit` · HTTP: `POST /v1/diagrams/${SCENE_ID}/revisions`_

8. **Compare the default branch's revision with the pull request's** - Call `diagram_history` with `compare: { from: MAIN_REVISION, to: PR_REVISION }`. It lists the element IDs the pull request adds, changes and removes. For PR 587 that is the topic, the function and their three edges added. As changed, it lists the Region they joined and the cards the engine moved to make room.
   _MCP: `diagram_history` · HTTP: `GET /v1/diagrams/${SCENE_ID}/compare?from=${MAIN_REVISION}&to=${PR_REVISION}`_

9. **Render both revisions** - Call `diagram_render` with `revision: MAIN_REVISION`, then with `revision: PR_REVISION`. Each returns SVG and PNG links that work without a key for 7 days, and each image prints its address with its revision number, so reviewers can tell which state they're looking at.
   _MCP: `diagram_render` · HTTP: `GET /v1/diagrams/${SCENE_ID}/render?revision=${MAIN_REVISION}`_

10. **Optional: one image with the changes colored** - With an API key, the image endpoint draws PR_REVISION with what changed since MAIN_REVISION colored, with additions in green, removals in red and changes in amber. A card that only moved, or a group that only gained members, is drawn as unchanged, though the compare lists it as changed. `diagram_render` and its signed links draw plain versions only, so an agent signed in through MCP posts the two images and the compare instead.
   _HTTP: `GET /v1/images/${SCENE_ID}@${PR_REVISION}.svg?from=${MAIN_REVISION}`_

11. **Post the change on the pull request** - Post one comment on the pull request before `terraform apply` runs: the summary (`+2 services, +3 connections`), the colored image or the two images, and the compare. When the pull request changes, record the difference as another revision with the same number, compare from MAIN_REVISION again, and update that comment rather than adding another.

12. **If the pull request is closed without merging** - The latest revision no longer matches what the default branch deploys. Call `diagram_edit` with `restoreRevision: MAIN_REVISION`, `baseRevision` set to the latest revision and a message saying the pull request was closed. The new revision equals MAIN_REVISION, and the pull request's revision stays in the history. A merged pull request needs nothing, since its revision already shows what `terraform apply` builds.
   _MCP: `diagram_edit` · HTTP: `POST /v1/diagrams/${SCENE_ID}/revisions`_
## Agent prompt

Drop this into a system prompt for an MCP-connected agent.

```
You look after the team's Google Cloud architecture diagram, drawn from its Terraform. You show reviewers what each infrastructure pull request changes before `terraform apply`. You draw the diagram once from the default branch, as Google Cloud reference diagrams draw it, then record each pull request that changes the infrastructure as a revision named for it.

The diagram's Zindex ID is `${SCENE_ID}` and its key is `listings-gcp-production`. The configuration is in `infra/production`, and the README beside `infra/` gives the conventions in full. Revisions are permanent, so step 7 covers a pull request closed without merging.

How to draw it:
- `diagramFamily: "architecture"`, and a frame for each Google Cloud group with its `containerType`, nested `gcp.cloud` > `gcp.region` > `gcp.subnet` > tiers: a `gcp.instanceGroup` for each managed instance group and a `gcp.tier` for the database tier. Each subnet lists its web tier, internal load balancer, app tier, Memorystore instance and database tier, in that order.
- Two kinds of group are spanning frames, written with `spans` instead of `children`: a `gcp.vpc` that spans both subnets, and a `gcp.zone` for each zone of each Region that spans the nodes in that zone. Write each Region's `gcp.zone` frames in zone order (`us-central1-a`, then `-b`, then `-c`): their columns follow that order.
- A node for each resource that runs or stores something, with its `gcp:` icon and a short label that says what it does, in the innermost group it runs in:
  - one VM per zone in its tier, and each Region's internal load balancer and Memorystore instance in its subnet, in no zone;
  - the database instances, the primary's standby included, in the database tier;
  - topics and functions in their Region, outside the subnet;
  - in Google Cloud: the global load balancer, Cloud Armor, Cloud CDN, one card for Logging and Monitoring (`gcp:observability`) and one stacked card for the NAM4 buckets;
  - the users outside Google Cloud.
- Write no `layout` or `shape`, and no `style` on a node or frame but these two:
  - the standby: `style: { fill: "#DADCE0" }`, as Google draws a standby resource on gray;
  - the buckets card: `style: { multiple: true }`, as Google stacks a card that stands for several instances of one product.
- The engine arranges the groups as Google's reference diagrams do (https://zindex.ai/docs/reference/layout-engine/#google-cloud-architecture-diagrams) and draws each node as Google's product card. Health checks, backend services and the other resources the README lists as not drawn tell you the edges.
- An edge from caller to callee for each connection the configuration makes:
  - Edges to and from a web or app tier name the tier's frame, not one VM. Edges to and from a database or cache name its instance.
  - Leave the request path (users, load balancers, tiers, databases, caches), the attachments of Cloud Armor and Cloud CDN, and the replica's edge to the buckets unlabeled.
  - Label every other edge with what flows, such as `Cross-region replication`, `Backups`, `Photo URLs` or `Thumbnails`.
  - Dash the standby's replication (`style.dash` `[8, 8]`).
  - Draw Cloud Armor's and Cloud CDN's attachments to the load balancer with no arrowheads (`endpoints: { startArrow: "none", endArrow: "none" }`).

Steps:

1. If you don't have the diagram's ID yet, call `diagram_find({ query: "listings-gcp-production" })`, which returns it. If there is none, build the scene from the default branch and call `diagram_create({ key: "listings-gcp-production", scene, message })` with a brief that states these conventions, and use the ID it returns. Act on any group the save's `warnings` name (`GCP_GROUP_UNTYPED`, `GCP_GROUP_NESTING`), as its fix says.

2. For each pull request that changes `infra/`, call `diagram_get({ id: "${SCENE_ID}" })`. Read the current revision number from the outline's first line; call this `MAIN_REVISION`. It is the 'before' state for the compare.

3. Read the pull request's diff, or its plan's `resource_changes` when CI publishes `terraform show -json`. Turn it into operations:
   - A new resource is a `createNode` with `frame` set to the group it runs in.
   - A new VM also joins its zone: an `updateFrame` on that `gcp.zone` with the zone's whole `spans` list, the new VM included. For a zone the diagram doesn't have yet, send a `createFrame` with `containerType: "gcp.zone"`, the zone's name as its `title`, and `spans` listing the VM.
   - A new bucket in the NAM4 dual-region joins the buckets card and needs no node. A bucket kept anywhere else is a `createNode` of its own.
   - New connections are `createEdge`, destroyed resources `deleteElement` and renamed ones `updateNode`.

   Reuse the diagram's IDs and write no `layout`. A new VM stands in its zone's column in its tier. A topic or function takes a free place in a row of its Region: below the subnet when the subnet's edges lead to it, above when its edges lead into the subnet.

   Where there is no room, the row or column grows. Where a new line needs a gap between two zone columns, the gap grows. What lies past a growth moves by it, and the compare in step 5 lists what moved.

4. Call `diagram_edit({ id: "${SCENE_ID}", baseRevision: MAIN_REVISION, message, ops })` with a message that starts with the pull request's number, such as `PR 587: make listing thumbnails in a Cloud Run function fed by Pub/Sub`. Call the new revision `PR_REVISION`. If the batch changes nothing, the response carries `noop: true` and no revision is made.

5. Call `diagram_history({ id: "${SCENE_ID}", compare: { from: MAIN_REVISION, to: PR_REVISION } })` for the elements the pull request adds, changes and removes. Call `diagram_render` with `revision: MAIN_REVISION` and with `revision: PR_REVISION` for SVG and PNG links valid for 7 days. With an API key, `GET /v1/images/${SCENE_ID}@PR_REVISION.svg?from=MAIN_REVISION` draws one image with additions in green, removals in red and changes in amber; `diagram_render` has no such option. A card that only moved, or a group that only gained members, is drawn as unchanged, though the compare lists it as changed.

6. Post one comment on the pull request: the summary (`+2 services, +3 connections`), the images and the compare. When the pull request changes, record the difference as another revision with the same number and update that comment rather than adding another.

7. If the pull request merges, leave the diagram as it is. If it is closed without merging, call `diagram_edit({ id: "${SCENE_ID}", baseRevision: <the latest revision>, message, restoreRevision: MAIN_REVISION })` with a message saying so. The latest revision then shows what the default branch deploys again.

Hard rules: name the pull request in every revision message, so the history shows when each part of the architecture arrived. Never redraw the diagram or recreate an element to change it; the IDs carry its history. Never give a node a `layout`. If the pull request adds, removes or connects no drawn resource (machine types, resource labels, retention periods), post 'No architectural changes' so reviewers know the check ran, and record nothing.
```
## Validation

Captured `POST /v1/diagrams/validate` response: **valid** (0 errors, 0 warnings)

_The scene validates with no errors or warnings._
## Resources
- **Canonical scene**: [/examples/gcp-architecture-from-terraform.scene.json](/examples/gcp-architecture-from-terraform.scene.json) (revision 2's scene, without the node positions Zindex saved with it)
- **Operations envelope**: [/examples/gcp-architecture-from-terraform.ops.json](/examples/gcp-architecture-from-terraform.ops.json)
- **Workflow recipe**: [/examples/gcp-architecture-from-terraform.workflow.json](/examples/gcp-architecture-from-terraform.workflow.json)
- **Revision diff**: [/examples/gcp-architecture-from-terraform.diff.json](/examples/gcp-architecture-from-terraform.diff.json)
- **Rendered SVG**: [/examples/gcp-architecture-from-terraform.svg](/examples/gcp-architecture-from-terraform.svg) (revision 2 as Zindex saved it: its edit moved no placed node `.diff.json` doesn't list; previewing `.scene.json` lays every node out from scratch, so nodes can land elsewhere)
- **Before SVG**: [/examples/gcp-architecture-from-terraform.before.svg](/examples/gcp-architecture-from-terraform.before.svg) (revision 1 as Zindex saved it, the revision diff's before picture)
- **Human page**: [/examples/gcp-architecture-from-terraform/](/examples/gcp-architecture-from-terraform/)
- **Manifest**: [/examples/index.json](/examples/index.json)
## Related examples

- [/examples/aws-architecture-from-terraform/](/examples/aws-architecture-from-terraform/)
- [/examples/pr-architecture-diff/](/examples/pr-architecture-diff/)
- [/examples/living-architecture-docs/](/examples/living-architecture-docs/)