Skip to content

CloudSQL

A pre-requisite is to have enabled the API's for this to work if you haven't used this part of GCP before:

servicenetworking.googleapis.com

sqladmin.googleapis.com

Once the API's are enabled you have grant these roles:

Storage Admin
Cloud SQL Admin

To your Terraform Service user [hopefully].

Private Instance

Create a database instance, x databases with x number of users. This instance is created privately within your selected VPC.

Add module.cloudsql.tf to your code see other objects:-

module cloudsql {
  source       = "JamesWoolfenden/cloudsql/gcp"
  version      = "0.1.13"
  name         = var.name
  project      = var.project
  network_name = var.network_name
  database     = var.database
  users        = var.users
}

This creates any number of databases through the the variable "database":

variable "database" {
    type=list(object({
        name = string
    }))
    default=[]
}

Setting database to

    database=[{
        name= "my-database"
    },
    {
        name= "your-database"
    }]

Will create 2 databases. The "Users" variable and resource follows the same pattern.