This project provides a complete, production-ready infrastructure setup on Google Cloud Platform (GCP) for a modern web application. The entire infrastructure is defined and managed using HashiCorp Terraform, enabling automation, version control, and easy replication across different environments.
This project is configured with a complete CI/CD pipeline using GitHub Actions to automate infrastructure management.
- Plan on Pull Request: When a pull request is opened, the workflow automatically runs
terraform planand posts the output as a comment on the PR for review. - Apply on Merge: When a pull request is merged to the
mainbranch, the workflow automatically runsterraform applyto deploy the changes to thedevelopmentenvironment. - Authentication: The pipeline uses a secure, passwordless OIDC connection between GitHub Actions and GCP via Workload Identity Federation.
Terraform state is stored securely in a GCS remote backend, which enables team collaboration and state locking.
We use Terraform workspaces to manage separate environments (e.g., development, staging). Each workspace gets its own isolated set of resources.
These steps are for the very first person setting up this project.
- Configure Workload Identity Federation: Follow the internal documentation to set up the GCS backend bucket, the
github-actions-deployerservice account, and the Workload Identity Federation trust between GCP and the GitHub repository. - Create GitHub Secrets: In the GitHub repository settings (
Settings > Secrets and variables > Actions), create the following repository secrets:TF_VAR_billing_account_id: Your GCP Billing Account ID.TF_VAR_db_password: The desired password for the Cloud SQL database.
Here is the step-by-step guide for any new developer to get started with local development.
- Get the Code:
git clone <your-repository-url> - Authenticate with GCP:
gcloud auth application-default login - Configure Local Variables:
cp terraform/terraform.tfvars.example terraform/terraform.tfvars- Edit
terraform/terraform.tfvarsand fill in the secret values provided by your team lead.
- Initialize Terraform:
cd terraform && terraform init - Select Workspace:
terraform workspace select development
You are now ready to make changes locally.
- Create a new branch:
git checkout -b my-feature - Make your changes to the
.tffiles in theterraform/directory. - Commit and push your branch.
- Open a Pull Request against the
mainbranch. - Review the Plan: The GitHub Actions bot will automatically run
terraform planand add the output as a comment on your PR. Verify that the changes are what you expect. - Merge: Once approved, merge the pull request. The GitHub Actions bot will automatically run
terraform applyto deploy your changes.
- Build and Push the Docker Image:
- Configure Docker for GCP (one-time setup):
gcloud auth configure-docker $(terraform -chdir=terraform output -raw project_region)-docker.pkg.dev - From the project root, run:
./build-and-push.sh
- Configure Docker for GCP (one-time setup):
- Update the Image in Terraform:
- In
terraform/4-app-hosting.tf, update theimageargument in thegoogle_cloud_run_v2_serviceresource with the new image URI from the script output.
- In
- Commit and push this change through the PR workflow described above.
- GCP Project: A new, isolated project for your environment.
- VPC Network: A private network for your resources.
- Cloud SQL for PostgreSQL: A managed, private database instance.
- Cloud Run Service: A serverless, scalable environment to run your application container.
- Artifact Registry: A private Docker repository to store your application images.
- And all necessary IAM bindings and APIs.