Zero-Downtime Blue/Green VM Deployments with Managed Instance Groups, Cloud Build & Terraform
a Google Cloud Platform Demo
This repository provides source code for zero-downtime blue/green VM deployments using Cloud Build and Terraform.
It configures multiple regional external HTTP(S) load balancers and deploys a demo application to Compute Engine VMs in managed instance groups (MIGs). It adds Cloud Build triggers to start deployment automatically when configuration file changes in Source Code Repository.
You can watch the video of this demo running and follow the code walkthrough. You can also run this demo in your own project.
Warning
This project is for advanced Google Cloud Platform infrastructure developers and SRE. If you decide to clone and run any code from this repository, you will be billed for the real infrastructure it creates. We rely on unattended automation, which can make it difficult for you to manage your project manually. We use custom VPCs with public IP addresses, which can make your project vulnerable to attacks. If you are a novice GCP user and just want to learn -- watch the video above instead.
Note
Take a pause here if you are a developer in a large enterprise company that already uses Google Cloud Platform. Remember that all your projects are subject to foundational setup of your organization and its landing zones (link). There maybe org policy restrictions (for example: on using regions or external IP addresses) that will break this demo. Reach out to the team that manages GCP in your organization to discuss where and how you can run this demo following their policies.
Anyone can run this demo on their own. Here is how:
- create new Google Cloud Platform project
- enable billing, which is required
- install
gcloud
CLI (link) or use your project Cloud Shell - set SDK default project, by executing
gcloud config set project <YOUR_PROJECT_ID>
- execute setup script
- run it directly from our GitHub repo:
bash <(curl https://raw.githubusercontent.com/psimakov/gcp-mig-simple/main/setup.sh)
- or, fetch, review, and then run it:
curl https://raw.githubusercontent.com/psimakov/gcp-mig-simple/main/setup.sh -o setup.sh bash ./setup.sh
- run it directly from our GitHub repo:
- follow on-screen instructions
- trigger deployment by committing configuration change
mkdir ~/work cd ~/work gcloud source repos clone copy-of-gcp-mig-simple cd ./copy-of-gcp-mig-simple nano infra/main.tfvars git add . git commit -m "Promote green" git push
- visit Cloud Build History page to see progress of execution
- review Cloud Build apply pipeline logs for deployment IP addresses
- at the end, delete all created resources
bash <(curl https://raw.githubusercontent.com/psimakov/gcp-mig-simple/main/teardown.sh)
Good luck!