1.3 KiB
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.
replicationis required:auto {}for Google-managed locations, oruser_managedwith onereplicasblock per location.- Grant
roles/secretmanager.secretAccessoron the secret, to the one identity that reads it. - Never write a
google_secret_manager_secret_versionwith 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
}