Skip to content

Commit 42b474f

Browse files
committed
ci: Add static IP to Cloud Run instance (#13)
1 parent bd3e0e0 commit 42b474f

File tree

5 files changed

+85
-5
lines changed

5 files changed

+85
-5
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,10 @@ terraform.rc
209209
.terragrunt-cache
210210

211211
# Terraform plans
212-
tfplan
212+
tfplan
213+
214+
# Certificate Authority
215+
cert.pem
216+
ca.pem
217+
218+
*.sql

terraform/cloud_run.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ resource "google_cloud_run_service" "fastapi" {
3131

3232
metadata {
3333
annotations = {
34-
"autoscaling.knative.dev/minScale" = "1"
35-
"autoscaling.knative.dev/maxScale" = "1"
34+
"autoscaling.knative.dev/minScale" = "1"
35+
"autoscaling.knative.dev/maxScale" = "1"
36+
"run.googleapis.com/vpc-access-connector" = google_vpc_access_connector.run_connector.name
37+
"run.googleapis.com/vpc-access-egress" = "all-traffic"
3638
}
3739
}
3840
}

terraform/network.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

terraform/outputs.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
output "cloud_run_url" {
22
value = google_cloud_run_service.fastapi.status[0].url
3-
}
3+
}
4+
5+
output "cloud_run_static_ip" {
6+
value = google_compute_address.cloud_run_static_ip.address
7+
description = "Static IP used by Cloud Run through NAT"
8+
}

terraform/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ variable "enabled_apis" {
2020
"iam.googleapis.com",
2121
"serviceusage.googleapis.com",
2222
"logging.googleapis.com",
23-
"monitoring.googleapis.com"
23+
"monitoring.googleapis.com",
24+
"vpcaccess.googleapis.com"
2425
]
2526
}
2627

0 commit comments

Comments
 (0)