# AWS architecture diagram from Terraform
> Let an agent draw a team's AWS architecture from its Terraform, as AWS 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 bookings platform, drawn by an agent from the team's Terraform as AWS reference diagrams draw it: AWS's icons and group styles, the two Availability Zones side by side with their subnets in matching rows, the load balancer between them, the internet gateway on the VPC's border, regional services beside the VPC and global services along the top. The agent wrote no coordinates. Revision 2 records pull request 412, which adds an SQS queue and a Lambda worker that resizes uploaded photos. Scroll to the **Revision diff** panel below to see both revisions, before and after, with the compare response from `diagram_history`. It lists the queue, the worker and their three edges as added, and as changed the Region they joined and the uploads bucket, which moved down the Region's column 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, makes a node with its AWS icon for each resource that runs or stores something, puts it in the AWS group it runs in, adds an edge for each connection the configuration grants, such as a load balancer's targets or an IAM permission on a bucket, and saves the diagram with `diagram_create`. The engine places each group by what it is 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` that names the group to join, `createEdge` and `deleteElement`, and saves them with `diagram_edit` as a revision whose message names the pull request. The new services join the Region's column next to the services they talk to, the uploads bucket moves down to make room, and nothing inside the VPC moves. `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.

## What the output includes

- One architecture diagram per environment, in AWS's groups and icons on a dark 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.
- 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
Draw a team's AWS architecture diagram from its Terraform, once from the default branch and as AWS reference diagrams draw it, then record each infrastructure pull request as a revision named for it and share the compare and the colored diff on the pull request, so reviewers see what the change adds or removes 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, bookings-aws-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 AWS's groups and icons, 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, `bookings-aws-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. Each resource that runs or stores something becomes a node with its `aws:` icon, in the innermost AWS group it runs in: the ECS service's tasks, the NAT gateways and the database and cache instances in their subnets, one per Availability Zone; the load balancer and the internet gateway in the VPC; S3, SQS, Cognito, CloudWatch and any Lambda function without `vpc_config` in the Region; Route 53, CloudFront and WAF in AWS Cloud; the customers outside it. Security groups, IAM, certificates, log groups, target groups and listeners aren't drawn. They become edges or nothing.

3. **Save the first drawing** - Call `diagram_create` with the key, a message naming the commit, and the scene: `diagramFamily: "architecture"`, a frame per AWS group with its `containerType` (`aws.cloud`, `aws.region`, `aws.vpc`, `aws.az`, `aws.subnet.public`, `aws.subnet.private`), each Availability Zone listing its public subnet first, the nodes with their icons and short labels, an edge for each connection the configuration grants, and a brief that states these conventions. Leave `layout` and `shape` off. The engine arranges the groups as AWS reference diagrams do and draws each icon alone. `AWS_GROUP_UNTYPED` or `AWS_GROUP_NESTING` in the save's `warnings` names a group: do what 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: an IAM permission on a queue or bucket, an event source mapping, a CloudFront origin, a target group's targets.

6. **Compute the operation batch** - New resources become `createNode` with `frame` set to the group they run in (an SQS queue and a Lambda function outside the VPC join `region`), a new subnet or Availability Zone `createFrame` with `frame` set to the group that holds it, new connections `createEdge`, destroyed resources `deleteElement`, renamed ones `updateNode`. Reuse the IDs the diagram already has and write no `layout`. A change that adds, removes or connects no drawn resource (instance sizes, tags, 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: `PR 412: resize uploaded photos in a Lambda worker fed by SQS`. This creates PR_REVISION. The engine places the new services in the Region's column next to their neighbors, moves the uploads bucket down to make room and grows the Region; nothing inside the VPC moves. A batch that changes nothing answers `noop: true`, and no revision is made.
   _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 412 that is the queue, the worker and their three edges added, and as changed the Region they joined and the uploads bucket the engine moved down 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 group whose only change is new members keeps its own color, so the new services stand out. `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 AWS architecture diagram, drawn from its Terraform, and show reviewers what each infrastructure pull request changes before `terraform apply`. You draw the diagram once from the default branch, as AWS 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 `bookings-aws-production`. The configuration is in `infra/production`. Revisions are permanent, so step 7 covers a pull request closed without merging.

How to draw it:
- `diagramFamily: "architecture"`, and a frame for each AWS group with its `containerType`, nested `aws.cloud` > `aws.region` > `aws.vpc` > `aws.az` > `aws.subnet.public` and `aws.subnet.private`. Each Availability Zone lists its public subnet first, then its private subnets in the same order as the other zones.
- A node for each resource that runs or stores something, with its `aws:` icon and a short label, in the innermost group it runs in: tasks, NAT gateways and database instances in their subnets, one per zone; the load balancer and internet gateway in the VPC; S3, SQS, Cognito, CloudWatch and Lambda functions without `vpc_config` in the Region; Route 53, CloudFront and WAF in AWS Cloud; customers outside it. Write no `layout`, `shape` or `fill`. The engine arranges the groups as AWS does and draws each icon alone. Security groups, IAM, certificates, log groups and target groups aren't drawn; they tell you the edges.
- An edge from caller to callee for each connection the configuration grants, labeled with what flows; dash replication. Draw calls to regional services from the task in the last zone, and logging as one edge from the VPC to CloudWatch.

Steps:

1. If you don't have the diagram's ID yet, call `diagram_find({ query: "bookings-aws-production" })`, which returns it. If there is none, build the scene from the default branch and call `diagram_create({ key: "bookings-aws-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 (`AWS_GROUP_UNTYPED`, `AWS_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`. New resources → `createNode` with `frame` set to the group they run in; a new subnet or Availability Zone → `createFrame` with `frame` set to the group that holds it and its first resource in `children`; new connections → `createEdge`; destroyed resources → `deleteElement`; renamed ones → `updateNode`. Reuse the diagram's IDs and write no `layout`. New nodes take a free place in their group, next to their neighbors. Where a subnet row or the Region's column is full, it opens there and what lies past it moves along; the save's `changed` names 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 412: resize uploaded photos in a Lambda worker fed by SQS`. 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, then `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.

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, so the latest revision 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 (instance sizes, tags, 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/aws-architecture-from-terraform.scene.json](/examples/aws-architecture-from-terraform.scene.json) (revision 2's scene, without the node positions Zindex saved with it)
- **Operations envelope**: [/examples/aws-architecture-from-terraform.ops.json](/examples/aws-architecture-from-terraform.ops.json)
- **Workflow recipe**: [/examples/aws-architecture-from-terraform.workflow.json](/examples/aws-architecture-from-terraform.workflow.json)
- **Revision diff**: [/examples/aws-architecture-from-terraform.diff.json](/examples/aws-architecture-from-terraform.diff.json)
- **Rendered SVG**: [/examples/aws-architecture-from-terraform.svg](/examples/aws-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/aws-architecture-from-terraform.before.svg](/examples/aws-architecture-from-terraform.before.svg) (revision 1 as Zindex saved it, the revision diff's before picture)
- **Human page**: [/examples/aws-architecture-from-terraform/](/examples/aws-architecture-from-terraform/)
- **Manifest**: [/examples/index.json](/examples/index.json)
## Related examples

- [/examples/pr-architecture-diff/](/examples/pr-architecture-diff/)
- [/examples/living-architecture-docs/](/examples/living-architecture-docs/)
- [/examples/api-dependency-map/](/examples/api-dependency-map/)