How to Build a Multi-Cloud Config Loader with Provider Switching in Python
Load cloud provider configurations (AWS, Azure, GCP) from JSON files using a provider dispatch pattern in Python.
Python code
57 linesimport json
from pathlib import Path
from dataclasses import dataclass
from typing import Dict, Any
@dataclass
class CloudConfig:
provider: str
region: str
settings: Dict[str, Any]
class ConfigLoader:
def __init__(self, config_dir: str = "configs"):
self.config_dir = Path(config_dir)
self._providers = {
"aws": self._load_aws,
"azure": self._load_azure,
"gcp": self._load_gcp,
}
def _load_aws(self, data: Dict[str, Any]) -> CloudConfig:
return CloudConfig(
provider="aws",
region=data.get("region", "us-east-1"),
settings={"access_key": data.get("access_key"), "bucket": data.get("bucket")},
)
def _load_azure(self, data: Dict[str, Any]) -> CloudConfig:
return CloudConfig(
provider="azure",
region=data.get("location", "eastus"),
settings={"subscription_id": data.get("subscription_id"), "resource_group": data.get("resource_group")},
)
def _load_gcp(self, data: Dict[str, Any]) -> CloudConfig:
return CloudConfig(
provider="gcp",
region=data.get("zone", "us-central1-a"),
settings={"project_id": data.get("project_id"), "cluster": data.get("cluster")},
)
def load(self, provider: str) -> CloudConfig:
if provider not in self._providers:
raise ValueError(f"Unsupported provider: {provider}")
file_path = self.config_dir / f"{provider}.json"
with open(file_path) as f:
data = json.load(f)
return self._providers[provider](data)
if __name__ == "__main__":
loader = ConfigLoader()
for provider in ["aws", "azure", "gcp"]:
config = loader.load(provider)
print(f"{config.provider}: {config.region} | {config.settings}")
Output
aws: us-east-1 | {'access_key': 'AKIAEXAMPLE', 'bucket': 'my-bucket'}
azure: eastus | {'subscription_id': 'sub-123', 'resource_group': 'rg-dev'}
gcp: us-central1-a | {'project_id': 'my-project', 'cluster': 'prod-cluster'}
How it works
The ConfigLoader uses a dictionary mapping provider names to private loader methods, enabling clean dispatch without if-elif chains. Each loader method extracts provider-specific fields (like access_key for AWS, location for Azure, zone for GCP) and normalizes them into a uniform CloudConfig dataclass. The load method validates the provider, reads the corresponding JSON file from the config directory, and calls the mapped function. This pattern makes adding a new provider straightforward: just add a file and a method. It centralizes configuration parsing in one place, improving maintainability and testability.
Common mistakes
- Forgetting to create the JSON files in the configs directory, causing FileNotFoundError.
- Mixing up provider-specific keys (e.g., using 'region' for Azure instead of 'location').
- Not validating the provider name before file access, which can leave leftover state if errors occur.
Variations
- Use a factory function or enum to map providers instead of a dictionary of methods.
- Load all configs at startup into a cache to avoid repeated file I/O.
Real-world use cases
- CI/CD pipelines that deploy infrastructure across multiple clouds by reading provider-specific settings at build time.
- Multi-cloud management tools that need to normalize credentials and regions for cost or resource tracking.
- Serverless functions that switch between cloud backends based on environment or tenant configuration.
Sponsored
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Create a Data Helper Class for Beginners in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
Keep learning
Related tutorials and quizzes for this topic.