forked from USEPA/haztrak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GCP Cloud SQL terraform module (USEPA#619)
* rename backend to version to be more terraform idiomatic * local SQL module for provisioning GCP Cloud SQL instances and accompanying databases * add options for enabling private IP and VPC connections to our Cloud SQL instance
- Loading branch information
1 parent
1f7af3b
commit c434b70
Showing
8 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
output "network" { | ||
output "id" { | ||
value = module.vpc.network_id | ||
} | ||
|
||
output "name" { | ||
value = module.vpc.network_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
locals { | ||
# database tiers follow legacy sets of "db-custom-<VCPUs>-<RAM in MB>" | ||
database_tier = var.environment == "prod" ? "db-custom-1-3840" : "db-f1-micro" | ||
disk_size = var.environment == "prod" ? 10 : 10 # in GB, 10 GB is the minimum | ||
availability = var.environment == "prod" ? "ZONAL" : "ZONAL" | ||
instance_name = var.environment == "prod" ? "${replace(var.name, "_", "-")}-postgres" : "${replace(var.name, "_", "-")}-postgres-dev" | ||
ip_range_name = "${replace(var.name, "_", "-")}-ip-range" | ||
backup_enabled = var.environment == "prod" ? true : false | ||
deletion_protection = var.environment == "prod" ? true : false | ||
private_vpc_connection = var.enable_private_ip ? 1 : 0 | ||
} | ||
|
||
|
||
resource "google_sql_database_instance" "primary" { | ||
database_version = "POSTGRES_15" | ||
name = var.name | ||
project = var.project | ||
region = var.region | ||
deletion_protection = local.deletion_protection | ||
lifecycle { | ||
ignore_changes = [settings.0.activation_policy] | ||
} | ||
|
||
settings { | ||
activation_policy = "ALWAYS" | ||
availability_type = local.availability | ||
|
||
backup_configuration { | ||
backup_retention_settings { | ||
retained_backups = 3 | ||
retention_unit = "COUNT" | ||
} | ||
|
||
enabled = local.backup_enabled | ||
# location = "us" | ||
point_in_time_recovery_enabled = local.backup_enabled | ||
start_time = "12:00" | ||
transaction_log_retention_days = 3 | ||
} | ||
|
||
disk_autoresize = true | ||
disk_autoresize_limit = 0 | ||
disk_size = 10 | ||
disk_type = "PD_SSD" | ||
|
||
ip_configuration { | ||
ipv4_enabled = true | ||
private_network = var.vpc | ||
} | ||
pricing_plan = "PER_USE" | ||
tier = local.database_tier | ||
} | ||
depends_on = [google_service_networking_connection.sql_vpc_connection] | ||
} | ||
|
||
resource "google_sql_database" "primary" { | ||
for_each = { for o in var.databases : o.name => o } | ||
|
||
name = each.value.name | ||
instance = google_sql_database_instance.primary.name | ||
depends_on = [google_sql_database_instance.primary] | ||
} | ||
|
||
resource "google_compute_global_address" "private_ip_range" { | ||
count = local.private_vpc_connection | ||
name = "${var.name}-ip-range" | ||
purpose = "VPC_PEERING" | ||
address_type = "INTERNAL" | ||
prefix_length = 16 | ||
network = var.vpc | ||
} | ||
|
||
resource "google_service_networking_connection" "sql_vpc_connection" { | ||
count = local.private_vpc_connection | ||
network = var.vpc | ||
service = "servicenetworking.googleapis.com" | ||
reserved_peering_ranges = [ | ||
google_compute_global_address.private_ip_range[0].name | ||
] | ||
depends_on = [google_compute_global_address.private_ip_range] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output "instance" { | ||
value = google_sql_database_instance.primary.name | ||
} | ||
|
||
output "private_ip" { | ||
value = google_compute_global_address.private_ip_range[0].address | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
variable "region" { | ||
description = "The GCP region the database will be hosted in" | ||
type = string | ||
} | ||
|
||
variable "project" { | ||
description = "The project id to deploy to" | ||
type = string | ||
} | ||
|
||
variable "name" { | ||
description = "The name of the database to create" | ||
type = string | ||
} | ||
|
||
variable "databases" { | ||
description = "The names of the databases to create" | ||
type = list(object({ | ||
name = string | ||
})) | ||
} | ||
|
||
variable "environment" { | ||
description = "The environment to deploy to" | ||
type = string | ||
validation { | ||
condition = contains(["dev", "prod"], var.environment) | ||
error_message = "Environment must be one of [dev, prod]" | ||
} | ||
} | ||
|
||
variable "vpc" { | ||
description = "The ID of vpc the database is deployed to" | ||
type = string | ||
} | ||
|
||
variable "enable_private_ip" { | ||
description = "Whether to enable private IP for the sql instance to connect to the provided VPC" | ||
type = bool | ||
default = true | ||
} |