Terraform for GCP How to create Cloud SQL

Paul Ravvich
Terraform for the Google Cloud Platform
2 min readMay 5, 2024

--

Terraform for GCP How to create Cloud SQL

Hi, this is Paul, and welcome to the #27 part of my Terraform guide. Today we will discuss, how to create a Cloud SQL using the Terraform script.

Add Role for Service Account

First, to create a Cloud SQL you need to add a Role Cloud SQL Admin to you're Service Account. How to add permissions we already discussed in this article:

Defining the google_sql_database_instance Resource

First, we need to create a MySQL database instance. This is done using the google_sql_database_instance resource in Terraform. Here is an example configuration:

resource "google_sql_database_instance" "mysql_demo_db" {
name = "db"
deletion_protection = false
region = "us-central1"
settings {
tier = "db-f1-micro"
}
}

Configuration Parameters

  • name: This is the name of the database instance in GCP. The name must be unique within the project and region.
  • deletion_protection: A parameter that prevents accidental deletion of the database instance. Here, it is set to false, which means the database can be deleted without additional precautions.
  • region: The region where the database instance will be located. In this case, it’s “us-central1”.
  • settings.tier: The tier that specifies the hardware configuration for the database instance. “db-f1-micro” is the smallest available tier, suitable for development and testing but not for production loads.

Defining the google_sql_user Resource

After creating the database instance, we need to create a user for this database. This is done using the google_sql_user resource:

resource "google_sql_user" "demo_user" {
name = "paul"
password = "mypassword"
instance = google_sql_database_instance.mysql_demo_db.name
}

Configuration Parameters

  • name: The name of the database user.
  • password: The password for accessing the database. In real projects store this value in Secret Manager.
  • instance: Links to the database instance that this user will have access to. Here, the name attribute of the google_sql_database_instance a resource we defined earlier is used.

Full code:

resource "google_sql_database_instance" "mysql_demo_db" {
name = "db"
deletion_protection = false
region = "us-central1"
settings {
tier = "db-f1-micro"
}
}

resource "google_sql_user" "demo_user" {
name = "paul"
password = "mypassword"
instance = google_sql_database_instance.mysql_demo_db.name
}

Conclusion

Using Terraform to manage databases and users in GCP offers numerous benefits, including automation, change management, and reproducibility. The configuration outlined above demonstrates the initial steps for deploying a MySQL database and creating a user in a cloud environment. These principles can be expanded and adapted for more complex cloud resource management scenarios.

Thank you for reading until the end. Before you go:

Paul Ravvich

--

--

Paul Ravvich
Terraform for the Google Cloud Platform

Software Engineer with over 10 years of XP. Join me for tips on Programming, System Design, and productivity in tech! New articles every Tuesday and Thursday!