# Two guardrails, each off until you fill in its list: which models may be # used, and Model Armor screening of Gemini calls in chosen projects. # Which models and features the organisation may use. Values take the # documented form model_id:feature_family, such as # publishers/google/models/gemini-1.0-pro:predict. Name every version you use: # in one report, a version-less ID such as gemini-1.5-pro:predict did not # cover that model's numbered versions. A call to anything else fails with # FAILED_PRECONDITION. A change to this policy alerts as guardrails-weakened. resource "google_org_policy_policy" "allowed_models" { count = length(var.allowed_models) > 0 ? 1 : 0 name = "organizations/${var.org_id}/policies/vertexai.allowedModels" parent = "organizations/${var.org_id}" spec { rules { values { allowed_values = var.allowed_models } } } } # Model Armor for Vertex AI in each named project: a floor setting with the # AI_PLATFORM integration that screens for prompt injection, jailbreaks, and # the four responsible AI categories. # # Know its limits. It screens generateContent only. It fails open: when Model # Armor is unavailable, the call goes through unscreened. It does not screen # uploaded documents or files. # # Its platform logs carry the full prompt and response text into Cloud # Logging, in that project's _Default bucket. That is the one way here to keep # Gemini prompt text with Terraform, because the request-response logging for # publisher models has no Terraform resource. Restrict who can read those logs. # # Creating a floor setting is an UpdateFloorSetting call, so the apply that # adds a project should fire guardrails-weakened, unless the identity applying # it is in expected_principals. Terraform cannot delete a floor setting: destroy # leaves it in force, so turn enforcement off by hand to stop screening. data "google_project" "armored" { for_each = toset(var.model_armor_projects) project_id = each.value } resource "google_project_service" "model_armor" { for_each = toset(var.model_armor_projects) project = each.value service = "modelarmor.googleapis.com" disable_on_destroy = false } # Vertex AI calls Model Armor as its own service agent. resource "google_project_iam_member" "model_armor_user" { for_each = data.google_project.armored project = each.key role = "roles/modelarmor.user" member = "serviceAccount:service-${each.value.number}@gcp-sa-aiplatform.iam.gserviceaccount.com" } resource "google_model_armor_floorsetting" "vertex_ai" { for_each = toset(var.model_armor_projects) parent = "projects/${each.value}" location = "global" # False here would also switch off floor settings inherited from a folder # or the organisation. enable_floor_setting_enforcement = true integrated_services = ["AI_PLATFORM"] # Exactly one of inspect_only and inspect_and_block is set. ai_platform_floor_setting { inspect_only = var.model_armor_mode == "inspect_only" ? true : null inspect_and_block = var.model_armor_mode == "inspect_and_block" ? true : null enable_cloud_logging = true } filter_config { pi_and_jailbreak_filter_settings { filter_enforcement = "ENABLED" confidence_level = var.model_armor_confidence } rai_settings { dynamic "rai_filters" { for_each = ["SEXUALLY_EXPLICIT", "HATE_SPEECH", "HARASSMENT", "DANGEROUS"] content { filter_type = rai_filters.value confidence_level = var.model_armor_confidence } } } } depends_on = [ google_project_service.model_armor, google_project_iam_member.model_armor_user, google_monitoring_alert_policy.detection, google_project_iam_member.sink_writes, ] }