From 5d522922ee777d1394fdb67cadd2d8303b8e399f Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Fri, 7 Aug 2026 18:48:08 -0700 Subject: [PATCH] doc: create terraform for DigitalOcean --- digitalocean/README.md | 170 ++++++++++++++++++++++++++ digitalocean/byo-project/app.tf | 149 ++++++++++++++++++++++ digitalocean/byo-project/cache.tf | 22 ++++ digitalocean/byo-project/database.tf | 32 +++++ digitalocean/byo-project/dns.tf | 16 +++ digitalocean/byo-project/main.tf | 13 ++ digitalocean/byo-project/outputs.tf | 69 +++++++++++ digitalocean/byo-project/storage.tf | 16 +++ digitalocean/byo-project/variables.tf | 62 ++++++++++ digitalocean/byo-project/vpc.tf | 5 + digitalocean/main.tf | 27 ++++ digitalocean/outputs.tf | 54 ++++++++ digitalocean/variables.tf | 90 ++++++++++++++ 13 files changed, 725 insertions(+) create mode 100644 digitalocean/README.md create mode 100644 digitalocean/byo-project/app.tf create mode 100644 digitalocean/byo-project/cache.tf create mode 100644 digitalocean/byo-project/database.tf create mode 100644 digitalocean/byo-project/dns.tf create mode 100644 digitalocean/byo-project/main.tf create mode 100644 digitalocean/byo-project/outputs.tf create mode 100644 digitalocean/byo-project/storage.tf create mode 100644 digitalocean/byo-project/variables.tf create mode 100644 digitalocean/byo-project/vpc.tf create mode 100644 digitalocean/main.tf create mode 100644 digitalocean/outputs.tf create mode 100644 digitalocean/variables.tf diff --git a/digitalocean/README.md b/digitalocean/README.md new file mode 100644 index 0000000..6d59a8b --- /dev/null +++ b/digitalocean/README.md @@ -0,0 +1,170 @@ +# Terraform DigitalOcean Fleet Deployment + +This Terraform project automates the deployment of Fleet Device Management (Fleet) on DigitalOcean. It provisions all infrastructure components using DigitalOcean's managed services: + +* **App Platform** — Runs the Fleet application container (equivalent to AWS ECS or GCP Cloud Run) +* **Managed MySQL** — Database for Fleet (equivalent to AWS Aurora or GCP Cloud SQL) +* **Managed Valkey** — Redis-compatible cache (equivalent to AWS ElastiCache or GCP Memorystore) +* **Spaces** — S3-compatible object storage for software installers (equivalent to AWS S3 or GCP GCS) +* **VPC** — Private networking between all resources +* **DNS** — Domain and CNAME record for Fleet +* **Database Firewalls** — Restricts database and cache access to the App Platform app only + +## Prerequisites + +1. **Terraform:** Version `~> 1.11`. Install from [terraform.io](https://www.terraform.io/downloads.html). +2. **DigitalOcean Account:** With a payment method configured. +3. **DigitalOcean API Token:** Set as the `DIGITALOCEAN_TOKEN` environment variable. + ```bash + export DIGITALOCEAN_TOKEN="dop_v1_your_token_here" + ``` +4. **DigitalOcean Spaces Keys:** Set as environment variables. + ```bash + export SPACES_ACCESS_KEY_ID="your_spaces_key_id" + export SPACES_SECRET_ACCESS_KEY="your_spaces_secret_key" + ``` +5. **Registered Domain Name:** You need a domain whose DNS can be delegated to DigitalOcean's name servers. + +## Configuration + +Create a `terraform.tfvars` file: + +```hcl +# Required +domain_name = "fleet.your-domain.com" + +# Optional overrides +region = "nyc3" # Default: nyc3 + +# Fleet configuration +fleet_config = { + image_tag = "fleetdm/fleet:v4.90.0" + instance_size_slug = "apps-s-1vcpu-1gb" + instance_count = 1 + debug_logging = false + exec_migration = true + # license_key = "YOUR_FLEET_LICENSE_KEY" # Optional + extra_env_vars = {} +} + +# Database configuration +database_config = { + name = "fleet-mysql" + engine = "mysql" + version = "8" + size = "db-s-1vcpu-1gb" + node_count = 1 + database_name = "fleet" + database_user = "fleet" +} + +# Cache configuration +cache_config = { + name = "fleet-cache" + engine = "valkey" + version = "8" + size = "db-s-1vcpu-1gb" + node_count = 1 +} +``` + +## Deployment Steps + +1. **Set environment variables:** + ```bash + export DIGITALOCEAN_TOKEN="dop_v1_your_token_here" + export SPACES_ACCESS_KEY_ID="your_spaces_key_id" + export SPACES_SECRET_ACCESS_KEY="your_spaces_secret_key" + ``` + +2. **Initialize Terraform:** + ```bash + terraform init + ``` + +3. **Plan the deployment:** + ```bash + terraform plan -out=tfplan + ``` + +4. **Apply the configuration:** + ```bash + terraform apply tfplan + ``` + +5. **Delegate DNS:** + If your domain's DNS is managed elsewhere, update the name servers at your registrar to: + - `ns1.digitalocean.com` + - `ns2.digitalocean.com` + - `ns3.digitalocean.com` + +## Architecture + +```mermaid +graph TD + subgraph External + Internet[(Internet)] + Users[Web Console / fleetd agent] + GitHub[(GitHub - Vulnerability Resources)] + end + + subgraph "DigitalOcean" + subgraph VPC [VPC] + direction LR + subgraph PublicFacing [Public Zone] + AppPlatform[App Platform: Fleet] + end + subgraph PrivateZone [Private Zone] + MySQL[(Managed MySQL)] + Valkey[(Managed Valkey)] + Spaces[(Spaces Bucket)] + end + + AppPlatform --> MySQL + AppPlatform --> Valkey + AppPlatform --> Spaces + end + end + + Users -- "fleet.yourdomain.com" --> Internet + Internet -- "fleet.yourdomain.com" --> AppPlatform + AppPlatform -- "outbound" --> GitHub +``` + +## Key Differences from AWS/GCP Modules + +| Feature | AWS | GCP | DigitalOcean | +|---------|-----|-----|--------------| +| Compute | ECS Fargate | Cloud Run | App Platform | +| Database | Aurora MySQL | Cloud SQL MySQL | Managed MySQL | +| Cache | ElastiCache | Memorystore | Managed Valkey | +| Storage | S3 | GCS | Spaces | +| Networking | VPC + ALB | VPC + LB | VPC + built-in TLS | +| DNS | Route53 | Cloud DNS | DigitalOcean DNS | +| TLS | ACM | Managed SSL | Automatic (Let's Encrypt) | + +## Cost Estimate + +| Resource | Size | Monthly Cost | +|----------|------|-------------| +| App Platform | apps-s-1vcpu-1gb × 1 | ~$12 | +| Managed MySQL | db-s-1vcpu-1gb × 1 | ~$15 | +| Managed Valkey | db-s-1vcpu-1gb × 1 | ~$15 | +| Spaces | 250 GB included | ~$5 | +| VPC | Free | $0 | +| DNS | Free | $0 | +| **Total** | | **~$47/month** | + +## Cleaning Up + +```bash +terraform destroy +``` + +## Important Considerations + +* **Spaces Keys:** The `SPACES_ACCESS_KEY_ID` and `SPACES_SECRET_ACCESS_KEY` environment variables are needed by the Terraform provider to manage Spaces buckets. These are separate from the API token. Generate them at [DigitalOcean Spaces Keys](https://cloud.digitalocean.com/spaces/access_keys). +* **TLS:** App Platform automatically provisions and renews Let's Encrypt certificates for custom domains. +* **Database Security:** Database firewalls restrict connections to only the App Platform app. No public access is allowed. +* **Migration:** The `PRE_DEPLOY` job runs `fleet prepare db` before each deployment when `exec_migration = true`. +* **Scaling:** Adjust `instance_size_slug` and `instance_count` in `fleet_config` for horizontal/vertical scaling. diff --git a/digitalocean/byo-project/app.tf b/digitalocean/byo-project/app.tf new file mode 100644 index 0000000..16d55ab --- /dev/null +++ b/digitalocean/byo-project/app.tf @@ -0,0 +1,149 @@ +resource "random_password" "private_key" { + length = 32 + special = false +} + +locals { + fleet_env_vars = merge(var.fleet_config.extra_env_vars, { + FLEET_MYSQL_PROTOCOL = "tcp" + FLEET_MYSQL_ADDRESS = "${digitalocean_database_cluster.mysql.host}:${digitalocean_database_cluster.mysql.port}" + FLEET_MYSQL_USERNAME = digitalocean_database_user.fleet.name + FLEET_MYSQL_DATABASE = digitalocean_database_db.fleet.name + FLEET_MYSQL_PASSWORD = digitalocean_database_user.fleet.password + + FLEET_REDIS_ADDRESS = "${digitalocean_database_cluster.cache.host}:${digitalocean_database_cluster.cache.port}" + FLEET_REDIS_USE_TLS = "true" + FLEET_REDIS_PASSWORD = digitalocean_database_cluster.cache.password + + FLEET_SERVER_PRIVATE_KEY = random_password.private_key.result + + FLEET_S3_SOFTWARE_INSTALLERS_BUCKET = digitalocean_spaces_bucket.software_installers.name + FLEET_S3_SOFTWARE_INSTALLERS_ENDPOINT_URL = "https://${digitalocean_spaces_bucket.software_installers.endpoint}" + FLEET_S3_SOFTWARE_INSTALLERS_REGION = var.region + FLEET_S3_SOFTWARE_INSTALLERS_FORCE_S3_PATH_STYLE = "false" + + FLEET_LOGGING_JSON = "true" + FLEET_LOGGING_DEBUG = tostring(var.fleet_config.debug_logging) + FLEET_SERVER_TLS = "false" + FLEET_SERVER_SERVER = "true" + }) +} + +resource "digitalocean_app" "fleet" { + spec { + name = "${var.prefix}-app" + region = var.region + + # Custom domain + domain { + name = var.domain_name + type = "PRIMARY" + zone = local.zone_name + } + + # VPC + vpc { + id = digitalocean_vpc.fleet.id + } + + # Fleet service + service { + name = "fleet" + instance_count = var.fleet_config.instance_count + instance_size_slug = var.fleet_config.instance_size_slug + http_port = 8080 + + image { + registry_type = "DOCKER_HUB" + registry = "fleetdm" + repository = "fleet" + tag = trimprefix(var.fleet_config.image_tag, "fleetdm/fleet:") + } + + health_check { + http_path = "/healthz" + initial_delay_seconds = 30 + period_seconds = 30 + timeout_seconds = 5 + success_threshold = 1 + failure_threshold = 3 + } + + dynamic "env" { + for_each = local.fleet_env_vars + content { + key = env.key + value = env.value + type = "GENERAL" + scope = "RUN_TIME" + } + } + + # License key as a secret (only if provided) + dynamic "env" { + for_each = var.fleet_config.license_key != null ? { FLEET_LICENSE_KEY = var.fleet_config.license_key } : {} + content { + key = env.key + value = env.value + type = "SECRET" + scope = "RUN_TIME" + } + } + } + + # Migration job — runs before each deployment + dynamic "job" { + for_each = var.fleet_config.exec_migration ? [1] : [] + content { + name = "fleet-migration" + kind = "PRE_DEPLOY" + instance_count = 1 + instance_size_slug = var.fleet_config.instance_size_slug + + image { + registry_type = "DOCKER_HUB" + registry = "fleetdm" + repository = "fleet" + tag = trimprefix(var.fleet_config.image_tag, "fleetdm/fleet:") + } + + run_command = "fleet prepare db --no-prompt=true" + + dynamic "env" { + for_each = local.fleet_env_vars + content { + key = env.key + value = env.value + type = "GENERAL" + scope = "RUN_TIME" + } + } + + dynamic "env" { + for_each = var.fleet_config.license_key != null ? { FLEET_LICENSE_KEY = var.fleet_config.license_key } : {} + content { + key = env.key + value = env.value + type = "SECRET" + scope = "RUN_TIME" + } + } + } + } + + # Deployment alerts + alert { + rule = "DEPLOYMENT_FAILED" + } + + alert { + rule = "DOMAIN_FAILED" + } + } + + depends_on = [ + digitalocean_database_cluster.mysql, + digitalocean_database_cluster.cache, + digitalocean_spaces_bucket.software_installers, + ] +} diff --git a/digitalocean/byo-project/cache.tf b/digitalocean/byo-project/cache.tf new file mode 100644 index 0000000..aa8c35c --- /dev/null +++ b/digitalocean/byo-project/cache.tf @@ -0,0 +1,22 @@ +resource "digitalocean_database_cluster" "cache" { + name = var.cache_config.name + engine = var.cache_config.engine + version = var.cache_config.version + size = var.cache_config.size + region = var.region + node_count = var.cache_config.node_count + + private_network_uuid = digitalocean_vpc.fleet.id + + tags = ["fleet"] +} + +# Allow only the App Platform app to connect to the cache +resource "digitalocean_database_firewall" "cache" { + cluster_id = digitalocean_database_cluster.cache.id + + rule { + type = "app" + value = digitalocean_app.fleet.id + } +} diff --git a/digitalocean/byo-project/database.tf b/digitalocean/byo-project/database.tf new file mode 100644 index 0000000..e1008eb --- /dev/null +++ b/digitalocean/byo-project/database.tf @@ -0,0 +1,32 @@ +resource "digitalocean_database_cluster" "mysql" { + name = var.database_config.name + engine = var.database_config.engine + version = var.database_config.version + size = var.database_config.size + region = var.region + node_count = var.database_config.node_count + + private_network_uuid = digitalocean_vpc.fleet.id + + tags = ["fleet"] +} + +resource "digitalocean_database_db" "fleet" { + cluster_id = digitalocean_database_cluster.mysql.id + name = var.database_config.database_name +} + +resource "digitalocean_database_user" "fleet" { + cluster_id = digitalocean_database_cluster.mysql.id + name = var.database_config.database_user +} + +# Allow only the App Platform app to connect to the database +resource "digitalocean_database_firewall" "mysql" { + cluster_id = digitalocean_database_cluster.mysql.id + + rule { + type = "app" + value = digitalocean_app.fleet.id + } +} diff --git a/digitalocean/byo-project/dns.tf b/digitalocean/byo-project/dns.tf new file mode 100644 index 0000000..46ef6ea --- /dev/null +++ b/digitalocean/byo-project/dns.tf @@ -0,0 +1,16 @@ +# Extract the zone (e.g., "example.com") from the FQDN (e.g., "fleet.example.com") +locals { + zone_name = join(".", slice(split(".", var.domain_name), 1, length(split(".", var.domain_name)))) +} + +resource "digitalocean_domain" "fleet" { + name = local.zone_name +} + +resource "digitalocean_record" "fleet" { + domain = digitalocean_domain.fleet.id + type = "CNAME" + name = split(".", var.domain_name)[0] + value = "${digitalocean_app.fleet.default_ingress}." + ttl = 300 +} diff --git a/digitalocean/byo-project/main.tf b/digitalocean/byo-project/main.tf new file mode 100644 index 0000000..fac3319 --- /dev/null +++ b/digitalocean/byo-project/main.tf @@ -0,0 +1,13 @@ +terraform { + required_version = "~> 1.11" + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = ">= 2.99.0" + } + random = { + source = "hashicorp/random" + version = ">= 3.0" + } + } +} diff --git a/digitalocean/byo-project/outputs.tf b/digitalocean/byo-project/outputs.tf new file mode 100644 index 0000000..5af394b --- /dev/null +++ b/digitalocean/byo-project/outputs.tf @@ -0,0 +1,69 @@ +output "fleet_application_url" { + description = "The primary URL to access the Fleet application." + value = "https://${digitalocean_record.fleet.fqdn}" +} + +output "app_id" { + description = "The ID of the DigitalOcean App Platform app." + value = digitalocean_app.fleet.id +} + +output "app_live_url" { + description = "The live URL assigned by App Platform." + value = digitalocean_app.fleet.live_url +} + +output "app_default_ingress" { + description = "The default ingress URL for the app." + value = digitalocean_app.fleet.default_ingress +} + +output "database_host" { + description = "The hostname of the MySQL database cluster." + value = digitalocean_database_cluster.mysql.host +} + +output "database_port" { + description = "The port of the MySQL database cluster." + value = digitalocean_database_cluster.mysql.port +} + +output "database_user" { + description = "The MySQL database user for Fleet." + value = digitalocean_database_user.fleet.name +} + +output "cache_host" { + description = "The hostname of the Valkey cache cluster." + value = digitalocean_database_cluster.cache.host +} + +output "cache_port" { + description = "The port of the Valkey cache cluster." + value = digitalocean_database_cluster.cache.port +} + +output "spaces_bucket_name" { + description = "The name of the Spaces bucket for Fleet software installers." + value = digitalocean_spaces_bucket.software_installers.name +} + +output "spaces_bucket_endpoint" { + description = "The endpoint of the Spaces bucket." + value = digitalocean_spaces_bucket.software_installers.endpoint +} + +output "vpc_id" { + description = "The ID of the VPC." + value = digitalocean_vpc.fleet.id +} + +output "dns_record_fqdn" { + description = "The FQDN of the DNS record for Fleet." + value = digitalocean_record.fleet.fqdn +} + +output "domain_name_servers" { + description = "The name servers for the DigitalOcean DNS zone. Delegate your domain to these." + value = ["ns1.digitalocean.com", "ns2.digitalocean.com", "ns3.digitalocean.com"] +} diff --git a/digitalocean/byo-project/storage.tf b/digitalocean/byo-project/storage.tf new file mode 100644 index 0000000..280f348 --- /dev/null +++ b/digitalocean/byo-project/storage.tf @@ -0,0 +1,16 @@ +resource "random_id" "bucket_suffix" { + byte_length = 4 +} + +resource "digitalocean_spaces_bucket" "software_installers" { + name = "${var.prefix}-installers-${random_id.bucket_suffix.hex}" + region = var.region + acl = "private" + + lifecycle_rule { + enabled = true + expiration { + days = 90 + } + } +} diff --git a/digitalocean/byo-project/variables.tf b/digitalocean/byo-project/variables.tf new file mode 100644 index 0000000..8883f0b --- /dev/null +++ b/digitalocean/byo-project/variables.tf @@ -0,0 +1,62 @@ +variable "region" { + description = "The DigitalOcean region for all resources." + type = string + default = "nyc3" +} + +variable "prefix" { + description = "A prefix used for naming resources." + type = string + default = "fleet" +} + +variable "domain_name" { + description = "The domain name for Fleet (e.g., 'fleet.example.com')." + type = string +} + +variable "vpc_config" { + description = "Configuration for the DigitalOcean VPC." + type = object({ + name = string + ip_range = string + }) +} + +variable "fleet_config" { + description = "Configuration for the Fleet application deployment." + type = object({ + image_tag = string + instance_size_slug = string + instance_count = number + license_key = optional(string) + debug_logging = bool + exec_migration = bool + extra_env_vars = optional(map(string)) + }) +} + +variable "database_config" { + description = "Configuration for the managed MySQL database cluster." + type = object({ + name = string + engine = string + version = string + size = string + node_count = number + database_name = string + database_user = string + deletion_protection = optional(bool, false) + }) +} + +variable "cache_config" { + description = "Configuration for the managed Valkey (Redis-compatible) cache cluster." + type = object({ + name = string + engine = string + version = string + size = string + node_count = number + }) +} diff --git a/digitalocean/byo-project/vpc.tf b/digitalocean/byo-project/vpc.tf new file mode 100644 index 0000000..173585d --- /dev/null +++ b/digitalocean/byo-project/vpc.tf @@ -0,0 +1,5 @@ +resource "digitalocean_vpc" "fleet" { + name = var.vpc_config.name + region = var.region + ip_range = var.vpc_config.ip_range +} diff --git a/digitalocean/main.tf b/digitalocean/main.tf new file mode 100644 index 0000000..8c84daa --- /dev/null +++ b/digitalocean/main.tf @@ -0,0 +1,27 @@ +terraform { + required_version = "~> 1.11" + required_providers { + digitalocean = { + source = "digitalocean/digitalocean" + version = ">= 2.99.0" + } + } +} + +provider "digitalocean" { + # Uses DIGITALOCEAN_TOKEN environment variable by default. + # Spaces access is configured via SPACES_ACCESS_KEY_ID and + # SPACES_SECRET_ACCESS_KEY environment variables. +} + +module "fleet" { + source = "./byo-project" + + region = var.region + prefix = var.prefix + domain_name = var.domain_name + vpc_config = var.vpc_config + fleet_config = var.fleet_config + database_config = var.database_config + cache_config = var.cache_config +} diff --git a/digitalocean/outputs.tf b/digitalocean/outputs.tf new file mode 100644 index 0000000..2cc69d2 --- /dev/null +++ b/digitalocean/outputs.tf @@ -0,0 +1,54 @@ +output "fleet_application_url" { + description = "The primary URL to access the Fleet application." + value = module.fleet.fleet_application_url +} + +output "app_id" { + description = "The ID of the DigitalOcean App Platform app." + value = module.fleet.app_id +} + +output "app_live_url" { + description = "The live URL assigned by App Platform." + value = module.fleet.app_live_url +} + +output "database_host" { + description = "The hostname of the MySQL database cluster." + value = module.fleet.database_host +} + +output "database_port" { + description = "The port of the MySQL database cluster." + value = module.fleet.database_port +} + +output "cache_host" { + description = "The hostname of the Valkey cache cluster." + value = module.fleet.cache_host +} + +output "cache_port" { + description = "The port of the Valkey cache cluster." + value = module.fleet.cache_port +} + +output "spaces_bucket_name" { + description = "The name of the Spaces bucket for Fleet software installers." + value = module.fleet.spaces_bucket_name +} + +output "spaces_bucket_endpoint" { + description = "The endpoint of the Spaces bucket." + value = module.fleet.spaces_bucket_endpoint +} + +output "vpc_id" { + description = "The ID of the VPC." + value = module.fleet.vpc_id +} + +output "dns_record_fqdn" { + description = "The FQDN of the DNS record for Fleet." + value = module.fleet.dns_record_fqdn +} diff --git a/digitalocean/variables.tf b/digitalocean/variables.tf new file mode 100644 index 0000000..1786c03 --- /dev/null +++ b/digitalocean/variables.tf @@ -0,0 +1,90 @@ +variable "region" { + description = "The DigitalOcean region for all resources." + type = string + default = "nyc3" +} + +variable "prefix" { + description = "A prefix used for naming resources." + type = string + default = "fleet" +} + +variable "domain_name" { + description = "The domain name for Fleet (e.g., 'fleet.example.com'). A DigitalOcean DNS zone and A record will be created." + type = string +} + +variable "vpc_config" { + description = "Configuration for the DigitalOcean VPC." + type = object({ + name = string + ip_range = string + }) + default = { + name = "fleet-network" + ip_range = "10.10.10.0/24" + } +} + +variable "fleet_config" { + description = "Configuration for the Fleet application deployment on App Platform." + type = object({ + image_tag = string + instance_size_slug = string + instance_count = number + license_key = optional(string) + debug_logging = bool + exec_migration = bool + extra_env_vars = optional(map(string)) + }) + default = { + image_tag = "fleetdm/fleet:v4.90.0" + instance_size_slug = "apps-s-1vcpu-1gb" + instance_count = 1 + debug_logging = false + exec_migration = true + extra_env_vars = {} + } +} + +variable "database_config" { + description = "Configuration for the managed MySQL database cluster." + type = object({ + name = string + engine = string + version = string + size = string + node_count = number + database_name = string + database_user = string + deletion_protection = optional(bool, false) + }) + default = { + name = "fleet-mysql" + engine = "mysql" + version = "8" + size = "db-s-1vcpu-1gb" + node_count = 1 + database_name = "fleet" + database_user = "fleet" + } +} + +variable "cache_config" { + description = "Configuration for the managed Valkey (Redis-compatible) cache cluster." + type = object({ + name = string + engine = string + version = string + size = string + node_count = number + }) + default = { + name = "fleet-cache" + engine = "valkey" + version = "8" + size = "db-s-1vcpu-1gb" + node_count = 1 + } +}