Skip to main content

PyPI Version Latest Build

drawtf

Draw diagrams which include Cloud resources using TF state files or without them. Inspired by the Diagrams package and a burning desire to not have to manually keep architecture diagrams updated, this was born.

Prerequisites

Install

foo@bar:~$ python -m pip install --upgrade pip
foo@bar:~$ pip install drawtf
foo@bar:~$ drawtf --help

Usage: drawtf [OPTIONS]

  Top level command for drawtf.

Options:
  --help  Show this message and exit.

Commands:
  draw   Draw a single design from config and settings.
  watch  Watch a directory for changes to draw.

foo@bar:~$ drawtf draw --help

Usage: drawtf draw [OPTIONS]

  Draw a single design from config and settings.

Options:
  --name TEXT              The diagram name.
  --state TEXT             The tfstate file to run against.
  --platform TEXT          The platform to use 'azure' or 'aws', only 'azure'
                           currently supported

  --output-path TEXT       Output path if to debug generated json populated.
  --json-config-path TEXT  Config file path if populated.
  --verbose                Add verbose logs.
  --help                   Show this message and exit.

foo@bar:~$ drawtf watch --help

Usage: drawtf watch [OPTIONS]

  Watch a directory for changes to draw.

Options:
  --directory TEXT  Directory to watch for changes in.
  --help            Show this message and exit.

Draw

There are a few ways we can create diagrams here, all options on the CLI are optional, and it is basically just the order which you create them that draws a diagram.

Sample config File (app.json)

If we use a config file with the fields below, this will set the name for the designs title, import a state file, and add some custom components not in the state file, The final section at the bottom draws the lines between the resources.

{
    "name": "Aggreko Application (All Resources)",
    "state": "./test/app.tfstate",
    "components": [
        {
            "name": "Aggreko",
            "type": "draw_custom",
            "resource_group_name": "",
            "attributes":{},
            "components": [
                {
                    "name": "InternalDB",
                    "type": "draw_custom",
                    "resource_group_name": "",
                    "custom": "diagrams.azure.database.DatabaseForMysqlServers",
                    "attributes": {}
                },
                {
                    "name": "App",
                    "type": "draw_custom",
                    "resource_group_name": "",
                    "custom": "diagrams.onprem.compute.Server",
                    "attributes": {}
                },
                {
                    "name": "AnotherApp",
                    "type": "draw_custom",
                    "resource_group_name": "",
                    "custom": "diagrams.onprem.compute.Server",
                    "attributes": {}
                }
            ]
        }
    ],
	"links": [
        {
            "from": "App-draw_custom",
            "to": "InternalDB-draw_custom",
            "color": "darkgreen",
            "label": "Write",
            "type": "dotted"
        },
        {
            "from": "AnotherApp-draw_custom",
            "to": "InternalDB-draw_custom",
            "color": "darkgreen",
            "label": "Write",
            "type": "dotted"
        }
    ],
    "excludes": []
}                                            
foo@bar:~$ drawtf draw --json-config-path ./test/app.json    

Example

By running the command above pointing to the config file, this will set the name and grab other resources from the state file linked. Outputs from will create the design in the same sub-folder with the name app.png.

Override config File (app-subset.json)

Providing an override config alongside our main config file with the fields below, this will override the initial the designs title but still use the same state file and components from the original and attempt to join the links if all of the resources are available. You will notice an excludes section, if the keys for each resource added are in this list, then it will exclude those items.

{
    "name": "Aggreko Application (Subset)",
    "base": ".//test//app.json",
    "excludes": [ 
        "AnotherApp-draw_custom"
    ]
}  
foo@bar:~$ drawtf draw --json-config-path ./test/app-subset.json

Example

By running the command above pointing to the config file and override files, this will set the name from the override and grab other resources from the state file linked. Outputs from will create the design in the same sub-folder with the name app-subset.png.

Override config File and CLI overrides

foo@bar:~$ drawtf draw --json-config-path ./test/app-subset.json --name "Aggreko Application (Sample)" --state ./test/app.tfstate --output-path ./test/sample --verbose                                                                           

Example

The command above, though using the same config files, can override all for the name, state file path and output path. Outputs from will create the design in the directory test with the name sample.png.

Watch

It is now possible to simply watch a folder for *.json files to change or be created, this will then pick up the changes and draw designs as required.

foo@bar:~$ drawtf watch --directory ./test   

Starting watch for *.json files...
Watching in .//test...
Modified file .//test\app.json, drawing...
Adding resource APPLICATION_DEV-azurerm_resource_group
Adding resource storageaccount-azurerm_storage_account
Adding resource (from config) InternalDB-draw_custom
Adding resource (from config) App-draw_custom
Adding resource (from config) AnotherApp-draw_custom
Adding resource (from config) Aggreko-draw_custom
.//test\app.json done.
Modified file .//test\app-subset.json, drawing...
Adding resource APPLICATION_DEV-azurerm_resource_group
Adding resource storageaccount-azurerm_storage_account
Adding resource (from config) InternalDB-draw_custom
Adding resource (from config) App-draw_custom
Excluding resource (from config) AnotherApp-draw_custom
Adding resource (from config) Aggreko-draw_custom
WARNING:root:Ignoring link as object not in component cache: {'from': 'AnotherApp-draw_custom', 'to': 'InternalDB-draw_custom', 'color': 'firebrick', 'label': 'Write', 'type': 'dotted'}
.//test\app-subset.json done.

CI/CD Steps

Yes you can run this via GitHub actions or devops pipelines.

GitHub Actions

      - name: Setup Graphviz
        uses: ts-graphviz/setup-graphviz@v1
      - name: Set up Python 3.7
        uses: actions/setup-python@v4
        with:
          python-version: '3.7'
      - name: Generate Diagram
        run: |
          python -m pip install --upgrade pip
          pip install drawtf
          drawtf --help
          drawtf draw --json-config-path "./test/app.json"

Azure Devops

      - script: sudo apt-get -yq install graphviz
        displayName: Setup Graphviz
      - script: |
          python -m pip install --upgrade pip
          pip install drawtf
          drawtf --help
          drawtf draw --json-config-path "./test/app.json"
        displayName: Generate Diagram

Early days

Just an FYI, its early days here and is still a development style project. That said we are using for all of our projects internally using TF, but loads of resources types are still to be added.

Metadata

Release files for drawtf 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drawtf 0.12.0
File Size Uploaded
drawtf-0.12.0.tar.gz 21.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drawtf 0.12.0
File Interpreter ABI Platform
drawtf-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 67.5 kB

Release files / drawtf-0.12.0.tar.gz

Download URL drawtf-0.12.0.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a2ebc8163980ebb98b002b690303c2100e06fdbfeecec8d4b51c9cec73785d73
BLAKE2b-256 checksum
How to use checksums
3c83a34e4b04d5e7d2f5325c14f974d3a138a9059b0ab6baf711644a0834c38d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / drawtf-0.12.0-py3-none-any.whl

Download URL drawtf-0.12.0-py3-none-any.whl
Size 45.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c34873b736ee9ba067691be895e2bc2ac2167430898c904a1b07d9a82865f67
BLAKE2b-256 checksum
How to use checksums
2b8f5bd2a856007e33ad9e64155e8445823b8e6679e7c6ea24a9bfc3def36e1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page