main.tf 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Minimal GCP Cloud Run Terraform configuration for Llama deployment
  2. # This creates only the essential resources for Cloud Run deployment
  3. terraform {
  4. required_version = ">= 1.0"
  5. required_providers {
  6. google = {
  7. source = "hashicorp/google"
  8. version = "~> 6.0"
  9. }
  10. }
  11. }
  12. provider "google" {
  13. project = var.project_id
  14. region = var.region
  15. }
  16. # Local values
  17. locals {
  18. name_prefix = "${var.project_name}-${var.environment}"
  19. # Required APIs for Cloud Run
  20. required_apis = [
  21. "run.googleapis.com",
  22. "artifactregistry.googleapis.com",
  23. "iam.googleapis.com"
  24. ]
  25. }
  26. # Enable required Google Cloud APIs
  27. resource "google_project_service" "cloud_run_apis" {
  28. for_each = toset(local.required_apis)
  29. project = var.project_id
  30. service = each.value
  31. disable_dependent_services = false
  32. disable_on_destroy = false
  33. }
  34. # Artifact Registry repository for container images
  35. resource "google_artifact_registry_repository" "llama_repository" {
  36. repository_id = "${local.name_prefix}-repo"
  37. format = "DOCKER"
  38. location = var.region
  39. description = "Container repository for Llama inference images"
  40. labels = {
  41. project = var.project_name
  42. environment = var.environment
  43. managed-by = "terraform"
  44. }
  45. depends_on = [google_project_service.cloud_run_apis]
  46. }
  47. # Service account for Cloud Run service
  48. resource "google_service_account" "cloud_run_sa" {
  49. account_id = "${local.name_prefix}-run-sa"
  50. display_name = "Cloud Run Service Account for ${var.project_name}"
  51. description = "Service account for Llama Cloud Run deployment"
  52. depends_on = [google_project_service.cloud_run_apis]
  53. }
  54. # IAM role bindings for Cloud Run service account
  55. resource "google_project_iam_member" "cloud_run_sa_roles" {
  56. for_each = toset([
  57. "roles/logging.logWriter",
  58. "roles/monitoring.metricWriter",
  59. "roles/artifactregistry.reader"
  60. ])
  61. project = var.project_id
  62. role = each.value
  63. member = "serviceAccount:${google_service_account.cloud_run_sa.email}"
  64. }
  65. # Cloud Run service
  66. resource "google_cloud_run_v2_service" "llama_service" {
  67. name = "${local.name_prefix}-service"
  68. location = var.region
  69. template {
  70. service_account = google_service_account.cloud_run_sa.email
  71. containers {
  72. image = var.container_image
  73. # Resource allocation
  74. resources {
  75. limits = {
  76. cpu = var.cpu_limit
  77. memory = var.memory_limit
  78. }
  79. }
  80. # Environment variables
  81. dynamic "env" {
  82. for_each = var.environment_variables
  83. content {
  84. name = env.key
  85. value = env.value
  86. }
  87. }
  88. # Container port
  89. ports {
  90. container_port = var.container_port
  91. name = "http1"
  92. }
  93. }
  94. # Service scaling configuration
  95. scaling {
  96. min_instance_count = var.min_instances
  97. max_instance_count = var.max_instances
  98. }
  99. # Execution environment
  100. execution_environment = var.execution_environment
  101. }
  102. # Traffic configuration
  103. traffic {
  104. percent = 100
  105. type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
  106. }
  107. labels = {
  108. project = var.project_name
  109. environment = var.environment
  110. managed-by = "terraform"
  111. }
  112. depends_on = [google_project_service.cloud_run_apis]
  113. }
  114. # IAM policy for public access (optional)
  115. resource "google_cloud_run_v2_service_iam_member" "public_access" {
  116. count = var.allow_public_access ? 1 : 0
  117. location = google_cloud_run_v2_service.llama_service.location
  118. name = google_cloud_run_v2_service.llama_service.name
  119. role = "roles/run.invoker"
  120. member = "allUsers"
  121. }
  122. # IAM policy for authenticated access
  123. resource "google_cloud_run_v2_service_iam_member" "authenticated_access" {
  124. for_each = toset(var.allowed_members)
  125. location = google_cloud_run_v2_service.llama_service.location
  126. name = google_cloud_run_v2_service.llama_service.name
  127. role = "roles/run.invoker"
  128. member = each.value
  129. }