This commit is contained in:
144
skills/google-cloud-iac-patterns/references/cloud-run-service.md
Normal file
144
skills/google-cloud-iac-patterns/references/cloud-run-service.md
Normal file
@@ -0,0 +1,144 @@
|
||||
# Cloud Run service
|
||||
|
||||
`google_cloud_run_v2_service`: a long-running service that answers requests.
|
||||
|
||||
Where the settings sit, which is what goes wrong most often:
|
||||
|
||||
- `ingress`, `deletion_protection` and `traffic` are on the resource.
|
||||
- `service_account`, `scaling`, `timeout`, `max_instance_request_concurrency`, `execution_environment`, `vpc_access` and `encryption_key` are in `template`.
|
||||
- `image`, `env`, `ports`, `resources` and the probes are in `template.containers`.
|
||||
- `cpu_idle` and `startup_cpu_boost` are in `template.containers.resources`, beside `limits`. They exist on a service only.
|
||||
- `min_instance_count` and `max_instance_count` are in `template.scaling`.
|
||||
- A secret is read through `env.value_source.secret_key_ref`; the service account needs `roles/secretmanager.secretAccessor` on that secret.
|
||||
- Direct VPC egress is `template.vpc_access.network_interfaces`. A connector is `template.vpc_access.connector`. Use one of them.
|
||||
- Public access is a grant of `roles/run.invoker` to `allUsers` together with `ingress = "INGRESS_TRAFFIC_ALL"`. A private service has neither.
|
||||
|
||||
```hcl
|
||||
variable "project_id" {
|
||||
type = string
|
||||
description = "Project to deploy into."
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "Region for regional resources."
|
||||
}
|
||||
|
||||
variable "image" {
|
||||
type = string
|
||||
description = "Container image reference, including the tag or digest."
|
||||
}
|
||||
|
||||
variable "deletion_protection" {
|
||||
type = bool
|
||||
description = "Whether Terraform is prevented from deleting the service."
|
||||
default = true
|
||||
}
|
||||
|
||||
resource "google_service_account" "ingest" {
|
||||
project = var.project_id
|
||||
account_id = "ingest-service"
|
||||
display_name = "Ingest service"
|
||||
}
|
||||
|
||||
resource "google_secret_manager_secret" "signing_key" {
|
||||
project = var.project_id
|
||||
secret_id = "webhook-signing-key"
|
||||
|
||||
replication {
|
||||
auto {}
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_secret_manager_secret_iam_member" "ingest_reads_signing_key" {
|
||||
project = var.project_id
|
||||
secret_id = google_secret_manager_secret.signing_key.secret_id
|
||||
role = "roles/secretmanager.secretAccessor"
|
||||
member = google_service_account.ingest.member
|
||||
}
|
||||
|
||||
resource "google_cloud_run_v2_service" "ingest" {
|
||||
project = var.project_id
|
||||
name = "ingest"
|
||||
location = var.region
|
||||
ingress = "INGRESS_TRAFFIC_ALL"
|
||||
deletion_protection = var.deletion_protection
|
||||
|
||||
template {
|
||||
service_account = google_service_account.ingest.email
|
||||
timeout = "30s"
|
||||
max_instance_request_concurrency = 80
|
||||
execution_environment = "EXECUTION_ENVIRONMENT_GEN2"
|
||||
|
||||
scaling {
|
||||
min_instance_count = 1
|
||||
max_instance_count = 10
|
||||
}
|
||||
|
||||
containers {
|
||||
image = var.image
|
||||
|
||||
ports {
|
||||
container_port = 8080
|
||||
}
|
||||
|
||||
resources {
|
||||
limits = {
|
||||
cpu = "1"
|
||||
memory = "512Mi"
|
||||
}
|
||||
cpu_idle = true
|
||||
startup_cpu_boost = true
|
||||
}
|
||||
|
||||
env {
|
||||
name = "LOG_LEVEL"
|
||||
value = "info"
|
||||
}
|
||||
|
||||
env {
|
||||
name = "SIGNING_KEY"
|
||||
value_source {
|
||||
secret_key_ref {
|
||||
secret = google_secret_manager_secret.signing_key.secret_id
|
||||
version = "latest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
startup_probe {
|
||||
initial_delay_seconds = 0
|
||||
period_seconds = 5
|
||||
failure_threshold = 6
|
||||
http_get {
|
||||
path = "/healthz"
|
||||
}
|
||||
}
|
||||
|
||||
liveness_probe {
|
||||
period_seconds = 30
|
||||
http_get {
|
||||
path = "/healthz"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
traffic {
|
||||
type = "TRAFFIC_TARGET_ALLOCATION_TYPE_LATEST"
|
||||
percent = 100
|
||||
}
|
||||
|
||||
depends_on = [google_secret_manager_secret_iam_member.ingest_reads_signing_key]
|
||||
}
|
||||
|
||||
# Public access. Leave this out, and set ingress to INGRESS_TRAFFIC_INTERNAL_ONLY,
|
||||
# for a service only other workloads call.
|
||||
resource "google_cloud_run_v2_service_iam_member" "public" {
|
||||
project = var.project_id
|
||||
location = google_cloud_run_v2_service.ingest.location
|
||||
name = google_cloud_run_v2_service.ingest.name
|
||||
role = "roles/run.invoker"
|
||||
member = "allUsers"
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user