Terraform

Deploy to Google Cloud Run via Terraform

Hello. In this tutorial, we will deploy a cloud run using terraform script on the google cloud platform.

1. Introduction

Terraform is an open-source tool developed by HashiCorp for building, changing, and versioning the infrastructure safely and efficiently. It is used to manage the infrastructure of the popular cloud service providers and custom in-house solutions. It helps manage both low-level (Compute, Storage, Networking, etc.) and high-level components (such as SaaS, DNS, etc.) Terraform deployment automation is divided into different sections i.e. –

  • IaaC – IaaC is popularly known as the Infrastructure as a Code, describing the infrastructure using a high-level configuration syntax. This allows a blueprint of the infrastructure which can be deployed, versioned, and shared for re-use
  • Execution Plans – Terraform has a planning step where it generates an execution plan. The execution plan tells the administrator what Terraform will do once applied and helps to avoid any surprises when it creates the infrastructure
  • Resource Graph – Terraform builds a graph of all the resources and parallelizes the creation and modification of non-dependent resources. This offers insights into learning the dependencies in their infrastructure
  • Change Automation – Terraform allows to apply of complex changesets to the infrastructure with minimal human intervention

1.1 Steps

To create the infrastructure via the Terraform scripts following commands need to be executed in a sequence. However, details and actions may differ between workflows.

  • terraform init – Initializing the new or existing terraform configuration
  • terraform plan – Generate the execution plan from the resources specified in the file
  • terraform apply – Create the infrastructure from the resources specified in the file
  • terraform destroy – Destroy the created infrastructure
Fig. 1: Terraform workflow

2. Deploy to Google cloud run via Terraform

Google Cloud is a cloud computing platform offered by Google. It provides various services like compute engine, storage, networking, big data, and many others that run on google infrastructure. Google cloud run is a serverless platform offered by Google cloud to deploy and run Docker containers. It is fully managed and auto-scalable. It can be easily integrated with other cloud solutions such as Cloud pub/sub, Cloud scheduler, etc.

2.1 Pre-requisites

To start with you will need –

  • Terraform CLI and the instructions can be read from this link
  • Free tier google cloud account
  • Google Cloud SDK and the instructions can be read from this link

2.2 Creating the terraform script

We will be using a single terraform script named – main.tf to specify the –

  • Terraform provider
  • Google cloud provider and project id details
  • Resource creation and its IAM binding
  • Output tag to display the cloud run service endpoint on console

Please note that we will be using a google sample docker image (named – hello-app) for deploying the cloud run.

main.tf

terraform {
  required_version = ">=0.14"

  required_providers {
    google = ">= 3.3"
  }
}

provider "google" {
  # replace GCP_PROJECT_ID with your project
  project = "GCP_PROJECT_ID"
}

# enable the google cloud service
# note - uncomment the below block if the service is already not enabled in your GCP project
/* resource "google_project_service" "enable_api" {
  service = "run.googleapis.com"
  disable_on_destroy = true
}
 */

# create a cloud-run service
resource "google_cloud_run_service" "deploy_service" {
  name     = "app"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "gcr.io/google-samples/hello-app:1.0"
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }

  # wait for the cloud-run service to be enabled
  # note - uncomment the below block if google_project_service.enable_api is uncommented
  /* depends_on = [
    google_project_service.enable_api
  ] */
}

# allow unauthenticated invocations to the cloud-run service endpoint
resource "google_cloud_run_service_iam_member" "run_all_users" {
  location = google_cloud_run_service.deploy_service.location
  service  = google_cloud_run_service.deploy_service.name

  role   = "roles/run.invoker"
  member = "allUsers"
}

# log cloud run service endpoint on the console
output "service_url" {
  value = google_cloud_run_service.deploy_service.status[0].url
}

3. Code run and demo

Navigate to the project directory containing the above scripts and open the terminal. Execute the below commands in the respective order within the directory.

Commands

-- step1: initializing the new or existing terraform configuration --
terraform init


-- step2: generating the execution plan --
terraform plan


-- step3: building the infrastructure --
-- auto-approve flag skips interactive approval of the plan before applying
terraform apply --auto-approve


-- step4: destroying the infrastructure --
-- auto-approve flag skips interactive approval of the plan before applying
terraform destroy --auto-approve

Once the script is executed successfully head over to the google cloud run to confirm whether the resource is created successfully or not. If everything goes well the resource will be created as shown below.

Fig. 2: Created resource

You can copy the cloud run endpoint from the console and hit it in the browser. The hello app response will be shown below.

Fig. 3: Application response

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

4. Summary

In this tutorial, we learned an introduction to Terraform and deployed a simple cloud-run app via terraform script. You can download the source code from the Downloads section.

5. Download the Project

This was a tutorial to deploy a cloud-run app via terraform script.

Download
You can download the full source code of this example here: Deploy to Google cloud run via Terraform

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button