OSVauco/infrastructure/terraform/modules/vauco-bootstrap/main.tf

108 lines
3.1 KiB
HCL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================================================================
# vauco-bootstrap — gjenbrukbar modul for alle OSVauco-kundeprosjekter
# Oppretter: GCP-prosjekt, APIs, IAM, Cloud Run, billing-budsjett
# ==============================================================================
locals {
labels = {
managed-by = "terraform"
customer = var.customer_id
environment = "prod"
}
}
# ------------------------------------------------------------------------------
# GCP-prosjekt
# ------------------------------------------------------------------------------
resource "google_project" "customer" {
name = var.customer_id
project_id = var.project_id
billing_account = var.billing_account_id
labels = local.labels
}
# ------------------------------------------------------------------------------
# Aktiver nødvendige APIs
# ------------------------------------------------------------------------------
resource "google_project_service" "apis" {
for_each = toset([
"run.googleapis.com",
"artifactregistry.googleapis.com",
"secretmanager.googleapis.com",
"cloudresourcemanager.googleapis.com",
"iam.googleapis.com",
"dialogflow.googleapis.com",
])
project = google_project.customer.project_id
service = each.value
disable_on_destroy = false
}
# ------------------------------------------------------------------------------
# Service account for Cloud Run
# ------------------------------------------------------------------------------
resource "google_service_account" "cloud_run_sa" {
project = google_project.customer.project_id
account_id = "${var.customer_id}-run-sa"
display_name = "Cloud Run SA ${var.customer_id}"
depends_on = [google_project_service.apis]
}
# ------------------------------------------------------------------------------
# Cloud Run service
# ------------------------------------------------------------------------------
resource "google_cloud_run_v2_service" "app" {
name = "${var.customer_id}-app"
location = var.region
project = google_project.customer.project_id
template {
service_account = google_service_account.cloud_run_sa.email
containers {
image = var.container_image
resources {
limits = {
cpu = "1"
memory = "512Mi"
}
}
}
}
labels = local.labels
depends_on = [google_project_service.apis]
}
# ------------------------------------------------------------------------------
# Billing-budsjett med e-postvarsel
# ------------------------------------------------------------------------------
resource "google_billing_budget" "customer" {
billing_account = var.billing_account_id
display_name = "Budget ${var.customer_id}"
budget_filter {
projects = ["projects/${google_project.customer.number}"]
}
amount {
specified_amount {
currency_code = "NOK"
units = "500"
}
}
threshold_rules {
threshold_percent = 0.8
}
all_updates_rule {
monitoring_notification_channels = []
disable_default_iam_recipients = false
}
}