The third factor of the Twelve-Factor App methodology emphasizes storing configurations in the environment, rather than in the 3code.
Why Store Config in the Environment?
Storing config in the environment means keeping all the settings that vary between deployments (e.g., database credentials, API keys) separate from the code.
- Security: Keeps sensitive information out of the codebase.
- Flexibility: Allows for easy adjustment of configurations for different environments (e.g., development, staging, production).
- Maintainability: Simplifies code updates without affecting configurations.
How to Store Config in the Environment
Environment variables are a universal method for managing configuration across different systems.
Setting environment variables in a Unix-based system:
In the application, read the variable:
import os database_url = os.environ['DATABASE_URL']
Configuration Management Tools
Tools like Ansible, Puppet, or Kubernetes ConfigMaps can manage environment-specific configurations.
Example: Kubernetes ConfigMap
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: DATABASE_URL: "postgresql://user:pass@localhost:5432/mydb"
Deployment Strategies with Config
Configurations must be handled separately for each environment.
- Development Environment: Use local configurations.
- Staging Environment: Apply configurations that mirror production.
- Production Environment: Apply production-specific configurations.
This approach aligns with the principles of continuous deployment and facilitates the management of applications across various environments.