devCoPs - FEUP-MEIC-DS-2025-26/madeinportugal.store GitHub Wiki
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.
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.
The microservice must be packaged as a Docker image and available in a public or private container registry (Docker Hub).
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> .Push the locally built image to your chosen registry:
docker push <your-username>/<repo>:<tag>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.
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"Execute the following commands from the repository root:
-
Initialize Terraform:
terraform -chdir=hello_world init
-
Apply Configuration:
terraform -chdir=hello_world apply
The Terraform configuration performs the following actions:
-
Cloud Run Service: Provisions a
google_cloud_run_v2_serviceusing the specifieddocker_image. The service name and location are determined byvar.service_nameandvar.region, respectively. -
Public Access: Creates a
google_cloud_run_v2_service_iam_bindingto grant public access to the service by setting theroletoroles/run.invokerand themembersto["allUsers"].
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
}The repository regarding our Communities of Practice can be found here