DigitalOcean Terraform Provider

repository·main·Indexed 20 days ago

https://github.com/digitalocean/terraform-provider-digitalocean

The DigitalOcean Terraform Provider allows developers to manage DigitalOcean infrastructure, including Droplets, Kubernetes clusters (DOKS), networking, and databases, using HashiCorp Terraform configuration files. It supports resources such as BYOIP prefixes, PostgreSQL database clusters with rsyslog log forwarding, Global Load Balancer TLS certificate rotation, and Gradient AI Indexing jobs.

Tokens
129.4K
Snippets
367
Records
594
Agent score
68%

What's inside terraform-provider-digitalocean

  1. Retrieve all supported DigitalOcean regions with digitalocean_regions

    main

    Use the digitalocean_regions data source to fetch information about all supported DigitalOcean regions. You can use filter blocks to narrow down results (e.g., finding only available regions) and sort blocks to order the output. If no filters are applied, the data source returns all regions.

    Note: If you already know the specific slug of a region, use the digitalocean_region data source instead to retrieve metadata for that single region.

    data "digitalocean_regions" "available" {
      filter {
        key    = "available"
        values = ["true"]
      }
    }
  2. Manage DigitalOcean Projects with digitalocean_project

    main

    The digitalocean_project resource allows you to organize DigitalOcean resources (such as Droplets, Kubernetes Clusters, Load Balancers, and Spaces Buckets) into logical groups. This helps align your infrastructure with specific applications or workflows.

    Important Note: A project managed by Terraform cannot be set as the default project.

    resource "digitalocean_project" "playground" {
      name        = "playground"
      description = "A project to represent development resources."
      purpose     = "Web Application"
      environment = "Development"
    }
  3. Use the digitalocean_droplet data source

    main

    The digitalocean_droplet data source allows you to retrieve information about an existing Droplet for use in other Terraform resources. This is particularly useful for accessing properties of Droplets that are not managed by your current Terraform configuration.

    Important Constraints:

    • This data source returns exactly one Droplet.
    • If you use the tag argument and the search returns more than one Droplet, Terraform will trigger an error.
    • To include GPU Droplets when searching by name, you must explicitly set the gpu argument to true.
    data "digitalocean_droplet" "example" {
      name = "web"
    }
    
    output "droplet_output" {
      value = data.digitalocean_droplet.example.ipv4_address
    }
  4. Use GradientAI data sources for model discovery and automation

    main

    The GradientAI data sources are designed for:

    • Inventory Management: Discovering all custom models on a team to build inventory outputs.
    • Lifecycle Management: Narrowing results to specific states using the status argument (server-side) or top-level attributes via filter (client-side).
    • Resource Orchestration: Using a filtered subset of models to drive downstream resources, such as dedicated inference deployments.
  5. Retrieve multiple DigitalOcean projects with digitalocean_projects

    main

    Use the digitalocean_projects data source to retrieve a list of all projects associated with your DigitalOcean account. You can apply filter blocks to narrow down results and sort blocks to order them. If no filters are applied, the data source returns all projects.

    Note: If you already know the specific id or unique name of a project, use the digitalocean_project (singular) data source instead for more efficient metadata retrieval.

    data "digitalocean_projects" "example" {
      filter {
        key    = "environment"
        values = ["Staging"]
      }
    }
  6. Configure advanced options for DigitalOcean Redis clusters

    main

    The digitalocean_database_redis_config resource allows you to manage advanced configuration settings for a DigitalOcean managed Redis database cluster.

    Important Migration Note: DigitalOcean managed Redis clusters were discontinued on 30 June 2025 and replaced by the Managed Valkey product. For new deployments, use the digitalocean_database_valkey_config resource instead.

    Behavioral Note: Redis configurations are only removed from Terraform state when the resource is destroyed. Destroying the resource does not unset the remote configuration on the DigitalOcean side.

    resource "digitalocean_database_redis_config" "example" {
      cluster_id             = digitalocean_database_cluster.example.id
      maxmemory_policy       = "allkeys-lru"
      notify_keyspace_events = "KEA"
      timeout                = 90
    }
    
    resource "digitalocean_database_cluster" "example" {
      name       = "example-redis-cluster"
      engine     = "redis"
      version    = "7"
      size       = "db-s-1vcpu-1gb"
      region     = "nyc1"
      node_count = 1
    }
  7. Important considerations for OpenSearch logsinks

    main

    Managed OpenSearch with Trusted Sources

    When forwarding logs to a DigitalOcean Managed OpenSearch cluster that has trusted sources enabled, you must manually allow-list the IP addresses of your database cluster nodes in the OpenSearch configuration.

    Authentication Patterns

    1. URL-based: Include credentials in the endpoint URL: https://username:password@host:port.
    2. IP-based: Configure your OpenSearch/Elasticsearch cluster to accept connections from your database cluster's IP addresses.
  8. Configure advanced MySQL settings with digitalocean_database_mysql_config

    main

    The digitalocean_database_mysql_config resource is a virtual resource used to manage advanced configuration options for a DigitalOcean managed MySQL database cluster.

    Important Note: MySQL configurations are only removed from the Terraform state when the resource is destroyed. Destroying the resource does not unset the remote configuration on the DigitalOcean side.

    resource "digitalocean_database_mysql_config" "example" {
      cluster_id        = digitalocean_database_cluster.example.id
      connect_timeout   = 10
      default_time_zone = "UTC"
    }
    
    resource "digitalocean_database_cluster" "example" {
      name       = "example-mysql-cluster"
      engine     = "mysql"
      version    = "8"
      size       = "db-s-1vcpu-1gb"
      region     = "nyc1"
      node_count = 1
    }
  9. Important considerations for Database and Rsyslog configuration

    main

    When using the database and logsink resources, keep the following in mind:

    • Metrics Credentials: Database metrics credentials are account-wide, not specific to an individual cluster.
    • Rsyslog Connectivity: The rsyslog_server must be reachable from DigitalOcean. Ensure you update the variable to a valid hostname or IP.
    • Security: For production environments, it is recommended to enable TLS for the rsyslog connection. Refer to the database_logsink_rsyslog resource documentation for specific TLS configuration examples.
  10. Manage a DigitalOcean Floating IP

    main

    The digitalocean_floating_ip resource represents a publicly-accessible static IP address that can be mapped to a Droplet.

    Assignment Logic: You can assign a Floating IP to a Droplet using one of two mutually exclusive methods:

    1. Directly on the resource: Set the droplet_id argument within the digitalocean_floating_ip block.
    2. Using an assignment resource: Use the digitalocean_floating_ip_assignment resource.

    Note: You cannot use both methods simultaneously for the same IP.

    resource "digitalocean_droplet" "foobar" {
      name               = "baz"
      size               = "s-1vcpu-1gb"
      image              = "ubuntu-18-04-x64"
      region             = "sgp1"
      ipv6               = true
      private_networking = true
    }
    
    resource "digitalocean_floating_ip" "foobar" {
      droplet_id = digitalocean_droplet.foobar.id
      region     = digitalocean_droplet.foobar.region
    }
  11. Manage DigitalOcean Tags with digitalocean_tag

    main

    The digitalocean_tag resource allows you to create and manage DigitalOcean Tags. Tags are labels used to organize resources or facilitate lookups and actions. Once created, tags can be referenced in other resource configurations (like Droplets) using their id or name.

    resource "digitalocean_tag" "foobar" {
      name = "foobar"
    }
    
    resource "digitalocean_droplet" "web" {
      image  = "ubuntu-18-04-x64"
      name   = "web-1"
      region = "nyc3"
      size   = "s-1vcpu-1gb"
      tags   = [digitalocean_tag.foobar.id]
    }