Making infrastructure diagram with python code


Introduction

When designing infrastructure in the cloud one thing you want to communicate is the design with your fellow engineers. There are usually industry standard tools that are used by industry like Lucid. I find that although these tools are great it would be nice to be able to write python code that will generate these diagrams. And when things change it will update those diagrams. Instead of having us go in and modify things as we iterate on our design.

Diagram python library

I found this library you can install it using the instructions on this website: https://diagrams.mingrammer.com/

But once you have the python dependencies you can generate an infrastructure diagram like the one below:

Profile pic

The python code to generate this is here:

# diagram.py
from diagrams import Diagram
from diagrams.aws.database import Dynamodb
from diagrams.aws.compute import  Lambda
from diagrams.aws.network import APIGateway

graph_attr = {
    "fontsize": "30",
    "bgcolor": "gray",
}
with Diagram("Serverless API", show=True, graph_attr=graph_attr):
    api = APIGateway("/v1/crud_api") 
    user_db =  Dynamodb("userdb")
    api >> Lambda("CreateRecord") >> user_db
    api >> Lambda("GetRecord") >>  user_db
    api >> Lambda("GetRecords") >>  user_db
    api >> Lambda("UpdateRecord") >> user_db
    api >> Lambda("DeleteRecord") >>  user_db

Zak's AI.Assist

Session only - not saved