Capstone/infra/main.tf

161 lines
4.1 KiB
Terraform
Raw Normal View History

2023-10-23 22:01:09 -04:00
terraform {
required_version = ">= 0.14"
required_providers {
google = ">= 3.3"
}
}
variable "project_id" {
description = "The GCP project ID where the infra will be built"
type = string
}
variable "project_region" {
description = "The GCP region where the infra will be built"
type = string
}
2023-11-19 10:37:45 -05:00
variable "webhook_service_tag" {
description = "Tag for the webhook service collector image"
type = string
}
variable "frontend_service_tag" {
description = "Tag for the frontend service collector image"
type = string
}
2023-10-23 22:01:09 -04:00
provider "google" {
project = var.project_id
}
resource "google_project_service" "run_api" {
service = "run.googleapis.com"
disable_on_destroy = true
}
resource "google_project_service" "artifact_api" {
2023-11-19 10:37:45 -05:00
service = "artifactregistry.googleapis.com"
2023-10-23 22:01:09 -04:00
disable_on_destroy = true
}
2023-10-26 12:35:53 -04:00
resource "google_project_service" "serverless_vpc_api" {
2023-11-19 10:37:45 -05:00
service = "vpcaccess.googleapis.com"
2023-10-26 12:35:53 -04:00
disable_on_destroy = true
}
2023-10-23 22:01:09 -04:00
resource "google_artifact_registry_repository" "capstone_repo" {
location = var.project_region
repository_id = "capstone-repo"
description = "Images for capstone project"
format = "DOCKER"
2023-11-19 10:37:45 -05:00
2023-10-23 22:01:09 -04:00
docker_config {
immutable_tags = false
}
depends_on = [ google_project_service.artifact_api ]
}
2023-10-26 12:35:53 -04:00
resource "google_cloud_run_v2_service" "webhook_service_cr" {
2023-11-19 10:37:45 -05:00
name = "webhook-service-cr"
2023-10-23 22:01:09 -04:00
location = var.project_region
2023-11-19 10:37:45 -05:00
2023-10-23 22:01:09 -04:00
template {
containers {
2023-11-19 10:37:45 -05:00
image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/webhook-service:${var.webhook_service_tag}"
2023-10-23 22:01:09 -04:00
}
}
2023-10-26 13:47:24 -04:00
depends_on = [ google_project_service.run_api, google_artifact_registry_repository.capstone_repo ]
2023-10-23 22:01:09 -04:00
}
2023-10-26 12:35:53 -04:00
resource "google_cloud_run_v2_service_iam_member" "webhook_service_run_all_users" {
project = var.project_id
name = google_cloud_run_v2_service.webhook_service_cr.name
2023-10-23 22:01:09 -04:00
location = var.project_region
role = "roles/run.invoker"
member = "allUsers"
}
2023-10-26 12:35:53 -04:00
resource "google_cloud_run_v2_service" "frontend_service_cr" {
2023-11-19 10:37:45 -05:00
name = "frontend-service-cr"
2023-10-23 22:01:09 -04:00
location = var.project_region
2023-11-19 10:37:45 -05:00
2023-10-23 22:01:09 -04:00
template {
2023-10-26 12:35:53 -04:00
containers {
2023-11-19 10:37:45 -05:00
image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/frontend-service:${var.frontend_service_tag}"
2023-10-26 12:35:53 -04:00
}
2023-10-23 22:01:09 -04:00
}
2023-10-26 13:47:24 -04:00
depends_on = [ google_project_service.run_api, google_artifact_registry_repository.capstone_repo ]
2023-10-23 22:01:09 -04:00
}
2023-10-26 12:35:53 -04:00
resource "google_cloud_run_v2_service_iam_member" "frontend_service_run_all_users" {
project = var.project_id
name = google_cloud_run_v2_service.frontend_service_cr.name
2023-10-23 22:01:09 -04:00
location = var.project_region
role = "roles/run.invoker"
member = "allUsers"
}
2023-10-26 13:47:24 -04:00
data "google_dns_managed_zone" "preston_baxter_zone" {
name = "pbaxter-main-zone"
}
resource "google_dns_record_set" "webhook_cname" {
2023-11-19 21:51:05 -05:00
name = "capstone-webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}"
2023-10-26 13:47:24 -04:00
managed_zone = data.google_dns_managed_zone.preston_baxter_zone.name
type = "CNAME"
ttl = 300
rrdatas = [
"ghs.googlehosted.com."
]
depends_on = [ google_cloud_run_v2_service.webhook_service_cr ]
}
resource "google_dns_record_set" "frontend_cname" {
2023-11-19 21:51:05 -05:00
name = "capstone.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}"
2023-10-26 13:47:24 -04:00
managed_zone = data.google_dns_managed_zone.preston_baxter_zone.name
type = "CNAME"
ttl = 300
rrdatas = [
"ghs.googlehosted.com."
]
depends_on = [ google_cloud_run_v2_service.frontend_service_cr ]
}
resource "google_cloud_run_domain_mapping" "frontend_cname_mapping" {
location = "us-central1"
2023-11-19 21:51:05 -05:00
name = trimsuffix("capstone.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
2023-10-26 13:47:24 -04:00
metadata {
2023-11-19 10:37:45 -05:00
namespace = var.project_id
2023-10-26 13:47:24 -04:00
}
spec {
route_name = google_cloud_run_v2_service.frontend_service_cr.name
}
}
resource "google_cloud_run_domain_mapping" "webhook_cname_mapping" {
location = "us-central1"
2023-11-19 21:51:05 -05:00
name = trimsuffix("capstone-webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
2023-10-26 13:47:24 -04:00
metadata {
2023-11-19 10:37:45 -05:00
namespace = var.project_id
2023-10-26 13:47:24 -04:00
}
2023-11-19 10:37:45 -05:00
2023-10-26 13:47:24 -04:00
spec {
route_name = google_cloud_run_v2_service.webhook_service_cr.name
}
}