# yaml-language-server: $schema=../references/diagram.schema.json
title: Multi-tenant SaaS with a shared ML pipeline on AWS
subtitle: One control plane routes each tenant to its own Amazon Aurora database; all of them feed one shared Amazon SageMaker AI pipeline
direction: down

legend:
  solid: Requests and data
  dotted: Sign-in

groups:
  cloud: { type: aws:cloud }
  region: { type: aws:region, label: us-east-1, in: cloud }
  control: { type: zone, label: Control plane, in: region }
  # The silo model: every tenant keeps its own database, one box per tenant.
  data: { type: zone, label: Tenant data silos, in: region }
  tenant-a: { type: zone, label: Tenant A, in: data }
  tenant-b: { type: zone, label: Tenant B, in: data }
  tenant-c: { type: zone, label: Tenant C, in: data }
  # cols: 5 lays the pipeline out as one row under the silos.
  pipeline: { type: zone, label: Shared ML pipeline, in: region, cols: 5 }

nodes:
  tenants: { icon: aws:users, label: Tenant users, note: Tenants A to C }
  auth: { icon: aws:cognito, label: Amazon Cognito, note: Pool per tenant, in: control }
  api: { icon: aws:api-gateway, label: Amazon API Gateway, in: control }
  # align keeps the router in the control plane's row, after API Gateway.
  router: { icon: aws:lambda, label: AWS Lambda, note: Tenant router, in: control, align: api }
  db-a: { icon: aws:aurora, label: Amazon Aurora, note: Tenant A data, in: tenant-a }
  db-b: { icon: aws:aurora, label: Amazon Aurora, note: Tenant B data, in: tenant-b }
  db-c: { icon: aws:aurora, label: Amazon Aurora, note: Tenant C data, in: tenant-c }
  # The endpoint the router calls sits beside the silos, level with the other request targets.
  endpoint: { icon: aws:sagemaker-ai, label: Amazon SageMaker AI, note: Inference endpoint, in: region }
  stream: { icon: aws:kinesis-data-streams, label: Amazon Kinesis Data Streams, note: Change events, in: pipeline }
  etl: { icon: aws:glue, label: AWS Glue, note: ETL jobs, in: pipeline }
  lake: { icon: aws:s3-bucket, label: Data lake, note: Amazon S3, in: pipeline }
  train: { icon: aws:sagemaker-ai-train, label: Training job, note: Amazon SageMaker AI, in: pipeline }
  registry: { icon: aws:sagemaker-ai-model, label: Model registry, note: Versioned models, in: pipeline }

edges:
  - tenants ..> auth: sign in
  - tenants -> api: requests
  # constraint: false keeps Cognito beside API Gateway.
  - { from: auth, to: api, line: dotted, constraint: false }
  - api -> router: tenant context
  # One arrow into the silos box and one out of it say what an arrow per tenant would.
  - router -> data: tenant's own database
  - router -> endpoint: predict
  - data -> stream: change events
  - stream -> etl -> lake -> train -> registry
  - endpoint -> registry: loads model
