Files
gcp_solution_architecture_a…/skills/google-cloud-iac-patterns/references/secret-manager.md
Jonathan Boniface 5bb22d984e
Some checks failed
validation / verify (push) Failing after 8s
fixed multile issues
2026-10-07 17:33:36 +01:00

1.3 KiB

Secret Manager

Terraform creates the secret and who may read it. The value is added outside Terraform, so it never enters the state or the repository.

  • replication is required: auto {} for Google-managed locations, or user_managed with one replicas block per location.
  • Grant roles/secretmanager.secretAccessor on the secret, to the one identity that reads it.
  • Never write a google_secret_manager_secret_version with the value in the configuration.
variable "project_id" {
  type        = string
  description = "Project to deploy into."
}

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

resource "google_service_account" "reader" {
  project      = var.project_id
  account_id   = "api-key-reader"
  display_name = "Reads the API key"
}

resource "google_secret_manager_secret" "api_key" {
  project   = var.project_id
  secret_id = "partner-api-key"

  replication {
    user_managed {
      replicas {
        location = var.region
      }
    }
  }
}

resource "google_secret_manager_secret_iam_member" "reader_reads_api_key" {
  project   = var.project_id
  secret_id = google_secret_manager_secret.api_key.secret_id
  role      = "roles/secretmanager.secretAccessor"
  member    = google_service_account.reader.member
}