A Terraform module to deploy a Cloud Run Service using a Kubernetes-like syntax.
This module provides a Kubernetes-like interface for deploying Cloud Run services, making it easier for teams familiar with Kubernetes to adopt Cloud Run.
If you're coming from Kubernetes and want to deploy to Cloud Run, this module bridges the gap by providing:
- Familiar syntax - Use the same
metadata
,spec
, andtemplate
structures you know from Kubernetes - Smooth migration path - Existing Kubernetes YAML manifests can be adapted with minimal changes
- Reduced learning curve - No need to learn Cloud Run-specific Terraform syntax from scratch
- Manifest-driven deployments - Keep your configuration in version-controlled YAML files
- Environment flexibility - Use the same manifest across environments with parameter overrides
- Team consistency - Standardize deployments using familiar Kubernetes patterns
Instead of learning Cloud Run's native Terraform syntax, you can leverage your existing Kubernetes knowledge while still getting all the benefits of Cloud Run's serverless platform.
- Kubernetes-like syntax - Use familiar
metadata
,spec
, andtemplate
structures - Manifest file support - Load configuration from YAML files
- Parameter overrides - Direct module parameters override manifest values
- Multiple containers - Support for sidecar containers (similar to Kubernetes pods)
- Volume management - Support for secrets, Cloud SQL instances, and empty directories
- Health checks - Startup and liveness probes with HTTP, TCP, and gRPC support
- Resource management - CPU and memory limits with Cloud Run-specific optimizations
- Environment variables - Support for both static values and secret references
- Traffic management - Blue-green and canary deployments
- Auto-scaling - Configure min/max instances (equivalent to replicas)
Create a deployment.yaml
file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-app
location: us-central1
project: my-gcp-project
spec:
replicas: 2
template:
metadata:
labels:
app: hello-app
spec:
containers:
- name: hello-app
image: gcr.io/google-samples/hello-app:2.0
ports:
- containerPort: 8080
resources:
limits:
cpu: 1000m
memory: 512Mi
Then use it in Terraform:
module "hello_app" {
source = "github.com/cstanislawski/terraform-gcp-cloud-run-deploy"
manifest_path = "./deployment.yaml"
}
module "hello_app_staging" {
source = "github.com/cstanislawski/terraform-gcp-cloud-run-deploy"
manifest_path = "./deployment.yaml"
# Override for staging environment
metadata = {
name = "hello-app-staging"
location = "us-west1"
}
template = {
spec = {
containers = [
{
name = "hello-app"
env = [
{
name = "ENV"
value = "staging"
}
]
}
]
}
}
}
module "hello_app" {
source = "github.com/cstanislawski/terraform-gcp-cloud-run-deploy"
# Kubernetes-like metadata
metadata = {
name = "hello-app"
location = "us-central1"
project = "my-gcp-project"
}
# Kubernetes-like spec (replicas equivalent)
spec = {
replicas = 2
}
# Kubernetes-like template
template = {
metadata = {
labels = {
app = "hello-app"
}
}
spec = {
containers = [
{
name = "hello-app"
image = "gcr.io/google-samples/hello-app:2.0"
ports = [
{
containerPort = 8080
}
]
resources = {
limits = {
cpu = "1000m"
memory = "512Mi"
}
}
}
]
}
}
}
See the examples/
directory for complete working examples:
examples/1-simple-deployment/
- Basic single-container deployment using manifestexamples/2-complex-deployment/
- Multi-container deployment with manifest and overrides
Name | Version |
---|---|
terraform | >= 1.3 |
>= 4.84.0 |
Name | Version |
---|---|
>= 4.84.0 |
No modules.
Name | Type |
---|---|
google_cloud_run_v2_service.cloudrun_service | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
binaryAuthorization | Binary authorization configuration for the Cloud Run service | object({ |
null |
no |
client | Arbitrary identifier for the API client | string |
null |
no |
client_version | Arbitrary version identifier for the API client | string |
null |
no |
deletion_protection | Whether Terraform will be prevented from destroying the service. Defaults to true. | bool |
true |
no |
description | User-provided description of the Cloud Run service | string |
null |
no |
ingress | Ingress configuration for the Cloud Run service | string |
null |
no |
invoker_iam_disabled | Whether to disable IAM for the invoker. Defaults to false. | bool |
false |
no |
launch_stage | The launch stage as defined by Google Cloud Platform Launch Stages | string |
null |
no |
manifest_path | Path to a Kubernetes-like YAML manifest file. Direct module parameters will override manifest values. | string |
null |
no |
metadata | Metadata for the Cloud Run service (similar to Kubernetes metadata) | object({ |
null |
no |
spec | Specification for the Cloud Run service (similar to Kubernetes spec) | object({ |
null |
no |
template | Template specification for the Cloud Run service (similar to Kubernetes template) | object({ |
null |
no |
traffic | Traffic allocation for the Cloud Run service | list(object({ |
null |
no |
Name | Description |
---|---|
conditions | The conditions of the Cloud Run service |
latest_created_revision | The latest created revision of the Cloud Run service |
latest_ready_revision | The latest ready revision of the Cloud Run service |
location | The location where the Cloud Run service is deployed |
observed_generation | The observed generation of the Cloud Run service |
project | The project where the Cloud Run service is deployed |
service_id | The unique identifier of the Cloud Run service |
service_name | The name of the Cloud Run service |
service_uri | The URL of the Cloud Run service |
terminal_condition | The terminal condition of the Cloud Run service |
traffic | The traffic allocation for the Cloud Run service |
Apache 2.0 - see LICENSE file for details.