The module creates and manages DigitalOcean DB cluster with one of supported SQL database engines:

  • PostgreSQL
  • MySQL

Key features:

  • flexible configuration
  • replicas management
  • users and databases management
  • connection pools management (for PostgreSQL cluster)

Managed SQL Databases in DigitalOcean

Creates managed SQL database resources in DigitalOcean
$550
BUY
626
Log in to Corewide IaC registry

Once you have a Corewide Solutions Portal account, this one-time action will use your browser session to retrieve credentials:

 shellterraform login solutions.corewide.com
Provision instructions

Initialize mandatory providers:

Copy and paste into your Terraform configuration and insert the variables:

 hclmodule "tf_do_database_sql" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3.0"

  # specify module inputs here or try one of the examples below
  ...
}

Initialize the setup:

 shellterraform init
Define update strategy

Corewide DevOps team strictly follows Semantic Versioning Specification to provide our clients with products that have predictable upgrades between versions. We recommend pinning patch versions of our modules using pessimistic constraint operator (~>) to prevent breaking changes during upgrades.

To get new features during the upgrades (without breaking compatibility), use ~> 1.3 and run terraform init -upgrade

For the safest setup, use strict pinning with version = "1.3.0"

v1.3.0 released 3 months, 3 weeks ago
New version approx. every 5 weeks

Minimal MySQL setup creates 1-node MySQL cluster with db-s-1vcpu-1gb machine type (by default, defaultdb database and privileged doadmin user are created):

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region      = "fra1"
  name_prefix = "develop"
  db_engine   = "mysql"
}

Minimal PostgreSQL setup creates 1-node PostgreSQL cluster with db-s-1vcpu-1gb machine type (by default, defaultdb database and privileged doadmin user are created):

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region      = "fra1"
  name_prefix = "develop"
  db_engine   = "postgres"
}

Managed SQL database engine can be configured. To omit hardcoding each config parameter, it is managed outside the module.

MySQL with custom configuration options:

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region      = "fra1"
  name_prefix = "develop"
  db_engine   = "mysql"
}

resource "digitalocean_database_mysql_config" "main" {
  cluster_id        = module.db.database_cluster.id
  connect_timeout   = 10
  default_time_zone = "UTC"
}

PostgreSQL with custom configuration options:

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region      = "fra1"
  name_prefix = "develop"
  db_engine   = "postgres"
}

resource "digitalocean_database_postgresql_config" "main" {
  cluster_id = module.db.database_cluster.id
  timezone   = "UTC"
  work_mem   = 16
}

All available parameters can be found in the resource's documentation for MySQL and PostgreSQL engines, respectively

Full MySQL setup creates 2-node MySQL cluster on specified VPC with db-s-2vcpu-2gb machine type, 20 GB storage size, myuser_db database, myuser user, configures the MySQL with specified parameters, add trusted sources, and tags the cluster as dev:

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region               = "fra1"
  name_prefix          = "develop"
  db_engine            = "mysql"
  vpc_id               = "xxxxxxxx-yyyy-zzzz-xxxx-yyyyyyyyyyyy"
  node_count           = 2
  machine_size         = "db-s-2vcpu-2gb"
  machine_storage_size = 20
  replica_count        = 2
  replica_machine_size = "db-s-2vcpu-2gb"

  maintenance_window = {
    day  = "Tue"
    hour = 15
  }

  tags = [
    "dev",
  ]

  databases = [
    "myuser_db",
  ]

  users = {
    myuser = {
      mysql_auth_plugin = "caching_sha2_password"
    }
  }

  trusted_sources = [
    {
      type  = "ip_addr"
      value = "10.114.32.24"
    },
    {
      type  = "k8s"
      value = "123ab45-67c8-de9f0-1ghj23"
    },
  ]
}

Full PostgreSQL setup creates 2-node PostgreSQL cluster on specified VPC with db-s-2vcpu-2gb machine type, 20 GB storage size, myuser_db database, myuser user, configures the connection pool for myuser_db database, add trusted sources, and tags the cluster as dev:

 hclmodule "db" {
  source  = "solutions.corewide.com/digitalocean/tf-do-database-sql/digitalocean"
  version = "~> 1.3"

  region               = "fra1"
  name_prefix          = "develop"
  db_engine            = "postgres"
  vpc_id               = "xxxxxxxx-yyyy-zzzz-xxxx-yyyyyyyyyyyy"
  engine_version       = 17
  node_count           = 2
  machine_size         = "db-s-2vcpu-2gb"
  machine_storage_size = 20
  replica_count        = 2
  replica_machine_size = "db-s-2vcpu-2gb"

  maintenance_window = {
    day  = "Tue"
    hour = 15
  }

  tags = [
    "dev",
  ]

  databases = [
    "myuser_db",
  ]

  users = {
    myuser = {}
  }

  connection_pools = [
    {
      database = "myuser_db"
      user     = "myuser"
      mode     = "transaction"
      size     = 22
    },
  ]

  trusted_sources = [
    {
      type  = "ip_addr"
      value = "10.114.32.24"
    },
    {
      type  = "k8s"
      value = "123ab45-67c8-de9f0-1ghj23"
    },
  ]
}
Variable Description Type Default Required Sensitive
db_engine Database engine. Supported values are: mysql for MySQL and pg or postgres for PostreSQL string yes no
name_prefix Name prefix for the created resources string yes no
region DigitalOcean region to create resources in string yes no
users[<key>] Name of user name to create string yes no
connection_pools List of connection pools to create. This parameter could only be applied to pg engine list(object) [] no no
connection_pools[*].database The name of a database to use with the connection pool string defaultdb no no
connection_pools[*].mode The PGBouncer transaction mode for the connection pool. The allowed values are: session, transaction, and statement string transaction no no
connection_pools[*].size The desired size of the PGBouncer connection pool number 20 no no
connection_pools[*].user The name of the database user to use with the connection pool string doadmin no no
databases List of additional databases to create inside the cluster list(string) [] no no
engine_version Version for current database engine. If not specified 16 considered as default for pg engine; Not applicable to mysql engine 8 is the only available version for mysql number 16 no no
machine_size Database Droplet size associated with the cluster string db-s-1vcpu-1gb no no
machine_storage_size Custom machine storage size (Gb) number 10 no no
maintenance_window Maintenance window configuration object {} no no
maintenance_window.day The day of the week (Mon, Tue, etc.) the maintenance window occurs string Mon no no
maintenance_window.hour The hour when the maintenance updates are applied, in UTC 24-hour format. Examples: 2, 3, 23 number 2 no no
node_count Number of cluster nodes number 1 no no
replicas Replica creation configuration object {} no no
replicas.count Number of database replicas to be created number 0 no no
replicas.machine_size Database replica Droplet machine size. If not provided, cluster machine_size is used string no no
tags List of tags to be assigned to the cluster resource list(string) [] no no
trusted_sources List of trusted sources to restrict connections to the database cluster list(object) [] no no
trusted_sources[*].type Type of resource that the firewall rule allows to access the database cluster. Possible values are: droplet, k8s, ip_addr, tag, or app string yes no
trusted_sources[*].value ID of the specific resource, name of a tag applied to a group of resources, or IP address that the firewall rule allows to access the database cluster string yes no
users Map of users, where key is a user name and value is parameters object map(object) {} no no
users[<key>].mysql_auth_plugin The authentication method to use for connections to the MySQL user account. The valid values are mysql_native_password or caching_sha2_password. Applicable for mysql engine string caching_sha2_password no no
vpc_id The ID of the VPC where the database cluster will be located. If not provided, default DigitalOcean VPC is used string no no
Output Description Type Sensitive
connection_parameters Connection parameters including connection pools if applicable computed yes
database_cluster Attributes of Cluster resource resource yes
databases Attributes of Database resources computed no
replicas Attributes of Replica resources computed yes
users Attributes of User resources computed yes
Dependency Version Kind
terraform >= 1.3 CLI
digitalocean/digitalocean ~> 2.34 provider

Not sure where to start?
Let's find your perfect match.