This directory contains the Terraform code for deploying the Rhesis application infrastructure on Google Cloud Platform.
The infrastructure code has been optimized to follow Terraform best practices:
-
DRY (Don't Repeat Yourself) - Common configurations are extracted to:
common/defaults.tfvars: Environment-specific defaults for servicescommon/providers.tf: Common provider configurationsmodules/service: A unified service deployment module
-
Parameterization - Environment-specific configurations are parameterized:
- Database settings (machine type, high availability, disk size)
- Service settings (CPU, memory, instance counts)
- IAM roles
-
Modularity - The code is organized into reusable modules:
environment: Orchestrates the deployment of a complete environmentservice: Standardizes service deployment (Cloud Run + IAM)- Other resource-specific modules (cloud-sql, network, storage, etc.)
-
Consistency - Common patterns are used across all resources:
- Consistent naming conventions
- Standardized labels
- Unified approach to environment variables
Cloud Run services by default don't have fixed IP addresses. To provide fixed IPs and proper SSL certificates, this infrastructure includes load balancers for each service:
- Static IP - Each service can have a reserved static IP address
- SSL Certificates - Managed SSL certificates are automatically provisioned
- Domain Mapping - Custom domains are mapped to each service
- Environment-specific - Different domains for dev/stg/prd environments
To enable a load balancer for a service, set the corresponding domain variable:
# For example, in terraform.tfvars
backend_domain = "api.example.com"
frontend_domain = "app.example.com"If a domain is not specified (empty string), no load balancer will be created for that service.
common/: Common configurations and defaultsenvironments/: Environment-specific configurationsdev/: Development environmentstg/: Staging environmentprd/: Production environment
modules/: Reusable Terraform modulesenvironment/: Environment orchestration moduleservice/: Service deployment modulecontainer-registry/: Container registry module- Other resource-specific modules
scripts/: Deployment and utility scriptsdeploy-terraform.sh: Script for deploying to different environmentsbootstrap-terraform-deployer.sh: Script for setting up a Terraform deployer service account
-
Create a bootstrap admin project in GCP for Terraform state management:
# Create a new project in GCP called "rhesis-platform-admin" (or your preferred name) # Enable billing for the project -
Set up your GCP service account and Terraform state bucket:
# Update the bootstrap script with your project and billing account IDs cd scripts ./bootstrap-terraform-deployer.shThis script will:
- Create a service account with necessary permissions
- Enable required APIs
- Create a GCS bucket for Terraform state storage
- Configure bucket versioning and permissions
- Generate a service account key file
-
Deploy to an environment using the deployment script:
cd scripts ./deploy-terraform.sh --environment dev --key /path/to/service-account-key.json -
For more deployment options:
./deploy-terraform.sh --help
This repository includes a GitHub Actions workflow for automated deployments. See scripts/DEPLOYMENT.md for detailed instructions.
The deployment process uses a standardized approach to map GitHub secrets to Terraform variables:
- GitHub secrets follow the naming convention
TF_VAR_VARIABLE_NAME(uppercase) - Terraform variables use snake_case in the .tf files
- The deployment script automatically converts between these formats
- Variables are organized by environment using GitHub Environments feature
- For detailed information about the variable mapping process, see
scripts/DEPLOYMENT.md
-
Navigate to the desired environment directory:
cd environments/dev -
Initialize Terraform:
terraform init -
Apply the common defaults:
terraform apply -var-file=../../common/defaults.tfvars -
Create a
terraform.tfvarsfile based on the example:cp terraform.tfvars.example terraform.tfvars -
Edit the
terraform.tfvarsfile with your specific values. -
Apply the Terraform configuration:
terraform apply
modules/: Reusable Terraform modulesenvironment/: Main environment module that creates all resourcesgcp-project/: GCP project creation and API enablementcloud-run/: Cloud Run servicescloud-sql/: Cloud SQL databasesstorage/: GCS bucketsnetwork/: VPC networks, subnets, and related resourcesiam/: Service accounts and IAM permissionspubsub/: Pub/Sub topics and subscriptionscontainer-registry/: Artifact Registry repositories for container imagesload-balancer/: Load balancer with SSL certificates for Cloud Run services
environments/: Environment-specific configurationsdev/: Development environmentstg/: Staging environmentprd/: Production environment
common/: Shared resources and configurations
Resources follow the naming convention: {service}-{env}[-{region}]
service: backend, frontend, worker, polyphemus, dbenv: dev, prd, stgregion: included for region-specific resources
The default region is europe-west4.
Each environment (dev, stg, prd) has its own:
- GCP project
- Network infrastructure
- Service accounts
- Cloud SQL database
- Storage buckets
- Pub/Sub topics
- Cloud Run services