Files
gcp_solution_architecture_a…/skills/google-cloud-iac-patterns/references/firestore.md
Jonathan Boniface 71da0ed504
Some checks failed
validation / verify (push) Failing after 12s
chore: housekeeping from the runs of 2026-10-09
- An Execution's files are written as each Deliverable is submitted, marked
  in_progress, so a killed process or one waiting for its user leaves them on
  disk. A write removes files an earlier write left that it no longer has.
- A question about delivered work is answered from that Execution's solution
  guide by the reply writer; a question about the service is answered as
  before. Nothing is started for either.
- Lessons: prove writes only for lessons the Skill holds; the Skill records
  the vendored Skill versions the lessons were read against, status names any
  that changed, and a test fails until they are reviewed.
- Four more code patterns: Firestore, BigQuery, Eventarc and GKE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 11:44:38 +01:00

2.5 KiB

Firestore

A Firestore database in Native mode, with point-in-time recovery, a scheduled backup, a composite index and the grant a workload needs.

  • A project's first database is named (default). Any other name creates an additional database, which clients must name when they connect.
  • location_id is a region or a multi-region such as eur3, and cannot be changed afterwards.
  • type is FIRESTORE_NATIVE or DATASTORE_MODE, and cannot be changed once the database holds data.
  • delete_protection_state is the guard in the API; deletion_policy = "DELETE" is needed before Terraform will remove a database at all.
  • point_in_time_recovery_enablement keeps seven days of versions. A backup schedule is separate and keeps whole backups for retention, in seconds with an s.
  • A workload reads and writes with roles/datastore.user, granted on the project. There is no per-database role; restrict with an IAM condition on the database name.
variable "project_id" {
  type        = string
  description = "Project to deploy into."
}

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

variable "app_service_account_email" {
  type        = string
  description = "Service account of the workload that uses the database."
}

resource "google_firestore_database" "app" {
  project     = var.project_id
  name        = "app"
  location_id = var.region
  type        = "FIRESTORE_NATIVE"

  concurrency_mode                  = "OPTIMISTIC"
  point_in_time_recovery_enablement = "POINT_IN_TIME_RECOVERY_ENABLED"
  delete_protection_state           = "DELETE_PROTECTION_ENABLED"
  deletion_policy                   = "ABANDON"
}

resource "google_firestore_backup_schedule" "daily" {
  project   = var.project_id
  database  = google_firestore_database.app.name
  retention = "1209600s"

  daily_recurrence {}
}

resource "google_firestore_index" "orders_by_customer" {
  project    = var.project_id
  database   = google_firestore_database.app.name
  collection = "orders"

  fields {
    field_path = "customer_id"
    order      = "ASCENDING"
  }

  fields {
    field_path = "created_at"
    order      = "DESCENDING"
  }
}

resource "google_project_iam_member" "app_uses_firestore" {
  project = var.project_id
  role    = "roles/datastore.user"
  member  = "serviceAccount:${var.app_service_account_email}"

  condition {
    title      = "only-the-app-database"
    expression = "resource.name == \"${google_firestore_database.app.id}\" || resource.name.startsWith(\"${google_firestore_database.app.id}/\")"
  }
}