# Development: store in .env.local
echo "VAST_API_KEY=dev_key_abc123" >> .env.local
echo "VAST_MAX_PRICE_PER_HR=0.40" >> .env.local # max $/hr for dev instances
# Staging: GitHub Actions environment secrets
# Add VAST_API_KEY_STAGING to staging environment
# Production: AWS Secrets Manager or similar
aws secretsmanager create-secret \
--name vast/production/api-key \
--secret-string "prod_key_xyz789"
# config/vastai.py
import os
from dataclasses import dataclass
@dataclass
class VastAIConfig:
api_key: str
max_price_per_hr: float # maximum $/hr to spend on an instance
allowed_gpu_types: list[str] # GPU whitelist
max_instances: int
spot_only: bool # restrict to interruptible spot instances
ENV_CONFIGS = {
"development": VastAIConfig(
api_key=os.environ.get("VAST_API_KEY", ""),
max_price_per_hr=0.40, # RTX 3090 range
allowed_gpu_types=["RTX_3090", "RTX_4090", "RTX_3080"],
max_instances=2,
spot_only=True, # spot only in dev to minimize cost
),
"staging": VastAIConfig(
api_key=os.environ.get("VAST_API_KEY_STAGING", ""),
max_price_per_hr=1.00,
allowed_gpu_types=["RTX_4090", "A6000", "A100_PCIE_40GB"],
max_instances=5,
spot_only=False,
),
"production": VastAIConfig(
api_key=os.environ.get("VAST_API_KEY_PROD", ""),
max_price_per_hr=4.00, # up to H100 range
allowed_gpu_types=["A100_SXM4_80GB", "H100_SXM5_80GB", "A100_PCIE_40GB"],
max_instances=20,
spot_only=False,
),
}
def get_vastai_config() -> VastAIConfig:
env = os.environ.get("APP_ENV", "development")
config = ENV_CONFIGS.get(env, ENV_CONFIGS["development"])
if not config.api_key:
raise ValueError(f"VAST_API_KEY not configured for {env}")
return config
# lib/vastai_service.py
import vastai
from config.vastai import get_vastai_config
def find_cheapest_valid_instance(gpu_count: int = 1):
"""Find cheapest instance matching environment policy."""
cfg = get_vastai_config()
vastai.api_key = cfg.api_key
# Build search query respecting environment policy
gpu_filter = " || ".join(f"gpu_name={g}" for g in cfg.allowed_gpu_types)
query = f"({gpu_filter}) num_gpus={gpu_count} dph_total<{cfg.max_price_per_hr}"
if cfg.spot_only:
query += " reliability>0.9" # spot instances with high reliability
offers = vastai.search_offers(query, order="dph_total", limit=5)
if not offers:
raise ValueError(f"No instances matching policy under ${cfg.max_price_per_hr}/hr")
return offers[0]
# scripts/validate-vastai-env.py
from config.vastai import get_vastai_config
cfg = get_vastai_config()
print(f"Environment: {os.environ.get('APP_ENV', 'development')}")
print(f"Max price/hr: ${cfg.max_price_per_hr}")
print(f"Allowed GPUs: {', '.join(cfg.allowed_gpu_types)}")
print(f"Max instances: {cfg.max_instances}")
print(f"Spot only: {cfg.spot_only}")
# Verify key is valid
import vastai
vastai.api_key = cfg.api_key