devCoPs - FEUP-MEIC-DS-2025-26/madeinportugal.store GitHub Wiki

Microservice Deployment Practice: Terraform and Google Cloud Run

This document details the standard operating procedure for deploying our microservice Docker images to Google Cloud Run using Infrastructure as Code (IaC) managed by Terraform. This process ensures our deployments are consistent, versioned, and repeatable across environments.


1. Prerequisites and Setup

Before proceeding with the deployment, ensure the following tools are installed and configured:

Tool Purpose Setup Command
Google Cloud CLI (gcloud) Authentication and access to Google Cloud Platform. gcloud auth login
Terraform IaC tool for provisioning Cloud Run infrastructure. (Install hints available in Google Cloud and HashiCorp docs)
Docker Building and pushing the service container image. docker login

The Google Cloud CLI and Terraform must be installed and authenticated/initialized for your project.


2. Microservice Containerization and Registry Push

The microservice must be packaged as a Docker image and available in a public or private container registry (Docker Hub).

2.1 Docker Image Build and Tagging

Using your service's Dockerfile (e.g., a simple Python app structure):

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Build and tag the image using a meaningful convention:

docker build -t <your-username>/<repo>:<tag> .

2.2 Push to Container Registry

Push the locally built image to your chosen registry:

docker push <your-username>/<repo>:<tag>

3. Terraform Configuration and Deployment

The deployment configuration is located in the hello_world/ directory. It uses the hashicorp/google provider (version ~> 5.0) to deploy a Cloud Run v2 Service.

3.1 Variable Configuration

The deployment requires several variables, defined in hello_world/variables.tf, which can be set in hello_world/terraform.tfvars:

Variable Name Description Type Default Value Notes
project_id The Google Cloud project ID. string None This variable has no default and must be provided.
docker_image Container image to deploy to Cloud Run (including tag). string us-docker.pkg.dev/cloudrun/container/hello Update this to the image pushed in Section 2.2.
region The region where resources will be deployed. string europe-west1
service_name The name for the Cloud Run service. string hello-world-service

Example terraform.tfvars file:

# hello_world/terraform.tfvars
project_id   = "your-gcp-project-id"
docker_image = "your-username/your-repo:tag"

3.2 Deployment Execution

Execute the following commands from the repository root:

  1. Initialize Terraform:

    terraform -chdir=hello_world init
  2. Apply Configuration:

    terraform -chdir=hello_world apply

4. Service Details and Access

4.1 Deployed Resources

The Terraform configuration performs the following actions:

  • Cloud Run Service: Provisions a google_cloud_run_v2_service using the specified docker_image. The service name and location are determined by var.service_name and var.region, respectively.
  • Public Access: Creates a google_cloud_run_v2_service_iam_binding to grant public access to the service by setting the role to roles/run.invoker and the members to ["allUsers"].

4.2 Output

Upon successful deployment, the full URL of the deployed Cloud Run service is provided via an output variable:

output "service_url" {
  description = "The URL of the deployed Cloud Run service."
  value       = google_cloud_run_v2_service.default.uri
}

Resources

The repository regarding our Communities of Practice can be found here

⚠️ **GitHub.com Fallback** ⚠️