Skip to content

Commit 90e22cb

Browse files
committed
Add PSC environment
1 parent 9bb8269 commit 90e22cb

4 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Test Environments
2+
3+
This directory contains Terraform configurations to provision environments for testing the BigQuery JDBC driver.
4+
5+
## Available Environments
6+
7+
### Private Service Connect (PSC)
8+
9+
Located in `private_service_connect/`.
10+
11+
This environment provisions:
12+
- A custom VPC and subnet.
13+
- Cloud NAT (allowing outbound internet access without public IPs).
14+
- A Private Service Connect (PSC) endpoint for Google APIs (`all-apis`).
15+
- A Compute Engine VM instance with no public IP, accessible via IAP (Identity-Aware Proxy).
16+
- Firewall rules to allow IAP SSH access.
17+
18+
This setup is useful for testing connectivity to BigQuery via PSC and validating that traffic goes through the private endpoint.
19+
20+
## Deployment
21+
22+
To deploy an environment, you need Terraform installed and configured with Google Cloud credentials.
23+
24+
### Steps
25+
26+
1. Navigate to the specific environment directory:
27+
```bash
28+
cd tools/environments/private_service_connect
29+
```
30+
31+
2. Initialize Terraform:
32+
```bash
33+
terraform init
34+
```
35+
36+
3. Create a `terraform.tfvars` file or pass variables on the command line.
37+
Required variables:
38+
- `project_id`: The GCP project ID where resources will be created.
39+
40+
Optional variables (see `variables.tf` for defaults):
41+
- `region`: Defaults to `us-central1`.
42+
- `zone`: Defaults to `us-central1-a`.
43+
- `env_name`: Defaults to `demo`.
44+
45+
Example `terraform.tfvars`:
46+
```hcl
47+
project_id = "your-gcp-project-id"
48+
region = "us-central1"
49+
zone = "us-central1-a"
50+
env_name = "jdbc-test"
51+
```
52+
53+
4. Plan the deployment:
54+
```bash
55+
terraform plan
56+
```
57+
58+
5. Apply the configuration:
59+
```bash
60+
terraform apply
61+
```
62+
63+
6. When done, you can destroy the environment:
64+
```bash
65+
terraform destroy
66+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
provider "google" {
2+
project = var.project_id
3+
region = var.region
4+
}
5+
6+
# 1. VPC Network
7+
resource "google_compute_network" "vpc" {
8+
name = "${var.env_name}-vpc"
9+
auto_create_subnetworks = false
10+
}
11+
12+
# 2. Subnet
13+
resource "google_compute_subnetwork" "subnet" {
14+
name = "${var.env_name}-subnet"
15+
ip_cidr_range = "10.0.0.0/24"
16+
region = var.region
17+
network = google_compute_network.vpc.id
18+
}
19+
20+
# 3. Cloud NAT for Internet Access (No Public IP for VM)
21+
resource "google_compute_router" "router" {
22+
name = "${var.env_name}-router"
23+
region = var.region
24+
network = google_compute_network.vpc.id
25+
}
26+
27+
resource "google_compute_router_nat" "nat" {
28+
name = "${var.env_name}-nat"
29+
router = google_compute_router.router.name
30+
region = var.region
31+
nat_ip_allocate_option = "AUTO_ONLY"
32+
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
33+
}
34+
35+
# 4. Private Service Connect for Google APIs
36+
resource "google_compute_global_address" "psc_ip" {
37+
name = "${var.env_name}-psc-ip"
38+
address_type = "INTERNAL"
39+
purpose = "PRIVATE_SERVICE_CONNECT"
40+
network = google_compute_network.vpc.id
41+
address = "10.0.1.2" # Using a distinct IP outside the subnet range or just let it auto-allocate if possible, but purpose requires specific handling. Actually, global address for PSC can be any internal IP. Let's auto-allocate or use a specific one.
42+
}
43+
44+
resource "google_compute_global_forwarding_rule" "psc_rule" {
45+
name = "${var.env_name}" # This name generates the p.googleapis.com endpoint
46+
target = "all-apis"
47+
network = google_compute_network.vpc.id
48+
ip_address = google_compute_global_address.psc_ip.id
49+
load_balancing_scheme = ""
50+
}
51+
52+
# 5. Firewall for IAP SSH
53+
resource "google_compute_firewall" "iap_ssh" {
54+
name = "${var.env_name}-allow-iap-ssh"
55+
network = google_compute_network.vpc.id
56+
57+
allow {
58+
protocol = "tcp"
59+
ports = ["22"]
60+
}
61+
62+
source_ranges = ["172.253.30.0/23"] # IAP range
63+
}
64+
65+
# 6. VM Instance
66+
resource "google_compute_instance" "vm" {
67+
name = "${var.env_name}-vm"
68+
machine_type = "e2-medium"
69+
zone = var.zone
70+
71+
boot_disk {
72+
initialize_params {
73+
image = "debian-cloud/debian-12"
74+
}
75+
}
76+
77+
network_interface {
78+
subnetwork = google_compute_subnetwork.subnet.id
79+
# No access_config block ensures no public IP
80+
}
81+
82+
metadata = {
83+
enable-oslogin = "TRUE"
84+
}
85+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
output "psc_endpoint" {
2+
value = "https://bigquery-${var.env_name}.p.googleapis.com"
3+
description = "The expected Private Service Connect endpoint for BigQuery."
4+
}
5+
6+
output "psc_ip_address" {
7+
value = google_compute_global_address.psc_ip.address
8+
description = "The internal IP address reserved for Private Service Connect."
9+
}
10+
11+
output "vm_connect" {
12+
value = "ssh kirl_google_com@nic0.${google_compute_instance.vm.name}.${google_compute_instance.vm.zone}.c.${var.project_id}.internal.gcpnode.com"
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
variable "project_id" {
2+
type = string
3+
description = "The GCP project ID where resources will be created."
4+
}
5+
6+
variable "region" {
7+
type = string
8+
description = "The region where resources will be created."
9+
default = "us-central1"
10+
}
11+
12+
variable "zone" {
13+
type = string
14+
description = "The zone where the VM will be created."
15+
default = "us-central1-a"
16+
}
17+
18+
variable "env_name" {
19+
type = string
20+
description = "Identifier used as prefix/suffix for resource names to easily identify them."
21+
default = "demo"
22+
}

0 commit comments

Comments
 (0)