Design, migrate, and run scalable, highly available databases on Google Cloud across relational and NoSQL services, and track your readiness for the Professional Cloud Database Engineer exam.
Prepare and test your skills
Prepare and test your skills
Automate Database Configuration and Performance Parameters is the practice of defining database instance settings in repeatable scripts instead of manual console steps. Cloud SQL (opens in a new tab) instance creation accepts compute sizing, database flags, high availability, backup, and private connectivity settings through gcloud commands and Terraform resources. Automation with infrastructure as code (IaC) reduces human error, keeps development, staging, and production consistent, and enables faster recovery from failures.
Machine type and compute sizing controls the vCPUs and memory available to a Cloud SQL instance for its workload. In gcloud, sizing is set with --cpu=NUMBER_OF_CORES and --memory=AMOUNT_OF_MEMORY, where memory requires a unit such as 3072MiB or 9GiB, plus --database-version, --zone, and --root-password. In Terraform, the same sizing is set in the google_sql_database_instance resource with settings { tier = "db-custom-2-7680" }, where the tier encodes CPU and memory as db-custom-[CPUS]-[MEMORY_MBS] or uses a preset such as db-f1-micro. Instance names are externally visible, so the name must not contain sensitive or personally identifiable information, and the project ID must not be included in the name because Cloud SQL adds it automatically where needed.
Database flags are key-value instance parameters applied at creation time to change database behavior. A gcloud example sets --database-flags=cloudsql.iam_authentication=on to enable Identity and Access Management (IAM) database authentication for PostgreSQL, while the MySQL variant uses cloudsql_iam_authentication=on. In Terraform, the flag is set inside settings with a database_flags { name = "cloudsql.iam_authentication" value = "on" } block alongside name, region, database_version, and tier. The automation flow runs from the defined flag to the Cloud SQL control plane, which then applies the flag to the provisioned instance.
High availability (HA) (opens in a new tab) configuration determines whether a Cloud SQL instance can fail over to another zone in the same region. The availability_type parameter controls this behavior, and automated scripts set it to enable a regional standby that uses synchronous replication between zones for zero or near-zero recovery time objective (RTO) and recovery point objective (RPO). When primary and secondary zones are both specified, the primary zone defaults to Any and the secondary zone defaults to Any (different from primary), and the two zones must be distinct. A Terraform HA example combines availability_type = "REGIONAL" with tier = "db-custom-2-7680" and an enabled backup_configuration block, because automated backups support recovery alongside HA.
| Availability type | Failover behavior | Choose when |
|---|---|---|
| REGIONAL | Creates a regional instance with standby in a different zone; instance fails over within the selected region | Production instances that require HA |
| ZONAL | Provides no failover capability; this is the default value | Non-production or cost-sensitive instances that can tolerate zonal outage |
Exam tip: REGIONAL enables HA failover to another zone in the same region, while ZONAL provides no failover and is the default.
Automated backup configuration defines whether backups run on a schedule and when the backup window starts. In Terraform, backups are enabled inside settings with a backup_configuration { enabled = true start_time = "20:55" binary_log_enabled = true } block, where binary_log_enabled is used for MySQL point-in-time recovery. In gcloud, an existing instance is patched with gcloud sql instances patch INSTANCE_NAME --backup-start-time=HH:MM, where the time uses 24-hour format in UTC and defines the start of a 4-hour window during which the backup can begin. The change is verified with gcloud sql instances describe INSTANCE_NAME, which must show enabled: true and the specified time in the backupConfiguration section.
Private connectivity configuration controls how Virtual Private Cloud (VPC) clients reach the Cloud SQL instance without using public IP. In Terraform, the google_sql_database_instance resource sets ip_configuration { ipv4_enabled = false } with a nested psc_config { psc_enabled = true allowed_consumer_projects = [] } block to enable Private Service Connect (PSC). Traffic flows from allowed consumer projects listed in allowed_consumer_projects to the PSC endpoint attached to the Cloud SQL instance, while the trust boundary sits between the consumer VPC network and the Google-managed database network. The same Terraform project can deploy both the private networking setup and Compute Engine VMs as clients alongside the instance for consistent end-to-end networking.
The Terraform provisioning workflow applies version-controlled configuration files to a Google Cloud project through an ordered lifecycle. Each configuration file must live in its own directory, called a root module, with a .tf extension such as main.tf, and samples assume required APIs are already enabled in the project. Automation extends beyond initial provisioning because IaC minimizes human errors, Eventarc or Workflows can trigger remedial actions from audit logs, autoscaling can increase capacity during failover, and platform engineering can apply policy guardrails during deployment.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID, unless explicit values in the configuration override the environment variable.main.tf, copy in the sample code, review and modify parameters for the environment, then run terraform init once per directory, with -upgrade to use the latest Google provider version.terraform plan to preview resources that Terraform will create or update, and correct the configuration when the preview does not match expectations.terraform apply, enter yes at the prompt, wait for the Apply complete! message, then verify the resources in the Google Cloud console.deletion_protection = "false" in the configuration, run terraform apply to apply that change, then run terraform destroy and enter yes to remove previously applied resources.Infrastructure as Code (IaC) (opens in a new tab) is the process of provisioning and managing infrastructure using code instead of graphical interfaces or manual command-line scripts. IaC configurations declare the desired state of resources such as virtual machines, database connections, and storage, and the tooling creates that state in a safe and repeatable way. The same configuration can be deployed multiple times to create reproducible development, test, and production environments for databases including Cloud SQL, Spanner, and Bigtable.
The benefits of IaC for database provisioning come from treating infrastructure changes like application code with review, history, and reuse. A single versioned configuration becomes the source of truth that teams share across environments, which reduces manual errors at scale. IaC supports consistent database deployment, scalability, and high availability because every environment is built from the same declared settings.
IaC tools for Google Cloud divide the work between defining infrastructure and automating its deployment and state. HashiCorp Terraform defines resources in human-readable configuration files, while Infrastructure Manager (Infra Manager) and Terraform Cloud handle automated deployment, remote execution, and state. Choose the tool based on whether the need is authoring configurations, deploying them natively on Google Cloud, or managing change across an organization.
| Tool | What it does | Choose when |
|---|---|---|
| HashiCorp Terraform with Google Cloud provider | Defines cloud and on-premises resources in versioned, reusable files and manages them through a consistent lifecycle workflow | General code-based configuration and management of Google Cloud infrastructure is needed |
| Infrastructure Manager (Infra Manager) | Automates deployment and management of Google Cloud resources from Terraform configurations without maintaining a separate toolchain | Programmatic Terraform deployment to Google Cloud is needed |
| Terraform Cloud and Terraform Enterprise | Runs Terraform in a stable remote environment, securely stores state and secrets, connects to GitHub, GitLab, and Bitbucket, and can trigger plans on commits with API integration | Full organizational change management is needed, as SaaS with Terraform Cloud or self-hosted with Terraform Enterprise |
Defining database resources with Terraform means declaring Cloud SQL instances, databases, and users as resources that depend on each other. The google_sql_database_instance resource defines the instance name, region, database version such as POSTGRES_14 or MYSQL_8_0, machine tier such as db-custom-2-7680, database flags, high availability, and backups. The google_sql_database resource depends on the instance through instance = google_sql_database_instance.instance.name, and the google_sql_user resource attaches CLOUD_IAM_USER or CLOUD_IAM_SERVICE_ACCOUNT identities to the instance named by google_sql_database_instance.default.name. High availability and protection (opens in a new tab) are also declared in code, where setting availability_type = "REGIONAL" creates a regional Cloud SQL instance that spans multiple zones, while a backup_configuration block with enabled = true and start_time = "20:55" schedules automated backups. The deletion_protection argument controls whether Terraform is allowed to delete the instance, which separates day-to-day updates from destructive removal.
The standard Terraform provisioning workflow moves configuration from source files to running infrastructure in a fixed order. Each configuration lives in its own directory, called a root module, and Terraform tracks the mapping between files and real resources as state. The workflow depends on Cloud Shell for authentication and project context, on the root module for the declared resources, and on plan review before any change is applied.
GOOGLE_CLOUD_PROJECT once per project, unless explicit values in the configuration override it..tf file such as main.tf, copy and edit parameters for the environment, then run terraform init once per directory.terraform plan first and correct the configuration so created or updated resources match expectations.terraform apply, confirm with yes, wait for the Apply complete! message, then verify the resources in the Google Cloud console.deletion_protection = "false" and run terraform apply, and only after that run terraform destroy with yes to remove previously applied resources.Exam tip: Terraform cannot destroy a protected Cloud SQL instance with terraform destroy alone; first set deletion_protection to false, apply that change, then destroy.
Version control, state management, and CI/CD turn Terraform files into a reliable database delivery pipeline. Provisioning logic is stored in source control, modular templates are reused across projects, and configurations become artifacts with versioning for dev, staging, and production promotion. Terraform Cloud securely stores state and secrets remotely, while Infra Manager and Cloud Build run automated validate and plan checks and trigger updates on code commits, so environment differences are handled by templating rather than manual edits.
Integrate Security and CI/CD into Provisioning Workflows means defining database infrastructure, access, and encryption in version-controlled code and deploying that code through an automated pipeline. Terraform (opens in a new tab) defines the desired state of resources such as Cloud SQL instances in human-readable files, while Cloud Build executes the build and Infrastructure Manager (Infra Manager) automates the deployment of the Terraform configuration to Google Cloud. The same pipeline that creates development, test, and production environments also applies Identity and Access Management (IAM) roles, service accounts, and Customer-Managed Encryption Keys (CMEK) so security is consistent on every deployment.
Cloud Build is the service that runs database provisioning builds on Google Cloud by importing Terraform code, executing the specified steps, and producing versioned artifacts. Code flows from source control into Cloud Build, then from Cloud Build to Infra Manager or Terraform, and finally from the deployment tool to the target Google Cloud project and database instance. Cloud Build can import source from Cloud Storage buckets for data and artifacts, Cloud Source Repositories for Google-hosted code, GitHub for external version control, and Bitbucket for external version control. A database provisioning pipeline moves code through an ordered lifecycle where each stage depends on the previous stage:
validate and plan to preview creates, updates, or deletes.dev, staging, and prod definitions with environment-specific variables and templating.Automating IAM for databases means enabling IAM database authentication on the instance and granting database access through IAM policy bindings in code. Cloud SQL supports IAM database authentication through the cloudsql.iam_authentication database flag, and access is granted with the cloudsql.instanceUser role that permits principals to log in to the instance. IAM policies and service accounts are defined in Terraform or in Config Connector YAML manifests that are deployed to a Google Kubernetes Engine (GKE) cluster, so application and infrastructure access change together through the pipeline. Roles can be granted on the project that contains the Cloud SQL instances by using the Google Cloud console IAM page, the gcloud CLI, Terraform, or the Cloud SQL Admin API.
The cloudsql.instanceUser role applies to three types of principals, where an individual user or individual service account requires its own role binding while a group requires only one binding for all current members:
Best practice is to create a dedicated service account for each service that requires database access and assign least-privilege roles through the pipeline rather than manually in the console.
Exam tip: Enabling IAM login on Cloud SQL requires two separate actions: set cloudsql.iam_authentication to on in the google_sql_database_instance resource and grant the cloudsql.instanceUser role to the user, service account, or group.
Automating encryption and guardrails means declaring network, encryption, and lifecycle protections in the same Terraform templates that create the database. Customer-Managed Encryption Keys (CMEK) let the team control the encryption keys used for data, and compliance controls such as Require CMEK on Dataproc Clusters show the pattern of enforcing CMEK through code rather than manual selection. Related guardrails include restricting public IP addresses on database and compute resources, defining VPC Service Controls perimeters, and verifying Cloud SQL failover replication for high availability. Terraform enforces this desired state through a consistent workflow where Cloud Shell prepares the project, the root module holds the .tf files, and commands move state forward in order. First, terraform init prepares the directory, then terraform plan previews the changes against expectations, and only after review does terraform apply create or update the resources. To allow deletion, the configuration must first set deletion_protection to false before terraform destroy removes the managed resources.
--cpu and --memory flags and with Terraform tier = "db-custom-2-7680" in the google_sql_database_instance resource.availability_type = "REGIONAL" creates a Cloud SQL standby in a different zone in the same region, while ZONAL is the default with no failover capability.backup_configuration { enabled = true start_time = "20:55" }, while gcloud patching uses --backup-start-time=HH:MM in UTC for the start of a 4-hour window.cloudsql.iam_authentication to on and granting the cloudsql.instanceUser role.deletion_protection is set to false and applied before running terraform destroy.Last updated