FastAPI Microservice Patterns: Externalized Configuration
Declarative configuration of microservices made easy

What’s the problem?
Often service need to be run in different environments. The most obvious example w.r.t. the service development lifecycle which implies different environments development, testing and production. But in case services are deployed on-premise for different customers there is a need for individually configured services. This implies the need for environment specific configuration, e.g. secret keys, database credentials, etc. Having to change the service for every environment has a lot of disadvantages. How to enable a service to run in multiple environments without modification?
The solution!
The Externalized configuration pattern solves the problem. “Externalize all application configuration including the database credentials and network location. On startup, a service reads the configuration from an external source, e.g. OS environment variables, etc.”. For comparison the Spring Boot framework supports externalized configuration allows to read configuration from different sources and to override potentially previously set configuration values dependent on the read order. Thankfully FastAPI provides builtin support for externalized configuration as well.
Solution alternatives
There are several ways one can use to inject configuration into a FastAPI application. When developing cloud-native applications which shall be run using Kubernetes one has to consider the options given here as well. Again, there are several ways one can inject data into an application using Kubernetes. W.r.t. application management it’s a good idea to enable “declarative configuration”. This means one should avoid configuration files and use Kubernetes builtin capabilities instead. For environment variables other than secrets one can define environment variables for containers. For secret keys one should use Secret
s instead.
Pattern implementation
The post FastAPI Microservice Patterns: Local Development Environment — Skaffold, docker, kubectl and minikube in a nutshell describes how to setup the local development environment and how to get the source code of the pattern implementations. The sub directory externalized-configuration
of the source code repository contains the example implementation of the pattern.
In the minimal example we use pydantic.BaseSettings
to define a default string for a settings variable settings.string_to_display
which will be overridden in case there will be defined an environment variable STRING_TO_DISPLAY
.
from fastapi import FastAPI
from pydantic import BaseSettings
class Settings(BaseSettings):
string_to_display: str = "World"
settings = Settings()
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": settings.string_to_display}
In the container section of the Kubernetes deployment file deployment.yaml
we are overriding the environment variable:
(...)apiVersion: apps/v1
kind: Deployment
spec:
containers:
- name: fastapi-service
image: fastapi-service
ports:
- containerPort: 80
env:
- name: STRING_TO_DISPLAY
value: "Another World"
If we visit the root endpoint via localhost:9000
we’ll recognize that the default string World
has been overridden with Another World
:

The post series content
- FastAPI Microservice Patterns
- FastAPI Microservice Patterns: Local Development Environment
- FastAPI Microservice Patterns: Service discovery in container orchestration platforms
- FastAPI Microservice Patterns: Asynchronnous communication
- FastAPI Microservice Patterns: Application monitoring
- FastAPI Microservice Patterns: Serverless Deployment
- FastAPI Microservice Patterns: Externalized configuration