Files
solution_architecture_agent/skills/google-cloud-iac-patterns/references/kms.md
Jonathan Boniface c84dbdea20 feat: the solution architecture agent on the shared Execution engine
The reference agent's subject, cloud solution architecture, as an agent in
the operating model agent's pattern: the Execution engine (from
orchestration_engine) and one Workflow in app/solution_architecture,
built for the settings and provider bundle by assembly.workflow_for.

Copied from gcp_solution_architecture_agent at 8b397cd without its own
engine: its role files, Skills, provider bundle, ADRs, vocabulary, scripts
and evaluation datasets. The image takes the engine from a named build
context and installs Terraform. It is called through the Orchestration
engine, and its files are what the operating model agent digests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 19:49:28 +01:00

2.6 KiB

Cloud KMS

A key ring and a key for customer-managed encryption, and the grant that lets a product use it.

  • The key ring must be in the same location as the resource it protects.
  • Key rings and keys cannot be deleted. prevent_destroy stops Terraform trying.
  • rotation_period is in seconds, such as "7776000s" for 90 days.
  • The product's service agent, not the workload's service account, needs roles/cloudkms.cryptoKeyEncrypterDecrypter on the key. The resource that uses the key must depends_on that grant.
  • Service agents: Pub/Sub is service-NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com, Cloud Run is service-NUMBER@serverless-robot-prod.iam.gserviceaccount.com, Cloud Storage comes from the google_storage_project_service_account data source.
variable "project_id" {
  type        = string
  description = "Project to deploy into."
}

variable "region" {
  type        = string
  description = "Region for regional resources."
}

data "google_project" "this" {
  project_id = var.project_id
}

data "google_storage_project_service_account" "this" {
  project = var.project_id
}

resource "google_kms_key_ring" "workload" {
  project  = var.project_id
  name     = "workload"
  location = var.region
}

resource "google_kms_crypto_key" "data" {
  name            = "data"
  key_ring        = google_kms_key_ring.workload.id
  purpose         = "ENCRYPT_DECRYPT"
  rotation_period = "7776000s"

  lifecycle {
    prevent_destroy = true
  }
}

resource "google_kms_crypto_key_iam_member" "pubsub_uses_key" {
  crypto_key_id = google_kms_crypto_key.data.id
  role          = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
  member        = "serviceAccount:service-${data.google_project.this.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
}

resource "google_kms_crypto_key_iam_member" "storage_uses_key" {
  crypto_key_id = google_kms_crypto_key.data.id
  role          = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
  member        = "serviceAccount:${data.google_storage_project_service_account.this.email_address}"
}

resource "google_pubsub_topic" "encrypted" {
  project      = var.project_id
  name         = "encrypted-events"
  kms_key_name = google_kms_crypto_key.data.id

  depends_on = [google_kms_crypto_key_iam_member.pubsub_uses_key]
}

resource "google_storage_bucket" "encrypted" {
  project                     = var.project_id
  name                        = "${var.project_id}-encrypted"
  location                    = var.region
  uniform_bucket_level_access = true

  encryption {
    default_kms_key_name = google_kms_crypto_key.data.id
  }

  depends_on = [google_kms_crypto_key_iam_member.storage_uses_key]
}