cloud-foundation-fabric/examples/data-solutions/cloudsql-multiregion
Lorenzo Caggioni dea3e73cbd Mention Cloud NAT in the readme 2022-04-14 10:58:12 +02:00
..
README.md Mention Cloud NAT in the readme 2022-04-14 10:58:12 +02:00
backend.tf.sample Improve README 2022-04-11 18:14:59 +02:00
cloudsql.tf Fix Instance name 2022-04-13 16:39:33 +02:00
datastorage.tf Add VM 2022-04-13 13:09:10 +02:00
diagram.png Mention Cloud NAT in the readme 2022-04-14 10:58:12 +02:00
gce.tf Remove unused code, update output, fix GCE. 2022-04-14 10:06:04 +02:00
kms.tf Add VM 2022-04-13 13:09:10 +02:00
main.tf Remove unused code, update output, fix GCE. 2022-04-14 10:06:04 +02:00
outputs.tf Remove unused code, update output, fix GCE. 2022-04-14 10:06:04 +02:00
terraform.tfvars.sample Updates to README 2022-04-12 23:42:25 +02:00
variables.tf Add VM 2022-04-13 13:09:10 +02:00

README.md

Cloud SQL instance with multi-region read replicas

This example creates a Cloud SQL instance with multi-region read replicas as described in the Cloud SQL for PostgreSQL disaster recovery article.

The solution is resilient to a regional outage. To get familiar with the procedure needed in the unfortunate case of a disaster recovery, please follow steps described in part two of the aforementioned article.

The solution will use:

  • VPC with Private Service Access to deploy the instances and VM
  • Cloud SQL - Postgre SQL instanced with Private IP
  • Goocle Cloud Storage bucket to handle database import/export
  • Google Cloud Engine instance to connect to the Posgre SQL instance
  • Google Cloud NAT to access internet resources

This is the high level diagram:

Cloud SQL multi-region.

Requirements

This example will deploy all its resources into the project defined by the project_id variable. Please note that we assume this project already exists. However, if you provide the appropriate values to the project_create variable, the project will be created as part of the deployment.

If project_create is left to null, the identity performing the deployment needs the owner role on the project defined by the project_id variable. Otherwise, the identity performing the deployment needs resourcemanager.projectCreator on the resource hierarchy node specified by project_create.parent and billing.user on the billing account specified by project_create.billing_account_id.

Deployment

Configure the Terraform variables in your terraform.tfvars file. You need to specify at least the project_id and prefix variables. See terraform.tfvars.sample as starting point.

Run Terraform init:

$ terraform init
$ terraform apply

You should see the output of the Terraform script with resources created and some commands that you'll need in the following steps below.

Move to real use case consideration

This implementation is intentionally minimal and easy to read. A real world use case should consider:

  • Using a Shared VPC
  • Using VPC-SC to mitigate data exfiltration

Test your environment

We assume all those steps are run using a user listed on data_eng_principals. You can authenticate as the user using the following command:

$ gcloud init
$ gcloud auth application-default login

Below you can find commands to connect to the VM instance and Cloud SQL instance.

  $ gcloud compute ssh sql-test --project PROJECT_ID --zone ZONE
  sql-test:~$ cloud_sql_proxy -instances=CLOUDSQL_INSTANCE=tcp:5432
  sql-test:~$ psql 'host=127.0.0.1 port=5432 sslmode=disable dbname=DATABASE user=USER'

You can find computed commands on the Terraform demo_commands output.

Variables

name description type required default
postgres_user_password postgres user password. string
prefix Unique prefix used for resource names. Not used for project if 'project_create' is null. string
project_id Project id, references existing project if project_create is null. string
cmek_encryption Flag to enable CMEK on GCP resources created. bool false
data_eng_principals Groups with Service Account Token creator role on service accounts in IAM format, only user supported on CloudSQL, eg 'user@domain.com'. list(string) []
postgres_database postgres database. string "guestbook"
project_create Provide values if project creation is needed, uses existing project if null. Parent is in 'folders/nnn' or 'organizations/nnn' format. object({…}) null
regions Map of instance_name => location where instances will be deployed. map(string) {…}
sql_configuration Cloud SQL configuration object({…}) {…}

Outputs

name description sensitive
bucket Cloud storage bucket to import/export data from Cloud SQL.
connection_names Connection name of each instance.
demo_commands Demo commands.
ips IP address of each instance.
project_id ID of the project containing all the instances.
service_accounts Service Accounts.