Capstone/infra/main.tf

167 lines
4.2 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
}
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" {
service = "artifactregistry.googleapis.com"
disable_on_destroy = true
}
2023-10-26 12:35:53 -04:00
resource "google_project_service" "serverless_vpc_api" {
service = "vpcaccess.googleapis.com"
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"
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-10-23 22:01:09 -04:00
name = "webhook-service-cr"
location = var.project_region
2023-10-26 13:47:24 -04:00
launch_stage = "BETA"
2023-10-23 22:01:09 -04:00
template {
containers {
image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/webhook-service:latest"
}
2023-10-26 13:47:24 -04:00
vpc_access{
network_interfaces {
network = "main"
subnetwork = "application"
}
2023-10-26 12:35:53 -04:00
egress = "ALL_TRAFFIC"
}
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-10-23 22:01:09 -04:00
name = "frontend-service-cr"
location = var.project_region
2023-10-26 13:47:24 -04:00
launch_stage = "BETA"
2023-10-23 22:01:09 -04:00
template {
2023-10-26 12:35:53 -04:00
containers {
image = "${var.project_region}-docker.pkg.dev/${var.project_id}/${google_artifact_registry_repository.capstone_repo.name}/frontend-service:latest"
}
2023-10-26 13:47:24 -04:00
vpc_access{
network_interfaces {
network = "main"
subnetwork = "application"
}
2023-10-26 12:35:53 -04:00
egress = "ALL_TRAFFIC"
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" {
name = "webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}"
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" {
name = "frontend.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}"
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"
name = trimsuffix("frontend.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
metadata {
namespace = var.project_id
}
spec {
route_name = google_cloud_run_v2_service.frontend_service_cr.name
}
}
resource "google_cloud_run_domain_mapping" "webhook_cname_mapping" {
location = "us-central1"
name = trimsuffix("webhook.${data.google_dns_managed_zone.preston_baxter_zone.dns_name}", ".")
metadata {
namespace = var.project_id
}
spec {
route_name = google_cloud_run_v2_service.webhook_service_cr.name
}
}