chore: housekeeping from the runs of 2026-10-09
Some checks failed
validation / verify (push) Failing after 12s

- 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>
This commit is contained in:
2026-10-10 11:44:38 +01:00
parent 10d23a5f91
commit 71da0ed504
24 changed files with 1109 additions and 43 deletions

View File

@@ -4,10 +4,10 @@ description: >-
Terraform patterns for Google Cloud products that validate against one pinned
version of the hashicorp/google provider: Cloud Run services and jobs,
Pub/Sub, Cloud Storage, IAM, Secret Manager, Cloud KMS, Cloud SQL,
networking, load balancing, Cloud Scheduler, Artifact Registry, Monitoring and API
enablement. Use when writing Terraform for a Google Cloud product, before
writing the resource, to start from a pattern whose arguments are known to be
in the right place.
networking, load balancing, Cloud Scheduler, Artifact Registry, Monitoring,
Firestore, BigQuery, Eventarc, GKE and API enablement. Use when writing
Terraform for a Google Cloud product, before writing the resource, to start
from a pattern whose arguments are known to be in the right place.
metadata:
scope: deployment guidance
provider: hashicorp/google 8.6.0
@@ -39,6 +39,10 @@ Every pattern here passes `terraform validate` against `hashicorp/google` 8.6.0.
| Cloud Scheduler | `references/cloud-scheduler.md` |
| Artifact Registry | `references/artifact-registry.md` |
| Cloud Monitoring and Logging | `references/monitoring.md` |
| Firestore | `references/firestore.md` |
| BigQuery | `references/bigquery.md` |
| Eventarc | `references/eventarc.md` |
| Google Kubernetes Engine | `references/gke.md` |
A product with no reference has no pattern yet: write it from `terraform_schema` and the product documentation.

View File

@@ -0,0 +1,92 @@
# BigQuery
A dataset, a partitioned and clustered table, and the grants for a writer and a reader.
- `location` is a region or `EU`/`US`, and cannot be changed. Queries cannot join datasets in different locations.
- `delete_contents_on_destroy = false` makes Terraform refuse to remove a dataset that still has tables.
- A table's `deletion_protection` is true unless set; take it from a variable so a test deployment can be removed.
- `schema` is a JSON string. Changing a column's type or mode replaces the table.
- Partition on a `DATE` or `TIMESTAMP` column with `time_partitioning`, and set `require_partition_filter` so a query cannot scan every partition. `expiration_ms` there removes old partitions.
- Grant on the dataset with `google_bigquery_dataset_iam_member`. A principal that runs queries also needs `roles/bigquery.jobUser` on the project.
- Do not mix `google_bigquery_dataset_iam_member` with `access` blocks on the dataset: each removes what the other sets.
```hcl
variable "project_id" {
type = string
description = "Project to deploy into."
}
variable "region" {
type = string
description = "Region for regional resources."
}
variable "writer_service_account_email" {
type = string
description = "Service account that loads rows."
}
variable "reader_service_account_email" {
type = string
description = "Service account that queries the dataset."
}
variable "deletion_protection" {
type = bool
description = "Whether the table is protected from deletion."
default = true
}
resource "google_bigquery_dataset" "analytics" {
project = var.project_id
dataset_id = "analytics"
location = var.region
delete_contents_on_destroy = false
default_table_expiration_ms = null
max_time_travel_hours = 168
}
resource "google_bigquery_table" "events" {
project = var.project_id
dataset_id = google_bigquery_dataset.analytics.dataset_id
table_id = "events"
deletion_protection = var.deletion_protection
require_partition_filter = true
clustering = ["event_type"]
time_partitioning {
type = "DAY"
field = "event_time"
expiration_ms = 34560000000
}
schema = jsonencode([
{ name = "event_id", type = "STRING", mode = "REQUIRED" },
{ name = "event_type", type = "STRING", mode = "REQUIRED" },
{ name = "event_time", type = "TIMESTAMP", mode = "REQUIRED" },
{ name = "payload", type = "JSON", mode = "NULLABLE" }
])
}
resource "google_bigquery_dataset_iam_member" "writer" {
project = var.project_id
dataset_id = google_bigquery_dataset.analytics.dataset_id
role = "roles/bigquery.dataEditor"
member = "serviceAccount:${var.writer_service_account_email}"
}
resource "google_bigquery_dataset_iam_member" "reader" {
project = var.project_id
dataset_id = google_bigquery_dataset.analytics.dataset_id
role = "roles/bigquery.dataViewer"
member = "serviceAccount:${var.reader_service_account_email}"
}
resource "google_project_iam_member" "reader_runs_queries" {
project = var.project_id
role = "roles/bigquery.jobUser"
member = "serviceAccount:${var.reader_service_account_email}"
}
```

View File

@@ -0,0 +1,129 @@
# Eventarc
A trigger that delivers events to a Cloud Run service: one for messages on a Pub/Sub topic, one for objects finalised in a bucket.
- A trigger has its own service account. It needs `roles/eventarc.eventReceiver` on the project and `roles/run.invoker` on the service it calls.
- For a Pub/Sub trigger, name an existing topic under `transport.pubsub.topic`; left out, Eventarc creates a topic you do not manage.
- For a Cloud Storage trigger, `location` must be the bucket's location, and the Cloud Storage service agent needs `roles/pubsub.publisher` on the project before the trigger is created.
- `matching_criteria` attributes are exact. `type` is always present; a storage event also needs `bucket`.
- The destination service is named, with its `region`; `path` is optional.
- The service must not allow unauthenticated calls for this to be the only way in: grant `roles/run.invoker` to the trigger's service account only.
```hcl
variable "project_id" {
type = string
description = "Project to deploy into."
}
variable "region" {
type = string
description = "Region for regional resources."
}
variable "service_name" {
type = string
description = "Name of the Cloud Run service that receives the events."
}
variable "topic_id" {
type = string
description = "Id of the Pub/Sub topic whose messages are delivered."
}
variable "bucket_name" {
type = string
description = "Bucket whose new objects are delivered. It must be in var.region."
}
data "google_storage_project_service_account" "this" {
project = var.project_id
}
resource "google_service_account" "trigger" {
project = var.project_id
account_id = "eventarc-trigger"
display_name = "Eventarc trigger"
}
resource "google_project_iam_member" "trigger_receives_events" {
project = var.project_id
role = "roles/eventarc.eventReceiver"
member = "serviceAccount:${google_service_account.trigger.email}"
}
resource "google_cloud_run_v2_service_iam_member" "trigger_invokes_service" {
project = var.project_id
location = var.region
name = var.service_name
role = "roles/run.invoker"
member = "serviceAccount:${google_service_account.trigger.email}"
}
resource "google_project_iam_member" "storage_publishes_events" {
project = var.project_id
role = "roles/pubsub.publisher"
member = "serviceAccount:${data.google_storage_project_service_account.this.email_address}"
}
resource "google_eventarc_trigger" "on_message" {
project = var.project_id
name = "on-message"
location = var.region
service_account = google_service_account.trigger.email
matching_criteria {
attribute = "type"
value = "google.cloud.pubsub.topic.v1.messagePublished"
}
transport {
pubsub {
topic = var.topic_id
}
}
destination {
cloud_run_service {
service = var.service_name
region = var.region
path = "/events/message"
}
}
depends_on = [
google_project_iam_member.trigger_receives_events,
google_cloud_run_v2_service_iam_member.trigger_invokes_service,
]
}
resource "google_eventarc_trigger" "on_object" {
project = var.project_id
name = "on-object"
location = var.region
service_account = google_service_account.trigger.email
matching_criteria {
attribute = "type"
value = "google.cloud.storage.object.v1.finalized"
}
matching_criteria {
attribute = "bucket"
value = var.bucket_name
}
destination {
cloud_run_service {
service = var.service_name
region = var.region
path = "/events/object"
}
}
depends_on = [
google_project_iam_member.trigger_receives_events,
google_project_iam_member.storage_publishes_events,
google_cloud_run_v2_service_iam_member.trigger_invokes_service,
]
}
```

View File

@@ -0,0 +1,74 @@
# 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.
```hcl
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}/\")"
}
}
```

View File

@@ -0,0 +1,109 @@
# Google Kubernetes Engine
A private Autopilot cluster on an existing subnet, with its node service account and Workload Identity for one workload.
- `enable_autopilot = true` makes Google manage the nodes. Node pools, machine types and node counts are then not yours to set: leave out `node_pool`, `node_config` and `initial_node_count`.
- A regional `location` gives a control plane in three zones. A zone gives one.
- The subnet needs two secondary ranges, for Pods and Services, named in `ip_allocation_policy`.
- `enable_private_nodes = true` gives nodes no public address; they then need Cloud NAT to pull images from outside Google.
- `master_authorized_networks_config` limits who can reach the control plane. With `enable_private_endpoint = true` it has no public address at all.
- Give nodes a dedicated service account with `roles/container.defaultNodeServiceAccount`, not the Compute Engine default account.
- Autopilot has Workload Identity on. A Kubernetes service account acts as a Google service account through `roles/iam.workloadIdentityUser`, with the member `serviceAccount:PROJECT.svc.id.goog[NAMESPACE/KSA]`.
- `deletion_protection` is true unless set; take it from a variable.
```hcl
variable "project_id" {
type = string
description = "Project to deploy into."
}
variable "region" {
type = string
description = "Region for regional resources."
}
variable "network_id" {
type = string
description = "Id of the VPC the cluster is in."
}
variable "subnetwork_id" {
type = string
description = "Id of the subnet for nodes. It has secondary ranges named pods and services."
}
variable "admin_cidr" {
type = string
description = "Range allowed to reach the control plane."
}
variable "deletion_protection" {
type = bool
description = "Whether the cluster is protected from deletion."
default = true
}
resource "google_service_account" "nodes" {
project = var.project_id
account_id = "gke-nodes"
display_name = "GKE nodes"
}
resource "google_project_iam_member" "nodes_default_role" {
project = var.project_id
role = "roles/container.defaultNodeServiceAccount"
member = "serviceAccount:${google_service_account.nodes.email}"
}
resource "google_container_cluster" "workload" {
project = var.project_id
name = "workload"
location = var.region
enable_autopilot = true
deletion_protection = var.deletion_protection
network = var.network_id
subnetwork = var.subnetwork_id
ip_allocation_policy {
cluster_secondary_range_name = "pods"
services_secondary_range_name = "services"
}
private_cluster_config {
enable_private_nodes = true
enable_private_endpoint = false
}
master_authorized_networks_config {
cidr_blocks {
cidr_block = var.admin_cidr
display_name = "administrators"
}
}
cluster_autoscaling {
auto_provisioning_defaults {
service_account = google_service_account.nodes.email
}
}
release_channel {
channel = "REGULAR"
}
depends_on = [google_project_iam_member.nodes_default_role]
}
resource "google_service_account" "app" {
project = var.project_id
account_id = "orders-app"
display_name = "Orders application"
}
resource "google_service_account_iam_member" "app_workload_identity" {
service_account_id = google_service_account.app.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[orders/orders-app]"
}
```