From f9d3c551f00b4ee76075043f7072f61e25b8962d Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 15 Oct 2024 21:37:12 -0400 Subject: [PATCH] Add an API gateway. --- terraform/artifact_registry.tf | 1 + terraform/gateway.tf | 55 ++++++++++++++++++++++++++++++++++ terraform/main.tf | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 terraform/gateway.tf diff --git a/terraform/artifact_registry.tf b/terraform/artifact_registry.tf index a5cf750..c47a6eb 100644 --- a/terraform/artifact_registry.tf +++ b/terraform/artifact_registry.tf @@ -14,4 +14,5 @@ resource "google_artifact_registry_repository" "docker_repo" { } } } + depends_on = [google_project_service.service["artifactregistry"], ] } diff --git a/terraform/gateway.tf b/terraform/gateway.tf new file mode 100644 index 0000000..0f232db --- /dev/null +++ b/terraform/gateway.tf @@ -0,0 +1,55 @@ +resource "google_api_gateway_api" "api" { + provider = google-beta + project = google_project.project.project_id + api_id = "the-gateway" + depends_on = [google_project_service.service["apigateway"], ] +} + +resource "google_api_gateway_api_config" "api_config" { + provider = google-beta + project = google_project.project.project_id + api = google_api_gateway_api.api.api_id + api_config_id = "api-config" + + openapi_documents { + document { + path = "spec.yaml" + contents = base64encode(<<-EOF +swagger: "2.0" +info: + title: the-gateway foo + description: "Run auth through Google API Gateway." + version: "1.0.0" +schemes: + - "https" +produces: + - application/json +x-google-backend: + address: ${google_cloud_run_v2_service.api_server.uri} +paths: + "/": + get: + description: "Hello World." + operationId: "helloWorld" + responses: + 200: + description: "Success." + schema: + type: string +EOF + ) + } + } +} + +resource "google_api_gateway_gateway" "gateway" { + provider = google-beta + project = google_project.project.project_id + api_config = google_api_gateway_api_config.api_config.id + gateway_id = "gateway-to-the-api" +} + + +output "gateway_address" { + value = google_api_gateway_gateway.gateway.default_hostname +} diff --git a/terraform/main.tf b/terraform/main.tf index 178b87f..305c8a8 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -74,7 +74,7 @@ resource "google_project" "project" { resource "google_project_service" "service" { project = google_project.project.project_id - for_each = toset(["run"]) + for_each = toset(["run", "artifactregistry", "apigateway"]) service = "${each.key}.googleapis.com" disable_dependent_services = true }