Google Cloud Run with Terraform: Benefits, Use Cases, and the Role of Knative


An Introduction to Google Cloud Run with Terraform: Benefits, Use Cases, and the Role of Knative

Google Cloud Run is a fully managed compute platform by Google that enables you to run stateless containers in a serverless environment. Cloud Run removes the need for infrastructure management, thereby making development and deployment a seamless process. While Cloud Run is undoubtedly an excellent tool, you can unlock even more potential by deploying Cloud Run with Terraform, a powerful infrastructure as code (IaC) tool. This article explores the benefits of using Cloud Run with Terraform and also sheds light on Knative, the technology that powers Cloud Run.

What is Google Cloud Run?

Google Cloud Run is a cloud service that allows developers to run their applications in a fully managed environment without worrying about the underlying infrastructure. It’s designed to run stateless containers, which are containers that don’t maintain persistent state between sessions. The primary advantage of Cloud Run is that it abstracts away all the complexities of infrastructure management, allowing developers to focus on writing code.

Cloud Run is built on an open-source platform known as Knative.

Knative: The Foundation of Cloud Run

Knative is an open-source Kubernetes-based platform for deploying and managing modern serverless workloads. It provides foundational middleware primitives that enable developers to build, deploy, and manage applications on Kubernetes, abstracting away many of the complexities.

Google Cloud Run is essentially a fully managed version of Knative. This means that all the services you deploy on Cloud Run are Knative services. The use of Knative also ensures that your services are portable. You can run your services not only on Google Cloud but also on any platform that supports Knative.

Why Use Terraform with Google Cloud Run?

Terraform is an open-source infrastructure as code (IaC) software tool that provides a consistent CLI workflow for managing cloud service. It allows you to define and provide data center infrastructure using a declarative configuration language.

  1. Automated Infrastructure Management: With Terraform, you can automate the creation and management of your Cloud Run services. This means you can quickly replicate your infrastructure, manage resources efficiently, and reduce human error.
  2. Multi-cloud Deployment: Terraform supports a multitude of providers, not just Google Cloud. This means you can manage a multi-cloud deployment with the same tool and methodology, enhancing your deployment strategy’s consistency and portability.
  3. Version Control for Infrastructure: Using Terraform allows you to use version control systems like Git to version your infrastructure. This means you can track changes, maintain a historical record, and quickly rollback if necessary.
  4. Collaboration and Reusability: Terraform’s modular approach encourages the reuse of modules across the team or organization. This not only reduces duplication of work but also promotes collaboration among team members.

Deploying Cloud Run Services with Terraform

Deploying a Cloud Run service with Terraform involves defining the service and its related resources in a Terraform configuration file, and then executing Terraform commands to create those resources.

Here’s an example of what a Terraform configuration for a Cloud Run service might look like:


resource "random_id" "unique" {
  keepers = {
    name = var.name
  }
  byte_length = 8
}

resource "google_cloud_run_service" "default" {
  name        = "${var.name}-${random_id.unique.hex}"
  location = var.region

  template {
    spec {
      containers {
        image = var.container_image
        ports {
          container_port = 3000
        }
      }
    }
  }
}

data "google_iam_policy" "noauth" {
  binding {
    role = "roles/run.invoker"
    members = [
      "allUsers",
    ]
  }
}

resource "google_cloud_run_service_iam_policy" "noauth" {
  count = var.public_access == true ? 1 : 0
  location    = google_cloud_run_service.default.location
  project     = google_cloud_run_service.default.project
  service     = google_cloud_run_service.default.name
  policy_data = data.google_iam_policy.noauth.policy_data
}

Variables

variable "project_id" {
  type = string
}
variable "region" {
  type = string
}
variable "zone" {
  type = string
}
variable "tags" {
  type = map(string)
}

variable "name" {
  description = "Name of topic"
  type        = string
}

variable "container_image" {
  type = string
}
variable "public_access" {
  type = bool
  default = false
}

Usage

module "cloudrunexample" {
    source      = "../../"
    project_id = var.project_id
    region     = var.region
    zone       = var.zone
    tags       = var.tags
    name = var.name
    container_image = "gcr.io/<PROJECT_ID>/<CLOUDRUN_SRV_NAME>:latest"
    public_access = true
}

Terraform.tfvars

project_id = "<PROJECT_ID>"
region     = "us-west1"
zone       = "us-west1-b"
tags       = {
                product = "demo"
                env = "dev"
                managed_by = "terraform"
            }
name = "cloudrundemo"

This configuration tells Terraform to create a Cloud Run service named “cloudrundemo” in the “us-west1” region. The service runs the sample image from Google’s Cloud Run container repository.

Conclusion

In conclusion, using Google Cloud Run with Terraform brings together a lot of value. And its great for running stateless workloads quickly. It reminds me of AWS Fargate ECS but a bit better.

Zak's AI.Assist

Session only - not saved