# Managing Multiple Cloud Providers

Strategies for infrastructure spanning AWS, UpCloud, Hetzner, and more.

## Multi-Cloud Architecture

Distribute workloads across providers:

```nickel
{
  infrastructure = {
    # Primary: AWS
    primary_region = {
      provider = "aws",
      region = "us-east-1",
      weight = 50,

      services = {
        web = { servers = 5, cpu = 2, memory = 4 },
        api = { servers = 3, cpu = 4, memory = 8 },
        database = { engine = "postgres", replicas = 2 }
      }
    },

    # Secondary: UpCloud
    secondary_region = {
      provider = "upcloud",
      zone = "us-sjc",
      weight = 30,

      services = {
        web = { servers = 3, plan = "2xCPU-4GB" },
        cache = { engine = "redis" }
      }
    },

    # Backup: Hetzner
    backup_region = {
      provider = "hetzner",
      location = "fsn1",
      weight = 20,

      services = {
        backup = { storage_gb = 1000 }
      }
    }
  }
}
```

## Cost Optimization Across Clouds

Compare and optimize costs:

```bash
#!/usr/bin/env nu

def compare-cloud-costs [config] {
  let providers = ["aws", "upcloud", "hetzner"]

  print "Cost Comparison:"
  $providers | each { | provider |
    let cost = provisioning cost estimate --config $config --provider $provider
    print $"  ($provider): ($cost) per month"
  }
}
```

## Failover Strategy

Automatic failover between clouds:

```nickel
{
  failover = {
    enabled = true,
    primary = {
      provider = "aws",
      health_check_interval = 30,
      failure_threshold = 3
    },

    secondary = {
      provider = "upcloud",
      auto_promote = true
    },

    actions = [
      {
        trigger = "primary_unhealthy",
        action = "failover_to_secondary",
        notify = "true"
      },
      {
        trigger = "primary_recovered",
        action = "failback_to_primary",
        gradual = true,
        percentage = 10  # 10% at a time
      }
    ]
  }
}
```

## Data Replication

Synchronize data across clouds:

```bash
#!/usr/bin/env nu

def setup-cross-cloud-replication [] {
  # Primary database in AWS
  provisioning database create \
    --provider aws \
    --name primary-db \
    --replication enabled

  # Read replica in UpCloud
  provisioning database create-read-replica \
    --source primary-db \
    --provider upcloud \
    --zone us-sjc

  # Read replica in Hetzner
  provisioning database create-read-replica \
    --source primary-db \
    --provider hetzner \
    --location fsn1

  print "Cross-cloud database replication configured"
}
```

## Related Documentation

- [Multi-Cloud Examples](../examples/multi-cloud.md)
- [Provider Guides](../infrastructure/provider-guides.md)
- [Disaster Recovery](./disaster-recovery.md)
