2024-12-06 16:54:49 +01:00
|
|
|
resource "google_compute_network" "my_new_vpc" {
|
|
|
|
name = "my-new-vpc"
|
2024-12-04 17:26:53 +01:00
|
|
|
auto_create_subnetworks = false
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_subnetwork" "frontend" {
|
|
|
|
name = "frontend"
|
|
|
|
ip_cidr_range = var.frontend_cidr
|
|
|
|
region = var.region
|
2024-12-06 16:54:49 +01:00
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_subnetwork" "backend" {
|
|
|
|
name = "backend"
|
|
|
|
ip_cidr_range = var.backend_cidr
|
|
|
|
region = var.region
|
2024-12-06 16:54:49 +01:00
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_subnetwork" "database" {
|
|
|
|
name = "database"
|
|
|
|
ip_cidr_range = var.database_cidr
|
|
|
|
region = var.region
|
2024-12-06 16:54:49 +01:00
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
}
|
|
|
|
|
2024-12-06 16:54:49 +01:00
|
|
|
resource "google_compute_firewall" "web_rule" {
|
|
|
|
name = "web-rule"
|
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["80", "443"]
|
|
|
|
}
|
2024-12-06 16:54:49 +01:00
|
|
|
source_ranges = [var.web_source_ranges]
|
|
|
|
target_tags = ["frontend"]
|
2024-12-04 17:26:53 +01:00
|
|
|
}
|
|
|
|
|
2024-12-06 16:54:49 +01:00
|
|
|
resource "google_compute_firewall" "ssh_rule" {
|
|
|
|
name = "ssh-rule"
|
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["22"]
|
|
|
|
}
|
2024-12-06 16:54:49 +01:00
|
|
|
source_ranges = [var.ssh_source_ranges]
|
|
|
|
target_tags = ["ssh"]
|
2024-12-04 17:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_firewall" "frontend_to_backend" {
|
|
|
|
name = "frontend-to-backend"
|
2024-12-06 16:54:49 +01:00
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["8000"]
|
|
|
|
}
|
|
|
|
source_ranges = [
|
|
|
|
google_compute_subnetwork.frontend.ip_cidr_range
|
|
|
|
]
|
|
|
|
target_tags = ["backend"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_compute_firewall" "backend_to_database" {
|
|
|
|
name = "backend-to-database"
|
2024-12-06 16:54:49 +01:00
|
|
|
network = google_compute_network.my_new_vpc.id
|
2024-12-04 17:26:53 +01:00
|
|
|
allow {
|
|
|
|
protocol = "tcp"
|
|
|
|
ports = ["3306"]
|
|
|
|
}
|
|
|
|
source_ranges = [
|
|
|
|
google_compute_subnetwork.backend.ip_cidr_range
|
|
|
|
]
|
|
|
|
target_tags = ["db"]
|
|
|
|
}
|