Skip to content

Commit

Permalink
feat: add api gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpuiggarcia committed Sep 2, 2023
1 parent fe0d399 commit 9ea5be0
Showing 1 changed file with 1 addition and 74 deletions.
75 changes: 1 addition & 74 deletions gcp/nandos-api-gateway/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ locals {
config_hash = md5(local.openapi_contents)
}


# Create a service account for the API Gateway
resource "google_service_account" "api_gateway_sa" {
account_id = "${var.api_name}-api-gateway-sa"
Expand Down Expand Up @@ -58,81 +59,7 @@ resource "google_api_gateway_gateway" "nandos_api_gateway" {
region = var.project_region
}

# Create a managed SSL certificate
resource "google_compute_managed_ssl_certificate" "custom_ssl_cert" {
name = "custom-ssl-cert"
project = var.project_id
managed {
domains = [var.custom_domain]
}
}

# Reserve a Global IP Address
resource "google_compute_global_address" "default" {
name = "global-address"
project = var.project_id
}

# HTTP Health Check
resource "google_compute_health_check" "default" {
name = "http-health-check"
timeout_sec = 5
check_interval_sec = 5
http_health_check {
port = "80"
}
}

# Create a Backend Service
resource "google_compute_backend_service" "backend_service" {
name = "backend-service"
enable_cdn = false
port_name = "http"
protocol = "HTTP"
project = var.project_id

backend {
group = google_api_gateway_gateway.nandos_api_gateway.gateway_id
}

health_checks = [
google_compute_health_check.default.self_link
]
}

# Create a URL Map
resource "google_compute_url_map" "url_map" {
name = "url-map"
default_service = google_compute_backend_service.backend_service.self_link
project = var.project_id
}

# HTTPS Proxy
resource "google_compute_target_https_proxy" "https_proxy" {
name = "https-proxy"
url_map = google_compute_url_map.url_map.self_link
ssl_certificates = [google_compute_managed_ssl_certificate.custom_ssl_cert.self_link]
project = var.project_id
}

# Global Forwarding Rule for HTTPS
resource "google_compute_global_forwarding_rule" "https_global_forwarding_rule" {
name = "https-global-forwarding-rule"
target = google_compute_target_https_proxy.https_proxy.self_link
ip_address = google_compute_global_address.default.address
port_range = "443"
project = var.project_id
}

# Output URLs and other details
output "api_gateway_url_text" {
value = "Your API Gateway URL is: ${google_api_gateway_gateway.nandos_api_gateway.default_hostname}"
}

output "custom_domain_text" {
value = "Your custom domain is: ${var.custom_domain}"
}

output "load_balancer_ip" {
value = "Your Load Balancer IP is: ${google_compute_global_address.default.address}"
}

0 comments on commit 9ea5be0

Please sign in to comment.