In this post, will show how to use Terraform to mange Google Cloud SQL resources.
Prerequisites:
- Google Cloud Platform account
- Terraform installed on your machine (you can find more info here)
Introduction
This article will cover the following:
- Create a Cloud MySQL instance with a randomised name
- Use labels to indicate that this is a development environment
- Set the maintenance window
- Create a database and a database user
- Parameterise configurations using input variables in a file
- Use output variables for display connection name after creating the instance
Configuration
Create a directory to store all configuration files. By default Terraform will load all files ending in .tf or .tfvars. In that directory, create a main.tf file with additional resources.
provider "google" {
version = "3.10.0"
credentials = file(var.credentials_file)
project = var.project
region = var.region
zone = var.zone
}
resource "random_id" "db_name_suffix" {
byte_length = 4
}
resource "google_sql_database_instance" "mysql" {
name = "mysql-instance-${random_id.db_name_suffix.hex}"
database_version = "MYSQL_5_6"
settings{
tier = "db-f1-micro"
user_labels = {
"environment" = "development"
}
maintenance_window {
day = "1"
hour = "4"
}
backup_configuration {
enabled = true
start_time = "04:30"
}
}
}
Initialize and pull Terraform cloud-specific dependencies:
terraform init
Apply Terraform:
terraform apply -var-file="secret.tfvars"
- created a secret.tfvars file that do not version control. The file contains the following:
project = "[PROJECT_ID]"
credentials_file = "[NAME].json"
outputs.tf contains:
output "connection_name" {
value = google_sql_database_instance.postgres.connection_name
}
variables.tf:
variable "project" {
}
variable "credentials_file" {
}
variable "region" {
default = "europe-west2"
}
variable "zone" {
default = "urope-west2-c""
}
Creating MySQL instance will take a few minutes. After Terraform is done, go to the console to see the instance that has been created or use ‘gcloud sql instances list’.
Terraform will perform the following actions:
# google_sql_database.database will be created
+ resource "google_sql_database" "database" {
+ charset = (known after apply)
+ collation = (known after apply)
+ id = (known after apply)
+ instance = (known after apply)
+ name = "azuri"
+ project = (known after apply)
+ self_link = (known after apply)
}
# google_sql_database_instance.mysql will be created
+ resource "google_sql_database_instance" "mysql" {
+ connection_name = (known after apply)
+ database_version = "MYSQL_5_6"
+ first_ip_address = (known after apply)
+ id = (known after apply)
+ ip_address = (known after apply)
+ master_instance_name = (known after apply)
+ name = (known after apply)
+ private_ip_address = (known after apply)
+ project = (known after apply)
+ public_ip_address = (known after apply)
+ region = (known after apply)
+ self_link = (known after apply)
+ server_ca_cert = (known after apply)
+ service_account_email_address = (known after apply)
Enter a value: yes
random_id.db_name_suffix: Creating...
random_id.db_name_suffix: Creation complete after 0s [id=q_MscA]
google_sql_database_instance.mysql: Creating...
google_sql_database_instance.mysql: Still creating... [10s elapsed]