How to create CloudSQL Mysql instance with terraform
By: Samet Karadag
Tags: Cloud Sql, Gcp, Terraform

1- Create tfvar file:

#cat terraform.tfvars project = "project-id" region = "europe-west4"

2- Create tf file and put mysql database related information in it, such as instance, database name and root password:

#cat main.tf variable "project" {} variable "region" {} provider "google" { project = "${var.project}" region = "${var.region}" } resource “google_sql_database_instance” “master” { name = "instance_name" database_version = "MYSQL_5_7" region = "${var.region}" settings { tier = "db-n1-standard-2" } } resource "google_sql_database" “database” { name = "database_name" instance = "${google_sql_database_instance.master.name}" charset = "utf8" collation = "utf8_general_ci" } resource "google_sql_user" "users" { name = "root" instance = "${google_sql_database_instance.master.name}" host = "%" password = "XXXXXXXXX" }

3- Create service account on gcp which has necessary privileges to create CloudSQL instance:

gcloud iam service-accounts create sa-terraform — display-name "Terraform service account"

Assign CloudSQL Admin role to the service account:

gcloud projects add-iam-policy-binding PROJECT_ID — member="serviceAccount:sa-terraform@PROJECT_ID.iam.gserviceaccount.com" — role="roles/cloudsql.admin"

Create a service account key which will be used by terraform:

Export GOOGLE_APPLICATION_CREDENTIALS:

export GOOGLE_APPLICATION_CREDENTIALS=./key.json

4- Initialize terraform:

terraform init

5- Get terraform plan

terraform plan

6- Apply terraform

terraform apply

If it is a brand new project or if you haven’t create CloudSQL in the project before you may get an error like below:

Error: Error, failed to create instance emlak: googleapi: Error 403: Cloud SQL Admin API has not been used in project XXXXXXXXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=XXXXXXXXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured

You can enable Cloudsql api in the GCP console OR simply cp/pasting the url in the error message into your browser and then enable the API.

Then try again terraform apply.

Do you want to perform these actions?

Terraform will perform the actions described above.

Only ‘yes’ will be accepted to approve. Enter a value: yes google_sql_database_instance.master: Creating…

google_sql_database_instance.master: Still creating… [10s elapsed]

google_sql_database_instance.master: Still creating… [20s elapsed]

google_sql_database_instance.master: Still creating… [30s elapsed]

google_sql_database_instance.master: Still creating… [40s elapsed]

google_sql_database_instance.master: Still creating… [50s elapsed]

google_sql_database_instance.master: Still creating… [1m0s elapsed]

google_sql_database_instance.master: Still creating… [1m10s elapsed]

google_sql_database_instance.master: Still creating… [1m20s elapsed]

google_sql_database_instance.master: Still creating… [1m30s elapsed]

google_sql_database_instance.master: Still creating… [1m40s elapsed]

google_sql_database_instance.master: Still creating… [1m50s elapsed]

google_sql_database_instance.master: Still creating… [2m0s elapsed]

google_sql_database_instance.master: Still creating… [2m10s elapsed]

google_sql_database_instance.master: Still creating… [2m20s elapsed]

google_sql_database_instance.master: Still creating… [2m30s elapsed]

google_sql_database_instance.master: Still creating… [2m40s elapsed]

google_sql_database_instance.master: Still creating… [2m50s elapsed]

google_sql_database_instance.master: Creation complete after 2m55s [id=instanceid]

google_sql_database.database: Creating…

google_sql_user.users: Creating…

google_sql_user.users: Still creating… [22s elapsed]

google_sql_database.database: Still creating… [22s elapsed]

google_sql_database.database: Creation complete after 24s [id=projects/projectid/instances/instanceid/databases/dbid]

google_sql_user.users: Creation complete after 28s [id=root/%/instanceid]

That is it you created your Cloudsql Mysql intance with terraform.