This template provides a production-ready infrastructure setup for deploying applications on Google Cloud Platform (GCP) using Cloud Run and Cloud SQL (PostgreSQL). The infrastructure is defined using Terraform and supports multiple environments (development, production) with separate state management.
- Fully automated infrastructure deployment using Terraform
- Separate environment configurations (dev/prod) with isolated state management
- Managed PostgreSQL database using Cloud SQL
- Containerized application deployment using Cloud Run
- Artifact Registry for secure container image storage
- Infrastructure as Code (IaC) best practices
graph TB
subgraph "Google Cloud Platform"
AR[Artifact Registry]
subgraph "Environment (Dev/Prod)"
CR[Cloud Run Service]
subgraph "Cloud SQL"
PG[(PostgreSQL Database)]
end
CR -->|Connects to| PG
end
AR -->|Provides Images| CR
end
- Artifact Registry: Stores and manages container images securely within GCP
- Cloud Run: Serverless platform for running containerized applications
- Cloud SQL: Managed PostgreSQL database service
- Multiple Environments: Separate dev and prod environments with identical configuration but different resource allocations
- Google Cloud SDK
- Terraform installed
- GCP account with billing enabled
- Docker installed
# Authenticate with Google Cloud
gcloud auth login --update-adc
This creates a central repository for your Docker images
- Navigate to
infra/environments/artifacts/main.tf
- Update the following required values in the locals block:
billing_account_id = "YOUR_BILLING_ID" # Optional: org_id = "YOUR_ORG_ID"
- Deploy the registry:
cd infra/environments/artifacts terraform init terraform apply
⚠️ Important: Save thedocker_repo_id
output for the next steps
- Navigate to
infra/environments/dev/main.tf
- Update the configuration in the locals block:
billing_account_id = "YOUR_BILLING_ID" docker_repo_id = "FROM_PREVIOUS_STEP"
- Deploy the environment:
cd infra/environments/dev terraform init terraform apply
⏱️ Deployment takes approximately 10 minutes
- Navigate to
infra/environments/prod/main.tf
- Update the configuration in the locals block:
billing_account_id = "YOUR_BILLING_ID" docker_repo_id = "FROM_PREVIOUS_STEP"
- Deploy the environment:
cd infra/environments/prod terraform init terraform apply
⏱️ Deployment takes approximately 10 minutes
To deploy a new image to the development environment:
- Make sure you're in the root directory of the project
- Run the development deployment script:
./scripts/deploy-dev.sh
This script will:
- Build a new Docker image with the
:dev
tag - Push it to your Artifact Registry
- Deploy it to Cloud Run in your development environment
- Output the service URL when complete
To deploy a new image to the production environment:
- Make sure you've tested your changes in development first
- Run the production deployment script:
./scripts/deploy-prod.sh
This script will:
- Pull the current dev image
- Tag it as
:prod
- Push it to your Artifact Registry
- Ask for confirmation before deploying
- Deploy it to Cloud Run in your production environment
- Output the service URL when complete
⚠️ Important: The production deployment script requires manual confirmation before proceeding with the deployment.
If you encounter permission issues:
- Wait a few minutes for permission propagation and APIs to be enabled
- Try running the terraform commands again
- Verify your GCP authentication is current
- Ensure you have the necessary permissions in both projects
- Check that Docker is properly authenticated with GCP:
gcloud auth configure-docker [REGION]-docker.pkg.dev