21 Configure securely and deploy a verified release
An artifact identifies code and runtime dependencies. Configuration states how that code should behave in an environment. A secret grants capability and needs restricted delivery and rotation. Keep their ownership separate.
Type: how-to procedure with acceptance gates. Prerequisites: a provisioned target, requirements, tested application and recovery access. Environments: local reference lab first; an approved staging target before any public rollout. Outcome: release a specific artifact with explicit configuration and permission boundaries. Evidence: release identity, denied operations, successful workflow and a tested compatible recovery path.
Separate artifact, configuration and secrets
An artifact identifies code and runtime dependencies. Configuration states how that code should behave in an environment. A secret grants capability and needs restricted delivery and rotation. Keep their ownership separate. Rebuilding an image for each environment can change its contents; embedding production credentials in a shared image spreads those credentials to every person who can pull it.
Record the source revision, dependency lock hash, built artifact hash/digest, schema version and configuration revision. A human-friendly tag is a label, not an immutable identity. Retain a previous compatible artifact before replacing the current one. For a database-backed service, compatibility includes schema and data expectations, not merely whether an old process starts.
The reference application generates local secrets through setup_lab.py and stores only digests of high-entropy service tokens in the database. Configuration must be private even if the application hashes presented tokens: an operator file can still contain the original usable secret. Do not show expanded Compose configuration, environment dumps or command tracing in shared proof.
Establish identities and least privilege
List human administrators, deployment identities, runtime services, monitoring clients, backup actors and recovery actors. Give each the minimum capability required and record how access is revoked. A worker needs a database runtime credential; it does not need the initial tenant-admin token. A migration job needs schema ownership; request handlers should not carry that permission.
The application separates lab_owner migrations from lab_app runtime data operations. The PostgreSQL drill tests that runtime CREATE TABLE fails. This boundary reduces accidental damage but does not provide tenant isolation against direct database access. A stolen runtime credential remains valuable; network isolation and secret protection are essential. Use additional isolation controls where the threat model requires them.
For human administration, require a maintained identity approach suitable for the target, strong authentication and emergency recovery access. The teaching API login has defined limitations in Chapter 15. Before adapting it for customers, decide MFA, recovery, federation, account disable, session expiration and auditing requirements. A temporary classroom password flow should not become an undocumented production identity product.
Run the local application contract
From a fresh reference example directory, follow Chapter 3's initialization. If .env already exists, inspect ownership and purpose rather than deleting it to make setup succeed. Use the supported verification command:
Expected results include lint/type checks, behavioral tests, coverage and dependency-audit output. Read VERIFICATION.md for the actual recorded scope and versions. A successful audit means no known issues were reported by that tool at that time; it is not evidence that the application has no vulnerabilities.
Exercise both allowed and denied behavior. Missing credentials must fail; a valid credential with insufficient scope must fail; another tenant's object must remain inaccessible; invalid input must not enter the durable queue; duplicate requests must follow the idempotency contract. Check the returned status and body, not only the fact that a TCP connection opened.
Treat migration as a release operation
Before migration, prove a backup can restore representative data into a separate target. Inspect the actual current schema revision, run the candidate migration on a staging copy and measure locks/runtime. For the teaching v2-to-v5 lineage, follow the explicit baseline/adoption instructions in Chapter 15. Stamping an arbitrary database does not make its schema correct.
The supplied maintenance-release helper stops and drains API/worker writers before schema changes, then runs controlled initialization and restarts the application. It refuses remote Docker target overrides and reports its selected local context. This is planned downtime. If a migration fails, do not restart arbitrary old code against a partially changed database. Preserve the evidence, inspect the actual schema state and choose the rehearsed roll-forward or separate-target restore route.
A zero-downtime requirement needs a different migration design: expand the schema compatibly, support old and new versions during overlap, backfill with resumable bounded work, switch reads/writes deliberately, then remove old structures only after verifying no dependent release remains. That requires workload-specific tests; it cannot be enabled by adding a “rolling” flag alone.
Go live through independent layers
A staged release passes local readiness first. Then verify its dependency path, authorized workflow and security denials. Configure the public edge only after the service behaves correctly privately. DNS must identify the intended edge; TLS must validate the actual hostname; the edge must reach the intended upstream; that upstream must enforce permission and input contracts.
For a chosen hostname, an external client can check a health endpoint using the actual URL:
Replace the hostname with a domain you control; do not treat this placeholder as an executable deployment. Expect a valid certificate without disabling verification and the intended service response. Repeat with the complete authorized synthetic workflow. Test unauthorized access and the intended absence of externally reachable internal ports using only your approved target.
The local lifecycle lab exercises separate release files, candidate health, traffic selection and rollback. Its scope is the owned local services described by its receipt. The broader application TLS/monitoring drill adds real loopback TLS and alert delivery. Public DNS, certificate issuance and an external client path remain distinct acceptance tasks.
Contain a failed release
Set stop conditions before rollout: unavailable readiness, authorization regression, unacceptable error rate, queue growth, failed migrations or a broken core workflow. Limit the initial audience and observation window according to the actual deployment contract. Keep the rollback decision owner available. Automatic retries of a deployment can amplify a repeated bad change.
If the candidate is not ready, keep it out of traffic. If it fails after selection and the previous version remains schema-compatible, restore the previous routing/artifact and verify the user workflow. If the failure includes corrupted data or irreversible external effects, application rollback alone is insufficient. Use the incident procedure and reconcile those effects before replaying work.
Record what happened: candidate/previous identities, target, migration state, failed symptom, recovery action, service result and any confirmed data loss. Do not hide a failed attempt by replacing its receipt with a later success. Both results explain the release's risk and recovery behavior.
Cleanup: remove failed candidate processes only after collecting necessary evidence; retain the compatible previous artifact according to release policy. Pass: a client outside the target can complete the required workflow securely, internal services remain private, and a controlled bad release is contained with a verified recovery. Pending: any public account, DNS or paid resource step that lacks a concrete authorized target.