DEV Community

Michael Di Prisco
Michael Di Prisco

Posted on

Config - The Twelve Factor App Methodology

Store config in the environment

Welcome back to our journey through the Twelve Factors in Software Development. In this installment, we'll unravel Factor 3: Config. Config, short for configuration, plays a pivotal role in making your application adaptable and portable across various environments.

Config: Keep Settings Separate from Code

The Config factor advocates for storing configuration settings separately from your code. Configuration includes variables such as API keys, database connection strings, and other settings that might vary between different deployment environments.

Why It Matters

Separating configuration from code enhances flexibility. It allows you to change settings without modifying the codebase. This is particularly crucial when deploying an application in different environments, such as development, testing, and production.

How to Implement

Use environment variables to store configuration settings. Modern platforms and cloud services provide a way to manage these variables easily. For instance, in a Node.js application, you might use the dotenv package:

// Load environment variables from a .env file

// Access the API key
const apiKey = process.env.API_KEY;
Enter fullscreen mode Exit fullscreen mode

This way, your application can adapt to various environments by adjusting the environment variables.

Example in Action

Consider an application that connects to a database. Instead of hardcoding the database connection string in your code, store it as an environment variable. This ensures that when the application moves from a development environment to production, the database connection can be easily configured without touching the code.

// Database connection using environment variable
const dbConnection = process.env.DATABASE_CONNECTION_STRING;
Enter fullscreen mode Exit fullscreen mode

By following the Config factor, you enhance the maintainability and security of your application, as sensitive information remains separate from the codebase.

Stay tuned for Factor 4: Backing Services, where we'll explore the importance of treating backing services (databases, caches, etc.) as attached resources and delve into best practices for connecting to them.

Top comments (0)