Skip to content

Latest commit

 

History

History

postgres

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Terraform for creating a GCP PostgreSQL database instance

Sometimes you need a database. Why not use Google Cloud Platform's Cloud SQL?

Cloud SQL is an easy-to-use service that delivers fully managed SQL databases in the cloud. Cloud SQL provides PostgreSQL, SQL Server, and MySQL databases.

This sample takes advantage of the postgresql sub-module of sql-db within the Terraform Registry.

Starts with the assumption that you will use a service account's credentials that has appropriate role/permissions to create/delete a Cloud SQL database instance.

Copy sample configuration

cp terraform.tfvars.sample terraform.tfvars

Edit terraform.tfvars

Amend the values for

  • project
  • region
  • zone
  • database_version
  • database_tier
  • database_username
  • encryption_key_name
  • name
  • additional_databases
  • additional_users
  • service_account_credentials

Create

./create-database.sh

Use

You'll probably want to connect to the database instance you just created, huh?

In order to obtain the ssl_key, ssl_cert, and ssl_ca you'll, there's a convenient script to generate them and a sample psql client command

Change directories

cd ../connect

Copy sample configuration

cp terraform.tfvars.sample terraform.tfvars

Edit terraform.tfvars

Amend values for

  • project
  • region
  • instance_name
  • database_name
  • database_username
  • instance_public_ip_address
  • service_account_credentials

Obtain encrypted connection details

./create-connection.sh

Destroy encrypted connection details

./destroy-connection.sh

Destroy

To tear it down

./destroy-database.sh

Troubleshooting

If you're going to setup the database instance with an encryption_key_name then you will need to perform some work outside of Terraform as described here. The service account you use to create the database instance must also have the roles/cloudkms.cryptoKeyEncrypterDecrypter role assigned.