azure-serverless-app / docs / architecture.py
architecture.py
Raw
"""Render with:  python architecture.py   (needs: pip install diagrams ; brew install graphviz)"""
from diagrams import Diagram, Cluster, Edge
from diagrams.azure.web import StaticApps
from diagrams.azure.integration import APIManagement
from diagrams.azure.compute import FunctionApps
from diagrams.azure.database import CosmosDb
from diagrams.azure.storage import StorageAccounts
from diagrams.azure.devops import Pipelines, Repos
from diagrams.onprem.client import User
from diagrams.onprem.iac import Terraform

G = {"fontsize": "13", "pad": "0.5", "nodesep": "0.9", "ranksep": "1.1"}


def arch():
    with Diagram("Employee Profile App - Runtime Architecture", filename="architecture",
                 outformat=["png", "svg"], show=False, direction="TB", graph_attr=G):
        user = User("Browser")
        with Cluster("Azure subscription"):
            with Cluster("Resource group: <SHARED_RG> (existing)"):
                swa = StaticApps("Static Web App\n(Free plan)\nindex.html + scripts.js")
            with Cluster("Resource group: <APP_RG> (East US)"):
                apim = APIManagement("API Management\n(Consumption)\nGET/POST /employees\nCORS policy")
                func = FunctionApps("Function App\n(Flex Consumption,\nPython 3.11)")
                cosmos = CosmosDb("Cosmos DB (NoSQL)\ndb employeesdb\ncontainer employeeProfile\npartition key /empId")
                store = StorageAccounts("Storage account\n(function package)")
        user >> Edge(label="1. HTTPS 443\nload page") >> swa
        user >> Edge(label="2. HTTPS 443\nAJAX GET/POST\n(cross-origin, CORS)", color="darkgreen") >> apim
        apim >> Edge(label="3. HTTPS 443\nforward /api/employees", color="darkgreen") >> func
        func >> Edge(label="4. HTTPS 443\nupsert / query\n(connection string\nin app setting)", color="darkgreen") >> cosmos
        func >> Edge(label="host storage /\ndeploy package", style="dashed", color="gray") >> store


def provisioning():
    with Diagram("Employee Profile App - Provisioning and CI/CD", filename="flows/provisioning-cicd",
                 outformat=["png", "svg"], show=False, direction="LR", graph_attr=G):
        dev = User("Engineer\n(laptop)")
        with Cluster("Azure DevOps"):
            repo = Repos("Repo: main")
            pipe = Pipelines("Pipeline\nAzureStaticWebApp@0")
        with Cluster("Azure"):
            state = StorageAccounts("Terraform state\n(storage account in <SHARED_RG>)")
            swa = StaticApps("Static Web App")
            infra = [FunctionApps("Function App"), APIManagement("API Management"), CosmosDb("Cosmos DB")]
        tf = Terraform("terraform apply")
        dev >> Edge(label="git push\n(HTML/JS)") >> repo >> Edge(label="trigger") >> pipe >> Edge(label="upload app_location /") >> swa
        dev >> Edge(label="az login") >> tf
        tf >> Edge(label="state + lock", style="dashed") >> state
        tf >> Edge(label="creates") >> infra
        dev >> Edge(label="func azure functionapp publish\n(run by Terraform null_resource)", color="darkgreen") >> infra[0]


if __name__ == "__main__":
    arch()
    provisioning()