Labs: Deploy Web Infrastructure on Google Cloud Using Terraform (Cloud Run)
This is a continuation of Part 1. Here we focus on the “fun part”: breaking down Terraform code and actually deploying an application to Cloud Run so it’s publicly accessible.
Quick Context (DevFest Mode)
Section titled “Quick Context (DevFest Mode)”In the talk “From Code to Automated Web Infrastructure on Google Cloud with Terraform”, my goal was simple:
- start from Terraform code
- end with a real, live web service on Google Cloud
Infrastructure deployed in this Part 2:
- Cloud Run (fully managed)
- Public service (no auth) via IAM binding roles/run.invoker to allUsers
Perfect for stage demos: fast, repeatable, and no VM headaches.
Directory Structure
Section titled “Directory Structure”DirectoryTerraform-Devfest-Dev-Demo/
- service-account-gcp-your.json
- providers.tf
- variables.tf
- main.tf
📝 Note:
- The
*.jsonfile is a service account key (credential). For public repositories, never commit this file.
Prerequisites
Section titled “Prerequisites”- Terraform installed (Part 1)
- A GCP project with billing enabled
- A service account with minimum required permissions for Cloud Run
- Required APIs enabled
Required APIs
Section titled “Required APIs”run.googleapis.comiam.googleapis.comcloudresourcemanager.googleapis.comgcloud services enable run.googleapis.com iam.googleapis.com cloudresourcemanager.googleapis.comBreaking Down the Terraform Files
Section titled “Breaking Down the Terraform Files”providers.tf — Provider & Authentication
Section titled “providers.tf — Provider & Authentication”terraform { required_providers { google = { source = "hashicorp/google" version = "7.12.0" } }}
provider "google" { credentials = file(var.gcp_svc_key) project = var.gcp_project region = var.gcp_region}variables.tf — Demo-Friendly Defaults
Section titled “variables.tf — Demo-Friendly Defaults”variable "gcp_svc_key" { default = "service-account-google-your.json"}
variable "gcp_project" { default = "your-gcp-project-id"}
variable "gcp_region" { default = "asia-southeast2"}
variable "cloud_run_service_name" { default = "devfest-demo"}
variable "container_image" { default = "docker.io/fadharpra/devfest-demo:1.6"}main.tf — Cloud Run Service
Section titled “main.tf — Cloud Run Service”resource "google_cloud_run_v2_service" "app" { name = var.cloud_run_service_name location = var.gcp_region ingress = "INGRESS_TRAFFIC_ALL"
scaling { max_instance_count = 3 }
template { containers { image = var.container_image } }}Deploy Steps
Section titled “Deploy Steps”terraform initterraform planterraform applyOutput URL
Section titled “Output URL”output "cloud_run_url" { value = google_cloud_run_v2_service.app.uri}Conclusion
Section titled “Conclusion”Simple Terraform structure, fast Cloud Run deployment, and instant demo results.