|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# VPC Configuration for Cloud Run Egress via NAT |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | + |
| 5 | +# Creates a custom VPC network (no auto subnet creation) |
| 6 | +resource "google_compute_network" "run_vpc" { |
| 7 | + name = "sightcall-qa-api-vpc" |
| 8 | + auto_create_subnetworks = false |
| 9 | +} |
| 10 | + |
| 11 | +# Subnet used specifically for the Serverless VPC Access Connector |
| 12 | +resource "google_compute_subnetwork" "run_subnet" { |
| 13 | + name = "sightcall-qa-api-subnet" |
| 14 | + ip_cidr_range = "10.10.1.0/28" |
| 15 | + region = var.region |
| 16 | + network = google_compute_network.run_vpc.id |
| 17 | +} |
| 18 | + |
| 19 | +# ----------------------------------------------------------------------------- |
| 20 | +# Serverless VPC Access Connector |
| 21 | +# ----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +# Enables Cloud Run to access resources in the VPC |
| 24 | +resource "google_vpc_access_connector" "run_connector" { |
| 25 | + name = "scqa-connector" |
| 26 | + region = var.region |
| 27 | + network = google_compute_network.run_vpc.name |
| 28 | + ip_cidr_range = "10.10.1.0/28" # must match subnet range |
| 29 | + min_throughput = 200 |
| 30 | + max_throughput = 300 |
| 31 | + |
| 32 | + lifecycle { |
| 33 | + create_before_destroy = true |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +# ----------------------------------------------------------------------------- |
| 38 | +# Static IP Address and NAT Configuration |
| 39 | +# ----------------------------------------------------------------------------- |
| 40 | + |
| 41 | +# Reserves a static external IP address for outbound traffic |
| 42 | +resource "google_compute_address" "cloud_run_static_ip" { |
| 43 | + name = "sightcall-qa-api-static-ip" |
| 44 | + region = var.region |
| 45 | +} |
| 46 | + |
| 47 | +# Creates a Cloud Router to support Cloud NAT |
| 48 | +resource "google_compute_router" "run_router" { |
| 49 | + name = "sightcall-qa-api-router" |
| 50 | + region = var.region |
| 51 | + network = google_compute_network.run_vpc.id |
| 52 | +} |
| 53 | + |
| 54 | +# Configures Cloud NAT to route egress traffic through the static IP |
| 55 | +resource "google_compute_router_nat" "run_nat" { |
| 56 | + name = "sightcall-qa-api-nat" |
| 57 | + router = google_compute_router.run_router.name |
| 58 | + region = var.region |
| 59 | + |
| 60 | + # Manually assign our reserved static IP |
| 61 | + nat_ip_allocate_option = "MANUAL_ONLY" |
| 62 | + nat_ips = [google_compute_address.cloud_run_static_ip.id] |
| 63 | + |
| 64 | + # Apply NAT to all subnetworks and IP ranges in the VPC |
| 65 | + source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES" |
| 66 | +} |
0 commit comments