You sign a new agency client at noon. By 6 p.m. their environment should be provisioned, monitored, backed up, and addressable at a vanity URL — without anyone improvising. Six months later that client churns, and within 14 days every resource of theirs is gone, every secret is rotated, every backup is archived to cold storage with a documented retention window, and not one orphan record is left behind. Both flows are runbooks. Neither belongs in someone’s head.
What’s happening
Tenant lifecycle on multi-tenant PaaS has four stages and most teams script the first one well and improvise the other three.
- Provision. Create the environment (or namespace), apply tenancy tags, create credentials and database, configure monitoring + log shipping, register the vanity URL, push the first deploy.
- Monitor. From day one: uptime check, error budget tracking, backup health, cert expiry watch. The monitoring is provisioned at the same time as the workload, not “later when we have time.”
- Decommission. When a tenant leaves: revoke access, halt the workload, archive data per the contractual retention window, then destroy. Not the other way around — destroy-then-archive is impossible.
- Retain. After destroy, the audit record stays: who created the tenant, who decommissioned, what data was archived where, what retention period applies, when the retention expires and the archive can finally be erased.
Skipping stage 2 means a tenant runs unmonitored until they complain, which can mean months. Skipping stage 3’s order means losing the data the customer paid you to preserve. Skipping stage 4 means failing an audit that you didn’t expect.
How to verify
Inventory tenants that are provisioned but not monitored:
# List tagged environments
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/getallenvs" \
-d "session=${SESSION}" \
| jq -r '.infos[] | [.env.envName, (.env.properties.tags // "{}")] | @tsv' \
> /tmp/envs.tsv
# Cross-reference with monitored tenants (assumed: in Prometheus targets)
curl -fsS http://prom.your-paas/api/v1/targets \
| jq -r '.data.activeTargets[].labels.tenant' \
| sort -u > /tmp/monitored.txt
awk -F'\t' '{print $1}' /tmp/envs.tsv | sort > /tmp/all_envs.txt
comm -23 /tmp/all_envs.txt /tmp/monitored.txt
# Anything here is a provisioned tenant that is NOT monitored
Inventory abandoned tenants (no traffic for 30 days, still consuming compute):
# Per-tenant request count from LB log over last 30 days
awk -v cutoff="$(date -d '30 days ago' +%s)" '
{
# rough timestamp parse — adjust to your log format
ts=$4; gsub(/[\[\]]/, "", ts)
cmd="date -d \"" ts "\" +%s"; cmd | getline epoch; close(cmd)
if (epoch > cutoff) print $7 # request host or path
}' /var/log/nginx/access.log \
| sort -u > /tmp/active_hosts.txt
Anything in the env list that isn’t in active_hosts.txt is a candidate for offboarding outreach.
Verify the audit trail exists for any tenant decommissioned in the last 90 days:
SELECT tenant_slug, decommissioned_at, archived_to, retention_until
FROM tenant_lifecycle_audit
WHERE decommissioned_at > now() - interval '90 days'
ORDER BY decommissioned_at DESC;
If a tenant was destroyed but the audit table has nothing, the runbook was bypassed.
The fix
Two scripts and one Postgres table. The audit table is non-negotiable — it’s what makes the runbook auditable.
CREATE TABLE IF NOT EXISTS tenant_lifecycle_audit (
tenant_slug text PRIMARY KEY,
provisioned_at timestamptz NOT NULL,
provisioned_by text NOT NULL,
decommissioned_at timestamptz,
decommissioned_by text,
archived_to text,
retention_until date,
notes text
);
Onboarding script — wraps the per-tenant work into one idempotent call:
#!/usr/bin/env bash
set -euo pipefail
TENANT_SLUG="${1:?usage: $0 <tenant-slug> <tier> <operator-email>}"
TENANT_TIER="${2:?}"
OPERATOR="${3:?}"
[[ "$TENANT_SLUG" =~ ^[a-z][a-z0-9-]+$ ]] || { echo "bad slug"; exit 1; }
[[ "$TENANT_TIER" =~ ^(standard|premium|trial)$ ]] || { echo "bad tier"; exit 1; }
ENV_NAME="${TENANT_SLUG}-prod"
VANITY="${TENANT_SLUG}.yourapp.com"
echo "==> 1. Create environment + tag"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/createenvironment" \
-d "session=${SESSION}" \
-d "env={\"envName\":\"${ENV_NAME}\",\"region\":\"default\",\"displayName\":\"${TENANT_SLUG}\"}" \
--data-urlencode "nodes=[
{\"nodeType\":\"litespeedphp\",\"fixedCloudlets\":2,\"flexibleCloudlets\":16,\"count\":1},
{\"nodeType\":\"postgres16\",\"fixedCloudlets\":2,\"flexibleCloudlets\":8,\"count\":1}
]"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/setenvproperty" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}" -d "name=tags" \
--data-urlencode "value={\"tenant\":\"${TENANT_SLUG}\",\"tier\":\"${TENANT_TIER}\"}"
echo "==> 2. Per-tenant credentials (see credential-isolation runbook)"
/opt/runbooks/provision-tenant-isolation.sh "${TENANT_SLUG}"
echo "==> 3. Bind vanity URL to wildcard cert"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/addexternaldomains" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}" \
-d "extdomains=${VANITY}"
echo "==> 4. Register monitoring target"
cat >> /etc/prometheus/targets/tenants.json <<EOF
{"labels":{"tenant":"${ENV_NAME}","tier":"${TENANT_TIER}"},
"targets":["${VANITY}:443"]}
EOF
systemctl reload prometheus
echo "==> 5. Register uptime check"
curl -fsS -X POST "${UPTIME_KUMA_API}/api/monitor" \
-H "Authorization: Bearer ${UPTIME_KUMA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"name\":\"${TENANT_SLUG}\",\"type\":\"https\",\"url\":\"https://${VANITY}/health\",\"interval\":60}"
echo "==> 6. Register backup schedule"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/createbackup" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}" \
-d "cron=15 03 * * *" -d "rotation=14"
echo "==> 7. Audit row"
psql -h pgops -U opsadmin -d ops <<SQL
INSERT INTO tenant_lifecycle_audit (tenant_slug, provisioned_at, provisioned_by)
VALUES ('${TENANT_SLUG}', now(), '${OPERATOR}')
ON CONFLICT (tenant_slug) DO UPDATE
SET provisioned_at = EXCLUDED.provisioned_at,
provisioned_by = EXCLUDED.provisioned_by;
SQL
echo "==> Done. ${TENANT_SLUG} live at https://${VANITY}/"
Offboarding script — preserves data before destroying anything, leaves an audit trail:
#!/usr/bin/env bash
set -euo pipefail
TENANT_SLUG="${1:?usage: $0 <tenant-slug> <retention-days> <operator-email>}"
RETENTION_DAYS="${2:?}"
OPERATOR="${3:?}"
ENV_NAME="${TENANT_SLUG}-prod"
ARCHIVE_BUCKET="s3://tenant-archives/${TENANT_SLUG}"
RETENTION_UNTIL=$(date -u -d "+${RETENTION_DAYS} days" +%Y-%m-%d)
echo "==> 1. Revoke customer access"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/setaccessgroup" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}" -d "group=disabled"
echo "==> 2. Final backup before halt"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/createbackup" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}" -d "tag=offboard-final"
echo "==> 3. Halt workload"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/stopenv" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}"
echo "==> 4. Archive data to cold storage"
aws s3 sync "/data/tenants/${TENANT_SLUG}/" "${ARCHIVE_BUCKET}/files/" \
--storage-class GLACIER
pg_dump -h pgsql.shared-prod -U opsadmin -Fc "tenant_${TENANT_SLUG//-/_}" \
| aws s3 cp - "${ARCHIVE_BUCKET}/db.dump"
echo "==> 5. Audit row BEFORE destroying anything"
psql -h pgops -U opsadmin -d ops <<SQL
UPDATE tenant_lifecycle_audit
SET decommissioned_at = now(),
decommissioned_by = '${OPERATOR}',
archived_to = '${ARCHIVE_BUCKET}',
retention_until = '${RETENTION_UNTIL}'
WHERE tenant_slug = '${TENANT_SLUG}';
SQL
echo "==> 6. Destroy environment + per-tenant resources"
curl -fsS -X POST "${JEM_API}/1.0/environment/control/rest/deleteenv" \
-d "session=${SESSION}" -d "envName=${ENV_NAME}"
# Drop database role + database
psql -h pgsql.shared-prod -U opsadmin -d postgres <<SQL
REVOKE ALL ON DATABASE tenant_${TENANT_SLUG//-/_} FROM tenant_${TENANT_SLUG//-/_};
DROP DATABASE tenant_${TENANT_SLUG//-/_};
DROP ROLE tenant_${TENANT_SLUG//-/_};
SQL
# Remove system user + PHP-FPM pool
rm -f /etc/php-fpm.d/${TENANT_SLUG}.conf
systemctl reload php-fpm
userdel "tenant_${TENANT_SLUG//-/_}" || true
# Remove monitoring + uptime
sed -i "/\"tenant\":\"${ENV_NAME}\"/,+1d" /etc/prometheus/targets/tenants.json
systemctl reload prometheus
echo "==> 7. Schedule archive expiry"
aws s3api put-object-tagging --bucket tenant-archives \
--key "${TENANT_SLUG}/" \
--tagging "TagSet=[{Key=expires,Value=${RETENTION_UNTIL}}]"
echo "==> Done. ${TENANT_SLUG} decommissioned. Archive retained until ${RETENTION_UNTIL}."
Two operational rules to enforce: (1) decommissioning is gated on a written customer confirmation that the data export is no longer needed, captured in the audit notes column, (2) a weekly cron sweeps archives whose retention_until has passed and physically deletes them — both for compliance and to stop the archive bucket growing forever.
Terraform glue, for the audit + monitoring side:
resource "jelastic_environment" "tenants" {
for_each = var.active_tenants
env_name = "${each.key}-prod"
display_name = each.key
tags = {
tenant = each.key
tier = each.value.tier
}
# ... nodes ...
}
resource "uptimekuma_monitor" "tenants" {
for_each = var.active_tenants
name = each.key
type = "https"
url = "https://${each.key}.yourapp.com/health"
interval = 60
}
Stack Harbor runs tenant lifecycle for agencies and SaaS platforms under environment management — onboarding that wires monitoring at provision time and offboarding that doesn’t lose data or leave orphans. If your team is doing this from a checklist instead of a script, our agency partner program hands you the working runbook on day one — book a lifecycle review.