Configuration across environments

Cloud, deploys and infrastructure Junior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

What does startup print when DATABASE_URL is missing?

Python 3.13
import os


def load() -> str:
    url = os.environ.get("DATABASE_URL_DEMO_UNSET")
    if not url:
        raise SystemExit("config error: DATABASE_URL missing")
    return url


try:
    load()
except SystemExit as e:
    print(e)

Options
Hint 1 of 1

What does os.environ.get return for a missing key?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A config error: DATABASE_URL missing

Correct. It fails at startup with a clear message.

The app reads config from the environment and refuses to start when a required value is missing. Failing loudly at startup beats failing on the first request with an obscure error.

Why not the others?
  • B None. The function raises before returning.
  • C A KeyError. .get returns None instead of raising.
  • D Nothing. The SystemExit is caught and printed.

30-second answer

Build one artifact and configure it per environment at runtime, following the twelve-factor idea of config in the environment: URLs, feature settings and credentials come from environment variables or mounted files, never from code branches like if env == 'prod'. Validate all configuration at startup and fail fast with a clear error, so a missing variable doesn't surface as a strange bug hours later.

Practise saying it · 25 s. No recording, just a timer

Config per environment.

Say your answer out loud, then check which of these points you covered:

  • One artifact
  • Config at runtime
  • Validate at startup
  • Secrets from a manager

Deep dive

Typed settings classes (pydantic-settings) parse and validate config.

Secrets come from a secrets manager, not plain config files in git.

Keep environment differences to config, not code paths.

Log the effective non-secret config at startup.

Common mistakes

  • Different builds per environment.
  • Defaults that silently point at production.
  • Reading config lazily deep inside request handlers.

Senior take & follow-ups

One artifact, runtime config, validated at startup, secrets separate.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why fail fast?

    Show hint

    A bad config should stop a deploy, not break requests later.

  2. Why one artifact?

    Show hint

    What you tested is exactly what you ship.

Suggest a fix if something here is wrong or out of date.