Caution
The GCP environments are being decommissioned and scheduled to be destroyed. Therefore, this Terraform configuration is not in use anymore.
The repository is archived for historical records.
Custom Terraform module to create GCP Cloud SQL instances and databases
The list of input variables for this module can be seen in variables.tf
file.
A list of the outputs generated by the succesful application of the module can
also be seen in outputs.tf
.
If you wish to create a GCP Cloud SQL instance, with a user, a database set up in the instance and the secrets to access it, you could do in your Terraform project configuration:
./modules/my_database/database.tf
locals { postgresql_databases = toset(["my_app_database"]) } module "postgres_database" { source = "git@github.com:HummingbirdTechGroup/gcp-tf-module-sql-database?ref=v1.1.0" env = var.env name = "my-app-database" zone = var.gcp.region tier = var.cloudsql.instance_size availability_type = var.cloudsql.availability_type disk_size = "15" disk_type = "PD_SSD" app_name = "my-app" team = "engineering" labels = { backup = "false" } authorized_networks = var.network.authorized_networks enable_public_ip = true cost_type = "static" require_ssl = false private_network = var.network.private_network database_version = "POSTGRES_13" database_flags = { "max_connections" = var.cloudsql.max_connections } backup_bucket = var.backup.backup_bucket import_bucket = var.backup.import_bucket sql_user_name = ["my_app_database"] sql_user_host = ["%"] binary_log_enabled = false wait_after_create = 60 database_name = "my_app_database" }
./modules/my_database/secrets.tf
resource "google_secret_manager_secret" "my-app-database-url" { secret_id = "my-app-database-url" replication { automatic = true } } resource "google_secret_manager_secret_version" "my-app-database-url" { secret = google_secret_manager_secret.my-app-database-url.name secret_data = "postgresql://${module.postgres_database.database_users[0].name}:${module.postgres_database.database_password_list[0].result}@${module.postgres_database.private_ip_address}/${module.postgres_database.database_name}" lifecycle { ignore_changes = all } }
./modules/my_database/outputs.tf
output "private_ip_address" { value = module.postgres_database.private_ip_address } output "public_ip_address" { value = module.postgres_database.public_ip_address }
./modules/my_database/variables.tf
variable "env" { type = string } variable "gcp" { type = object({ region = string }) } variable "backup" { type = object({ backup_bucket = string, import_bucket = string }) } variable "cloudsql" { type = object({ instance_size = string availability_type = string max_connections = string }) } variable "network" { type = object({ private_network = string, authorized_networks = map(string) }) }
- And then, finally in your project's
main.tf
add a section like this:module "my_app_db" { source = "./modules/my_database" backup = { backup_bucket = var.cloudsql_database_backup_bucket import_bucket = var.cloudsql_database_backup_bucket } env = var.env gcp = { region = var.gcp_region } cloudsql = { instance_size = var.cloudsql_instance_size availability_type = var.cloudsql_availability_type max_connections = var.cloudsql_max_connections } network = { private_network = google_compute_network.vpc.self_link, authorized_networks = var.authorized_networks } }
When contributing to this Terraform module, please make sure that at least your PRs are properly formatted and the submitted configuration is valid.
You can easily review this before committing your changes by simply running:
terraform fmt
to automatically apply formatting to your.tf
files.terraform validate
to let Terraform check that the configuration is valid and doesn't contain any typo.
A pre-commit configuration has been put in place to automate this local checks for you, in case you find it useful. To use pre-commit, simply follow this two steps:
pip install pre-commit
pre-commit install
Now the pre-commit hooks will be triggered before you commit changes to Git.
This project uses tbump to automatically fulfill and
publish a new version. When you are ready to create a new version, make sure you have tbump
installed in your local machine:
pip install tbump
Then just stay in main
branch, in sync with Git repository, and issue the new version. E.g. if you wish to create
version v1.2.3:
tbump 1.2.3
And tbump
will update the version in the project files where it is specified and also create a new Git tag 1.2.3
and push everything to repository with a commit named "Bump to 1.2.3".
It'll also deploy that new version to Google Artifact Registry.
In case you simply want to test, you can run tbump 1.2.3 --dry-run
to see the changes that will be made, without
actually doing anything.