-
Notifications
You must be signed in to change notification settings - Fork 0
feat(infra): add Cloud SQL with VPC peering for preview environments #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
msantoroks
wants to merge
3
commits into
develop
Choose a base branch
from
kloudstax/vpcs-security
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+300
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,219 @@ | ||
| # Cloud SQL Database Configuration | ||
| # | ||
| # Strategy: | ||
| # - Dev: Creates and manages app-dev Cloud SQL instance | ||
| # - Preview: Does NOT create database, uses VPC peering to connect to dev database | ||
| # - Prod: Creates and manages app-prod Cloud SQL instance (completely isolated from dev/preview) | ||
|
|
||
| # Cloud SQL Instance for DEV environment | ||
| resource "google_sql_database_instance" "dev" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| name = "app-dev" | ||
| database_version = "POSTGRES_15" | ||
| region = local.region | ||
|
|
||
| settings { | ||
| tier = "db-custom-2-3840" # Dev: 2 vCPUs, 3.75GB RAM | ||
| availability_type = "ZONAL" | ||
| deletion_protection_enabled = false # Allow deletion for dev environment | ||
|
|
||
| # Storage configuration | ||
| disk_type = "PD_SSD" | ||
| disk_size = 50 # 50GB storage for dev | ||
| disk_autoresize = true # Enable autoresize | ||
|
|
||
| # Backup configuration | ||
| backup_configuration { | ||
| enabled = true | ||
| start_time = "03:00" | ||
| point_in_time_recovery_enabled = true | ||
| transaction_log_retention_days = 7 | ||
| backup_retention_settings { | ||
| retained_backups = 7 | ||
| retention_unit = "COUNT" | ||
| } | ||
| } | ||
|
|
||
| # IP configuration - Private IP only via VPC peering | ||
| ip_configuration { | ||
| ipv4_enabled = false | ||
| private_network = google_compute_network.main.id | ||
| enable_private_path_for_google_cloud_services = true | ||
| } | ||
|
|
||
| # Database flags | ||
| database_flags { | ||
| name = "max_connections" | ||
| value = "100" # Appropriate for dev tier | ||
| } | ||
| } | ||
|
|
||
| depends_on = [ | ||
| google_service_networking_connection.private_vpc_connection, | ||
| google_compute_network.main | ||
| ] | ||
|
|
||
| deletion_protection = false | ||
| } | ||
|
|
||
| # Cloud SQL Database for DEV | ||
| resource "google_sql_database" "dev" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| name = "app_db" | ||
| instance = google_sql_database_instance.dev[0].name | ||
| } | ||
|
|
||
| # Generate random password for DEV database | ||
| resource "random_password" "dev_password" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| length = 32 | ||
| special = true | ||
| upper = true | ||
| lower = true | ||
| numeric = true | ||
| } | ||
|
|
||
| # Create secret in Secret Manager for DEV database password | ||
| resource "google_secret_manager_secret" "database_password_dev" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| secret_id = "database-password-dev" | ||
| project = local.project_id | ||
|
|
||
| replication { | ||
| user_managed { | ||
| replicas { | ||
| location = local.region | ||
| } | ||
| } | ||
| } | ||
|
|
||
| labels = merge(local.common_labels, { | ||
| environment = "dev" | ||
| purpose = "database-password" | ||
| }) | ||
| } | ||
|
|
||
| # Store password in Secret Manager | ||
| resource "google_secret_manager_secret_version" "database_password_dev" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| secret = google_secret_manager_secret.database_password_dev[0].id | ||
| secret_data = random_password.dev_password[0].result | ||
| } | ||
|
|
||
| # Cloud SQL User for DEV | ||
| resource "google_sql_user" "dev" { | ||
| count = var.environment == "dev" ? 1 : 0 | ||
| name = "app_user" | ||
| instance = google_sql_database_instance.dev[0].name | ||
| password = random_password.dev_password[0].result | ||
| type = "BUILT_IN" | ||
| } | ||
|
|
||
|
|
||
| # Cloud SQL Instance for PROD environment (completely isolated) | ||
| resource "google_sql_database_instance" "prod" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| name = "app-prod" | ||
| database_version = "POSTGRES_15" | ||
| region = local.region | ||
|
|
||
| settings { | ||
| tier = "db-custom-4-7680" # Prod: 4 vCPUs, 7.5GB RAM | ||
| availability_type = "ZONAL" | ||
| deletion_protection_enabled = true # Protect production database | ||
|
|
||
| # Storage configuration | ||
| disk_type = "PD_SSD" | ||
| disk_size = 100 # 100GB storage for prod | ||
| disk_autoresize = true # Enable autoresize | ||
|
|
||
| # Backup configuration | ||
| backup_configuration { | ||
| enabled = true | ||
| start_time = "03:00" | ||
| point_in_time_recovery_enabled = true | ||
| transaction_log_retention_days = 7 | ||
| backup_retention_settings { | ||
| retained_backups = 7 | ||
| retention_unit = "COUNT" | ||
| } | ||
| } | ||
|
|
||
| # IP configuration - Private IP only via VPC peering | ||
| ip_configuration { | ||
| ipv4_enabled = false | ||
| private_network = google_compute_network.main.id | ||
| enable_private_path_for_google_cloud_services = true | ||
| } | ||
|
|
||
| # Database flags | ||
| database_flags { | ||
| name = "max_connections" | ||
| value = "200" # Higher for production tier | ||
| } | ||
| } | ||
|
|
||
| depends_on = [ | ||
| google_service_networking_connection.private_vpc_connection, | ||
| google_compute_network.main | ||
| ] | ||
|
|
||
| deletion_protection = true # Critical: Protect production database | ||
| } | ||
|
|
||
| # Cloud SQL Database for PROD | ||
| resource "google_sql_database" "prod" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| name = "app_db" | ||
| instance = google_sql_database_instance.prod[0].name | ||
| } | ||
|
|
||
| # Generate random password for PROD database | ||
| resource "random_password" "prod_password" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| length = 32 | ||
| special = true | ||
| upper = true | ||
| lower = true | ||
| numeric = true | ||
| } | ||
|
|
||
| # Create secret in Secret Manager for PROD database password | ||
| resource "google_secret_manager_secret" "database_password_prod" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| secret_id = "database-password-prod" | ||
| project = local.project_id | ||
|
|
||
| replication { | ||
| user_managed { | ||
| replicas { | ||
| location = local.region | ||
| } | ||
| } | ||
| } | ||
|
|
||
| labels = merge(local.common_labels, { | ||
| environment = "prod" | ||
| purpose = "database-password" | ||
| }) | ||
| } | ||
|
|
||
| # Store password in Secret Manager | ||
| resource "google_secret_manager_secret_version" "database_password_prod" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| secret = google_secret_manager_secret.database_password_prod[0].id | ||
| secret_data = random_password.prod_password[0].result | ||
| } | ||
|
|
||
| # Cloud SQL User for PROD | ||
| resource "google_sql_user" "prod" { | ||
| count = var.environment == "prod" ? 1 : 0 | ||
| name = "app_user" | ||
| instance = google_sql_database_instance.prod[0].name | ||
| password = random_password.prod_password[0].result | ||
| type = "BUILT_IN" | ||
| } | ||
|
|
||
| # Note: VPC Peering configuration has been moved to vpc.tf for better organization | ||
| # See vpc.tf for preview-to-dev and dev-to-preview peering resources | ||
|
|
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not clear why you are using VPC peering at all. Can't the Cloud SQL instance authorize a specific CIDR ranges. Since preview uses 10.1.0.0/16, why not just add that to the Cloud SQL's authorized networks?
With the current setup I think we would have to manage the dev->preview VPC connection as well right?
I might be a bit ignorant here, anything you can share about why bidirectional VPC peering is a good idea might help me understand better.