What drives ephemeral preview environments cost?
A per-PR preview environment is a short-lived copy of your application stack, deployed automatically when a pull request opens and torn down when it merges or closes. Teams use them so reviewers, QA and product owners can test a change in isolation without contending for a shared staging environment. That isolation is also why they get expensive: every PR gets its own copy of everything.
How many environments you create matters less than what keeps running after a PR goes quiet. A PR that sits unreviewed over a weekend costs the same per hour as one under active review, unless something scales it down. Most spend comes from idle resources that nobody is using.
| Component | Bills while PR is idle? | Why it keeps costing |
|---|---|---|
| Always-on compute (pods, VMs, containers) | Yes | Reserved CPU and memory are billed per hour, whether or not they serve requests |
| Databases and stateful services | Yes | Managed instances and caches rarely scale to zero and often have minimum sizes |
| Load balancers and public IPs | Yes | Providers typically charge per hour of existence, not per request |
| Persistent volumes and snapshots | Yes, even after teardown | Storage is billed by capacity, and snapshots often outlive the environment |
| Log and metric ingestion | Partly | Health checks, probes and debug-level logging generate data with zero traffic |
| Orphaned resources | Yes, indefinitely | Failed teardowns leave DNS records, volumes and LBs with no owner to notice them |
The last row is often the most expensive. The other rows stop billing when cleanup succeeds. Orphans keep billing until someone finds them.
How do per-PR preview environments work?
A preview environment is a pipeline driven by events from the code host. The lifecycle has four stages.
- Trigger. When a PR opens, gets new commits, or gets a label, the code host sends a webhook to CI. The pipeline reads the PR number, branch and commit SHA from the event.
- Build. CI builds container images for the changed services and tags them with the PR number or commit SHA, such as
checkout:pr-1234. Services the PR doesn't touch reuse existing images. - Deploy. The pipeline creates an isolated scope for the PR. That scope is usually a Kubernetes namespace like
preview-pr-1234, though some setups use a separate IaC stack. The pipeline deploys the services into that scope, provisions or attaches a database, and registers a hostname on a wildcard DNS record, such aspr-1234.preview.example.com. A shared ingress controller routes that hostname to the PR's services. The pipeline then posts the URL back to the PR as a comment or a deployment status, so reviewers can find it. - Update and teardown. Each new push redeploys into the same scope. When the PR closes or merges, a second webhook triggers the destroy job. Expiry timestamps and a reconciler catch anything the destroy job misses, as described below.
Every resource the pipeline creates carries the PR number, repository and owner as tags or labels. Cleanup, cost reporting and the live-environment dashboard all depend on those identifiers. A resource without them can't be traced to a PR and tends to become an orphan.
How much can scale-to-zero and TTLs save? An illustrative estimate
Illustrative estimate (assumed numbers, not a benchmark). Assume 30 PRs open at any time, an average PR lifetime of 4 days, 5 services per stack, and $0.40 per stack-hour for compute. All of these are assumptions. Swap in your own values:
monthly cost = concurrent stacks × hourly cost per stack × billed hours per month
| Policy | Concurrent stacks | Billed hours/month | Monthly cost |
|---|---|---|---|
| Always-on | 30 | 730 | 30 × 0.40 × 730 = $8,760 |
| Working hours only (10h × 22 days) | 30 | 220 | 30 × 0.40 × 220 = $2,640 |
| Scale to zero after inactivity (~3 active h/day × 22) | 30 | 66 | 30 × 0.40 × 66 = $792 |
| Inactivity scaling + TTL deletes stale PRs (assume 10 of 30) | 20 | 66 | 20 × 0.40 × 66 = $528 |
Scale-to-zero only reduces compute. Persistent volumes, load balancers and DNS keep billing while a stack sleeps, so those costs set a floor that only TTL deletion removes. With a 4-day average lifetime, most of the always-on bill pays for nights, weekends, and PRs nobody is looking at.
The savings come with a cost to reviewers. A reviewer who opens a sleeping preview waits for cold start: containers scheduled, images pulled, migrations run, caches empty. If that wait lasts minutes or ends in a timeout, people stop trusting previews. A wake-up page that shows progress makes the wait easier to accept.
TTLs fail in a similar way. If a stack is deleted the day before a demo, someone files a request to disable TTLs, and the savings disappear. Provide a cheap extend action, such as a PR label or a comment command, that pushes the expiry forward by a fixed amount and records who extended it. Engineers will keep the policy if bypassing it is easy and visible.
How does layered TTL cleanup work?
No single cleanup trigger is reliable on its own, so treat teardown as three layers that each catch what the one before it misses.
Layer 1: event-driven teardown. When a PR closes or merges, the CI system receives the webhook and runs the destroy job. This handles most environments within minutes. It also fails in predictable ways. Webhook deliveries get dropped or time out. Someone cancels the destroy pipeline halfway through. A force-push or branch rename confuses the job's lookup. Infrastructure-as-code state drifts from reality, so the destroy step skips resources it no longer tracks.
Layer 2: inactivity expiry. Each environment stores an expiry timestamp. New commits or incoming traffic push it forward, and the extend action pushes it forward manually. A scheduled job deletes anything past its expiry, which catches environments whose PRs stay open but idle for days.
Layer 3: the reconciler. On a schedule, a job lists every preview resource in the cloud account, asks the code host whether the matching PR is still open, and deletes any orphan. The reconciler starts from what actually exists, not from what pipelines think they created. That makes it the backstop for the first two layers.
All three layers depend on consistent tagging. Tag every resource with the PR number, repository, owner, and creation time, including resources created outside the main IaC stack: DNS records, object storage buckets, managed database instances, and queues. The reconciler can then query by tag, and the same tags let billing reports attribute spend to individual PRs and teams.
import os
from datetime import datetime, timezone
import boto3, requests
GH = ''
HEADERS = {'Authorization': f"Bearer {os.environ['GITHUB_TOKEN']}",
'Accept': 'application/vnd.github+json'}
DRY_RUN = os.environ.get('DRY_RUN', 'true') == 'true'
def pr_is_open(repo, number):
# repo is 'org/name'; a 404 means the PR or repo is gone, so treat it as closed
r = requests.get(f'{GH}/repos/{repo}/pulls/{number}', headers=HEADERS, timeout=10)
return r.status_code == 200 and r.json()['state'] == 'open'
def expired(tags):
# Layer 2: expiry timestamp pushed forward by commits, traffic or 'extend'
ts = tags.get('preview-expires-at') # ISO 8601 with offset, e.g. 2026-11-09T18:00:00+00:00
return ts is not None and datetime.fromisoformat(ts) < datetime.now(timezone.utc)
def delete_resource(arn, tags):
# Dispatch on service in the ARN (s3, rds, route53, sqs...); each needs its own delete call
print(f"{'DRY RUN ' if DRY_RUN else ''}delete {arn} pr={tags['preview-pr']} owner={tags.get('preview-owner')}")
tagging = boto3.client('resourcegroupstaggingapi', region_name='us-east-1')
pages = tagging.get_paginator('get_resources').paginate(
TagFilters=[{'Key': 'preview-pr'}]) # every preview resource, IaC-managed or not
checked = {}
for page in pages:
for res in page['ResourceTagMappingList']:
tags = {t['Key']: t['Value'] for t in res['Tags']}
key = (tags.get('preview-repo'), tags['preview-pr'])
if key not in checked: # one code-host lookup per PR, not per resource
checked[key] = key[0] is not None and pr_is_open(*key)
if not checked[key] or expired(tags):
delete_resource(res['ResourceARN'], tags)
A scheduled reconciler that finds preview resources by tag, checks each PR's state on the code host, and deletes orphaned or expired resources, defaulting to dry run.
Which database strategy should per-PR environments use?
Full production copies are off the table. They multiply storage and compute by the number of open PRs, and they spread real customer data into short-lived environments with weaker access controls. That leaves three realistic options.
| Strategy | Cost per PR | Isolation | Data realism |
|---|---|---|---|
| Shared server, schema or logical database per PR | Very low | Logical only | Seeded, often thin |
| Per-PR containerized database from fixtures | Low to moderate | Full | Only as good as fixtures |
| Copy-on-write or branching storage | Low until writes diverge | Full | High, if the source is masked |
Branching storage looks ideal because only changed blocks cost money. Whether you can use it depends on your platform. Its realism also depends on the snapshot it branches from. A snapshot with incomplete masking, such as a missed free-text column or an unhashed email in a JSON blob, is a compliance incident that gets copied into every preview. Fixture seeding avoids that risk. The trade-off is a realism gap: query plans, edge-case rows and data volume rarely match production, so performance bugs slip through.
On a shared server, give each PR its own schema and a role that can only reach that schema. A PR's migrations should never touch another PR's objects. Isolation still has limits. Long-running backfills, index builds and destructive changes compete for the same CPU, I/O and catalog locks. Lock waits then surface as timeouts in unrelated PRs' tests, and that cross-PR flakiness erodes trust in previews. A practical rule: detect PRs that add or modify migration files and route them to a dedicated per-PR database. Everything else stays on the shared server.
#!/usr/bin/env bash
set -euo pipefail
PR_NUMBER="${PR_NUMBER:?set by CI}"
[[ "$PR_NUMBER" =~ ^[0-9]+$ ]] || { echo "bad PR number" >&2; exit 1; }
NAME="pr_${PR_NUMBER}"
PR_DB_PASSWORD="${PR_DB_PASSWORD:?injected from CI secret store}"
SHARED_ADMIN_URL="${SHARED_ADMIN_URL:?admin connection string for shared server}"
if git diff --name-only --diff-filter=AM origin/main...HEAD | grep -q '^db/migrations/'; then
docker run -d --name "preview-db-${PR_NUMBER}" \
-e POSTGRES_USER="$NAME" -e POSTGRES_PASSWORD="$PR_DB_PASSWORD" \
-e POSTGRES_DB="$NAME" postgres:16
echo "routed ${NAME} to dedicated database"
exit 0
fi
psql "$SHARED_ADMIN_URL" -v ON_ERROR_STOP=1 <<SQL
CREATE ROLE ${NAME} LOGIN PASSWORD '${PR_DB_PASSWORD}' CONNECTION LIMIT 10;
CREATE SCHEMA ${NAME} AUTHORIZATION ${NAME};
ALTER ROLE ${NAME} SET search_path = ${NAME};
-- Fail fast instead of queueing behind another PR's locks.
ALTER ROLE ${NAME} SET lock_timeout = '5s';
ALTER ROLE ${NAME} SET statement_timeout = '60s';
SQL
echo "provisioned schema ${NAME} on shared server"
A CI provisioning script that sends migration-touching PRs to a dedicated containerized PostgreSQL instance and gives every other PR its own schema and role on a shared server, with lock and statement timeouts to limit cross-PR contention.
How can you implement smaller, cheaper preview stacks?
The same principle applies to the rest of the stack: share anything expensive whose isolation is not what the PR is testing.
Share the costly infrastructure
- Ingress: Run one ingress controller or load balancer and route by hostname, such as
pr-1234.preview.example.com. A load balancer per PR adds a fixed hourly charge that keeps running even when the pods behind it have scaled to zero. - Message brokers: Use one broker with a per-PR topic prefix or virtual host. This keeps traffic separate without paying for another cluster.
- Caches: Use a shared cache with a key prefix that includes the PR number. Then delete by that prefix during teardown.
- Third-party APIs: Point to stubs or vendor sandboxes. This avoids real charges and rate-limit collisions.
Deploy less
Make previews opt-in with a PR label, so documentation and dependency-bump PRs never start a stack. When a preview is requested, deploy only the services the diff touches plus their direct dependencies. Route everything else to a shared baseline. Set replicas to one and size resource requests from observed preview usage, not production. Inflated requests reserve node capacity that nothing uses.
Trim observability
Observability spend often grows unnoticed. Give preview logs and traces a retention of days, not weeks, and lower trace sampling. Drop preview metrics before they reach long-term storage, because each PR adds new label values and therefore new series.
Each shortcut makes the preview less like production. Shared brokers hide partitioning bugs, and stubs hide vendor quirks. Record what previews do not cover, so a green preview is not mistaken for production readiness.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: checkout
namespace: preview-pr-1234
spec:
ingressClassName: shared-preview # one controller for all previews
rules:
- host: pr-1234.preview.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: {name: checkout, port: {number: 80}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: checkout
namespace: preview-pr-1234
spec:
replicas: 1 # previews need correctness, not redundancy
selector: {matchLabels: {app: checkout}}
template:
metadata: {labels: {app: checkout}}
spec:
containers:
- name: checkout
image: registry.example.com/checkout:pr-1234
env: # app-defined settings that point at shared deps
- {name: TOPIC_PREFIX, value: "pr-1234."} # shared broker, per-PR topics
- {name: CACHE_KEY_PREFIX, value: "pr-1234:"} # delete by prefix on teardown
- {name: PAYMENTS_API_URL, value: ""}
resources:
requests: {cpu: 50m, memory: 128Mi} # sized from preview usage, not prod
limits: {memory: 256Mi}
A per-PR Kubernetes Ingress and Deployment that use a shared ingress controller, one replica, small resource requests, and environment settings that point the service at a shared broker, a shared cache and a payments stub through per-PR prefixes.
What guardrails keep per-PR environments from running away?
Cost controls work best as a few independent limits, each catching a failure the others miss.
Concurrency caps per repo or team
Set a maximum number of live previews per repository or team, for example eight. When a new PR would exceed the cap, the pipeline either queues it or evicts the oldest idle environment. A cap puts a hard ceiling on spend that holds even if TTL cleanup breaks. It also makes a team choose which PRs need a running stack. Many draft PRs don't.
Namespace-level resource quotas
Give each preview its own namespace with a resource quota on CPU, memory, persistent volume claims and load balancer services, and a default limit range so pods without requests still get bounded. A misconfigured replica count or a memory leak then fails inside one preview. It can't take over cluster capacity and trigger node autoscaling.
Budget alerts scoped to preview tags
Tag every preview resource with environment type, PR number and owner, including resources created outside the cluster such as managed databases, buckets and DNS records. Set a budget alert on that tag alone. Spend in a shared account hides preview growth, and resources created outside the cluster are where orphans usually build up.
A live-environment dashboard
List every running preview with its age, owner, PR state and last deploy time. An environment that is 20 days old and tied to a merged PR is an orphan. You can spot it in seconds instead of finding it on the next invoice.
These controls have a trade-off. If queues are long or quotas are too tight, engineers will request long-lived personal environments, which cost more and are rarely cleaned up. Review queue wait times along with spend.
apiVersion: v1
kind: Namespace
metadata:
name: preview-pr-1234
labels:
env-type: preview
pr-number: "1234"
owner: team-checkout
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: preview-quota
namespace: preview-pr-1234
spec:
hard:
requests.cpu: "2"
requests.memory: 4Gi
limits.cpu: "4"
limits.memory: 8Gi
pods: "20"
persistentvolumeclaims: "2"
requests.storage: 20Gi
services.loadbalancers: "0" # expose previews through the shared ingress instead
---
apiVersion: v1
kind: LimitRange
metadata:
name: preview-defaults
namespace: preview-pr-1234
spec:
limits:
- type: Container
defaultRequest:
cpu: 100m
memory: 128Mi
default:
cpu: 500m
memory: 512Mi
A per-PR Kubernetes namespace labeled with environment type, PR number and owner, capped by a ResourceQuota on CPU, memory, pods, storage and load balancers, with a LimitRange that bounds containers that set no requests or limits.
How can per-PR environments save engineering time?
Cloud spend is the visible cost. Engineering time is usually the larger one, and that is where previews pay off when they pay off at all. The time savings come from a few specific places.
- No staging queue. With one shared staging environment, engineers wait for a slot, coordinate in chat about who is deploying, and redeploy after someone else overwrites their branch. Per-PR previews remove that coordination because each change has its own environment.
- Less debugging of other people's changes. When two half-finished branches share staging, a failure could come from either one. Engineers then spend time working out whose change broke what. In a preview, a failure belongs to the PR that produced it.
- Faster review for non-engineers. Product owners, designers and QA can open a URL instead of asking an engineer to check out a branch, run it locally, or record a demo. Feedback arrives while the change is still fresh, not after merge, when rework costs more.
- Earlier integration failures. A preview runs the change against real service wiring, migrations and configuration. Problems that unit tests miss, such as a broken environment variable, a failing migration or a misrouted path, show up before merge instead of in staging or production.
- Cheaper reverts. Catching a bad change before merge avoids the revert, the follow-up PR, and the second review cycle.
Illustrative estimate (assumed numbers, not a measurement). Assume a team of 20 engineers, each losing 30 minutes a week to staging coordination and to debugging collisions on shared staging. That adds up to 10 engineer-hours a week, or roughly 40 a month. Put that next to the cloud estimate above. At an assumed loaded cost of $100 per engineer-hour, 40 hours is about $4,000 a month. That exceeds the $528 to $792 compute bill for a well-tuned setup, but it is less than the $8,760 always-on bill. Subtract the time spent maintaining the preview platform, and the conclusion depends on your actual inputs. Measure the inputs before you commit: how often staging blocks a merge, and how often reviewers open the preview URL.
The time savings disappear when previews are unreliable. A preview that takes ten minutes to wake, fails for reasons unrelated to the PR, or has data too thin to exercise the change sends engineers back to local setups and staging. They then pay for both systems.
Who should use per-PR preview environments?
Per-PR previews fit teams where contention and review latency cost more than the platform does to run. The common signs:
- Many concurrent PRs on a shared codebase. Several teams merging into the same services, with staging regularly occupied or broken by someone else's branch.
- Reviewers who aren't engineers. Product, design, QA or support staff need to see a change running before approving it. Frontend-heavy products and customer-facing workflows benefit most.
- Changes that need a running system to validate. Multi-service changes, API contract changes, and UI flows that are hard to judge from a diff.
- A platform team or clear owner. Someone owns the controller, cleanup jobs, DNS, TLS and seed data, and treats preview reliability as a supported service.
- Infrastructure already defined as code. If the stack can't be stood up from scratch automatically, per-PR copies will be fragile. Fix reproducibility first.
They fit poorly for small teams with low PR volume, for systems that depend on hardware or licensed software that can't be duplicated cheaply, and for workloads where realistic behavior requires production-scale data that can't be masked safely. For those teams, the alternatives below are usually better.
When should you avoid per-PR environments, and what are the alternatives?
Per-PR previews are a platform you have to run. Someone has to own the controller or pipeline that creates stacks, the cleanup jobs that delete them, wildcard DNS, certificate issuance, seed data, and the support requests when a preview breaks for reasons unrelated to the PR. That work does not shrink as the team shrinks. A team of five that opens a handful of PRs a week will spend more engineering hours maintaining preview automation than it saves in cloud spend or review time. The savings depend on volume: many concurrent PRs, reviewers who actually open the preview URL, and changes that conflict on a shared environment.
For small teams, one shared staging environment is usually the better choice. Deploy the merged branch or a short-lived queue of candidates, and settle collisions in chat. A cheaper middle option is selective on-demand previews. They run only when a PR has a label or touches specific paths, such as frontend or API contracts. This uses the same automation but cuts the number of stacks and makes each one deliberate.
| Approach | Cloud cost | Isolation | Maintenance effort |
|---|---|---|---|
| Per-PR previews (every PR) | Highest, scales with PR volume | Full, per change | High: controller, TTLs, DNS, TLS |
| Selective on-demand previews | Moderate, scales with opt-ins | Full, for chosen PRs | High setup, lower ongoing load |
| Shared staging | Fixed, one stack | None between changes | Low |
| Local environments | Near zero | Per developer, but not shareable | Medium: keeping parity with production |
A good starting point is shared staging plus local environments. Move to selective previews when staging contention regularly blocks merges.
How OneEnv can help
Disclosure: OneEnv is the product behind this blog.
OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.
Applied to ephemeral preview environments cost, this is where it helps:
Saves time
- Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
- One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
- Reviewers see the exact diff and the list of affected services up front, so approval is a decision rather than an investigation.
Saves cost
- Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
- Less duplicated configuration to maintain: one shared definition inherited by many services.
- Per-service approve/reject means a bad shared value can be refused where it would break something, before it is live.
- OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.
These are qualitative benefits, not measured results — how much you save depends on how many services share the value.
Want to try it? Start at oneenv.io.