📖 Scenario: You are setting up a Cloud SQL instance on Google Cloud Platform to host a database for a small web application. This instance will store user data securely and allow the application to connect to it.
🎯 Goal: Build a Cloud SQL instance configuration using Terraform that creates a MySQL database instance with basic settings.
📋 What You'll Learn
Create a Terraform resource block for a Cloud SQL instance named my-sql-instance.
Set the database version to MYSQL_8_0.
Configure the instance with a db-f1-micro machine type.
Set the region to us-central1.
Enable automated backups.
💡 Why This Matters
🌍 Real World
Cloud SQL instances are used to host managed relational databases for applications, removing the need to manage database servers manually.
💼 Career
Knowing how to create and configure Cloud SQL instances is essential for cloud engineers and developers working with Google Cloud Platform to build scalable and reliable applications.
Progress0 / 4 steps
1
DATA SETUP: Define the Terraform provider
Write a Terraform block to configure the Google Cloud provider with the project ID my-gcp-project and region us-central1.
GCP
Hint
Use the provider "google" block with project and region attributes.
2
CONFIGURATION: Define the Cloud SQL instance resource
Add a Terraform resource block named google_sql_database_instance with the name my-sql-instance and set the database version to MYSQL_8_0.
GCP
Hint
Use resource "google_sql_database_instance" "my-sql-instance" and set name and database_version.
3
CORE LOGIC: Configure the instance settings
Inside the google_sql_database_instance resource, add an settings block that sets the tier to db-f1-micro and the location to us-central1.
GCP
Hint
Use a settings block with tier and location inside the resource.
4
COMPLETION: Enable automated backups
Inside the settings block, add a backup_configuration block with enabled = true to enable automated backups.
GCP
Hint
Add a backup_configuration block inside settings with enabled = true.
Practice
(1/5)
1. What is the main purpose of creating a Cloud SQL instance in Google Cloud?
easy
A. To store files and objects in the cloud
B. To set up a managed database service without handling server maintenance
C. To create a virtual machine for running applications
D. To configure network firewalls for security
Solution
Step 1: Understand Cloud SQL service
Cloud SQL is a managed database service that handles database setup and maintenance for you.
Step 2: Identify the purpose of creating an instance
Creating a Cloud SQL instance means setting up a database server managed by Google Cloud, so you don't manage the server yourself.
Final Answer:
To set up a managed database service without handling server maintenance -> Option B
Quick Check:
Cloud SQL = Managed database service [OK]
Hint: Cloud SQL means managed database, not VM or storage [OK]
Common Mistakes:
Confusing Cloud SQL with virtual machines
Thinking Cloud SQL is for file storage
Mixing Cloud SQL with network configuration
2. Which of the following is the correct command to create a Cloud SQL instance named my-instance with MySQL version 8.0 in the region us-central1?
easy
A. gcloud sql instances create my-instance --database-version=MYSQL_8_0 --region=us-central1
B. gcloud sql create instance my-instance --version=MYSQL8 --location=us-central1
C. gcloud sql instances new my-instance --db-version=8.0 --zone=us-central1
D. gcloud sql instances create my-instance --db-version=MYSQL8 --region=us-central1
Solution
Step 1: Recall correct gcloud command syntax
The correct command starts with gcloud sql instances create followed by the instance name.
Step 2: Check flags for database version and region
The flag for database version is --database-version and for region is --region. The version value for MySQL 8.0 is MYSQL_8_0.
Final Answer:
gcloud sql instances create my-instance --database-version=MYSQL_8_0 --region=us-central1 -> Option A
But you get an error saying the tier is invalid. What is the likely cause?
medium
A. The tier name is correct but the database version is invalid
B. The region us-west1 does not support MySQL 5.7
C. The tier name should be db-n1-standard-1 with 'db-' prefix
D. The tier name requires a different format for MySQL
Solution
Step 1: Check tier naming conventions
Cloud SQL machine tiers use the 'db-' prefix. The correct tier name is 'db-n1-standard-1'.
Step 2: Validate other parameters
MySQL 5.7 is valid, and us-west1 supports it. The error is due to missing 'db-' prefix in tier name.
Final Answer:
The tier name should be db-n1-standard-1 with 'db-' prefix -> Option C
Quick Check:
Tier names include 'db-' prefix [OK]
Hint: Add 'db-' prefix to tier names for Cloud SQL [OK]
Common Mistakes:
Omitting 'db-' prefix in tier names
Assuming region limits cause tier errors
Mixing database version errors with tier errors
5. You want to create a highly available Cloud SQL instance with PostgreSQL 13 in region us-central1. Which command correctly sets up a primary instance with high availability enabled?
hard
A. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --availability-type=REGIONAL
B. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --high-availability
C. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --availability=HIGH
D. gcloud sql instances create ha-instance --database-version=POSTGRES_13 --region=us-central1 --replica-type=PRIMARY
Solution
Step 1: Identify flag for high availability
The correct flag to enable high availability in Cloud SQL is --availability-type=REGIONAL.
Step 2: Confirm other parameters
Database version POSTGRES_13 and region us-central1 are valid. The command uses correct syntax for creating a highly available instance.