├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── examples ├── autoscaling │ ├── README.md │ ├── gceme.sh.tpl │ ├── main.tf │ └── test.sh ├── blue-green │ ├── README.md │ ├── gceme.sh.tpl │ ├── main.tf │ ├── terraform.tfplan │ └── test.sh ├── regional │ ├── README.md │ ├── gceme.sh.tpl │ ├── main.tf │ └── test.sh ├── terraform-install.sh └── zonal │ ├── README.md │ ├── gceme.sh.tpl │ ├── main.tf │ └── test.sh ├── main.tf ├── outputs.tf ├── tests ├── get_tf_env.sh ├── pipelines │ ├── tf-mig-regression.yaml │ └── values.yaml ├── set_pipelines.sh └── tasks │ └── example-mig.yaml └── variables.tf /.gitignore: -------------------------------------------------------------------------------- 1 | **/terraform.tfstate* 2 | **/.terraform* 3 | **/backend.tf 4 | **/terraform.tfvars 5 | **/values-*.yaml 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License 9 | Agreement (CLA). 10 | 11 | * If you are an individual writing original source code and you're sure you 12 | own the intellectual property, then you'll need to sign an [individual CLA] 13 | (https://developers.google.com/open-source/cla/individual). 14 | * If you work for a company that wants to allow you to contribute your work, 15 | then you'll need to sign a [corporate CLA] 16 | (https://developers.google.com/open-source/cla/corporate). 17 | 18 | Follow either of the two links above to access the appropriate CLA and 19 | instructions for how to sign and return it. Once we receive it, we'll 20 | be able to accept your pull requests. 21 | 22 | ## Contributing A Patch 23 | 24 | 1. Submit an issue describing your proposed change to the repo in question. 25 | 1. The repo owner will respond to your issue promptly. 26 | 1. If your proposed change is accepted, and you haven't already done so, sign a 27 | Contributor License Agreement (see details above). 28 | 1. Fork the desired repo, develop and test your code changes. 29 | 1. Ensure that your code adheres to the existing style in the sample to which 30 | you are contributing. 31 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 32 | 1. Submit a pull request. 33 | 34 | ## Style 35 | 36 | Format your HCL code with [`hclfmt`](https://github.com/fatih/hclfmt). 37 | 38 | Make sure there are no differences between the `hclfmt` output and your .tf files: 39 | 40 | ``` 41 | diff -B <( cat *.tf ) <( hclfmt *.tf ) 42 | ``` 43 | 44 | Write changes to the files: 45 | 46 | ``` 47 | hclfmt -w *.tf 48 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Managed Instance Group Terraform Module 2 | 3 | ## NOTE: This module is no longer maintained. Please use [this module](https://github.com/terraform-google-modules/terraform-google-vm/tree/master/modules/mig) instead. 4 | 5 | Modular Google Compute Engine managed instance group for Terraform. 6 | 7 | ## Usage 8 | 9 | ```ruby 10 | module "mig1" { 11 | source = "GoogleCloudPlatform/managed-instance-group/google" 12 | version = "1.1.14" 13 | region = "${var.region}" 14 | zone = "${var.zone}" 15 | name = "group1" 16 | size = 2 17 | service_port = 80 18 | service_port_name = "http" 19 | http_health_check = false 20 | target_pools = ["${module.gce-lb-fr.target_pool}"] 21 | target_tags = ["allow-service1"] 22 | ssh_source_ranges = ["0.0.0.0/0"] 23 | } 24 | ``` 25 | 26 | > NOTE: Make sure you are using [version pinning](https://www.terraform.io/docs/modules/usage.html#module-versions) to avoid unexpected changes when the module is updated. 27 | 28 | ## Resources created 29 | 30 | - [`google_compute_instance_template.default`](https://www.terraform.io/docs/providers/google/r/compute_instance_template.html): The instance template assigned to the instance group. 31 | - [`google_compute_instance_group_manager.default`](https://www.terraform.io/docs/providers/google/r/compute_instance_group_manager.html): The instange group manager that uses the instance template and target pools. 32 | - [`google_compute_firewall.default-ssh`](https://www.terraform.io/docs/providers/google/r/compute_firewall.html): Firewall rule to allow ssh access to the instances. 33 | -------------------------------------------------------------------------------- /examples/autoscaling/README.md: -------------------------------------------------------------------------------- 1 | # Managed instance group autoscaling example 2 | 3 | This example creates a managed instance group with autoscaling enabled. 4 | 5 | ## Set up the environment 6 | 7 | Configure your environment to use your default Google Cloud credentials: 8 | 9 | ```bash 10 | gcloud auth application-default login 11 | export GOOGLE_PROJECT=$(gcloud config get-value project) 12 | ``` 13 | 14 | ## Run Terraform 15 | 16 | Initialize and run Terraform to deploy the example: 17 | 18 | ```bash 19 | terraform init 20 | terraform plan 21 | terraform apply 22 | ``` 23 | 24 | Open URL of load balancer in browser after the load balancer is ready: 25 | 26 | ```bash 27 | EXTERNAL_IP=$(terraform output -module gce-lb-fr external_ip) 28 | (until curl -sf -o /dev/null http://${EXTERNAL_IP}; do echo "Waiting for Load Balancer... "; sleep 5 ; done) && open http://${EXTERNAL_IP} 29 | ``` 30 | 31 | ## Testing autoscaling 32 | 33 | ### CPU utilization test 34 | 35 | The CPU autoscaler tries to balance the load across the managed instance group by adding capacity so that the average load is at or below the target utilization. For details, see the [docs on CPU based autoscaling](https://cloud.google.com/compute/docs/autoscaler/scaling-cpu-load-balancing). 36 | 37 | Run the [`lookbusy`](https://github.com/beloglazov/cpu-load-generator.git) command for 2 minutes on one of the instances to trigger CPU based autoscaling: 38 | 39 | ```bash 40 | gcloud compute ssh $(terraform output instance) -- lookbusy -c 50 41 | ``` 42 | 43 | Open the [Cloud Console](https://console.cloud.google.com/compute/instanceGroups/details/us-central1/autoscale-cluster) to seee that 4 more instances were added to the group in response to the increased CPU. 44 | 45 | Press CTRL-C to stop the lookbusy command. After a few minutes, the managed instance group should scale back down. 46 | 47 | ### Metric test 48 | 49 | ### Load balancing utilization test 50 | 51 | ## Cleanup 52 | 53 | Remove all resources created by terraform: 54 | 55 | ```bash 56 | terraform destroy 57 | ``` 58 | -------------------------------------------------------------------------------- /examples/autoscaling/gceme.sh.tpl: -------------------------------------------------------------------------------- 1 | #!/bin/bash -xe 2 | 3 | apt-get update 4 | apt-get install -y apache2 libapache2-mod-php 5 | 6 | # install lookbusy script to test autoscaling 7 | apt-get install -y build-essential git 8 | cd /tmp 9 | git clone https://github.com/beloglazov/cpu-load-generator.git 10 | ./cpu-load-generator/install-lookbusy.sh 11 | cd - 12 | 13 | cat > /var/www/html/index.php <<'EOF' 14 | [ 18 | "method" => "GET", 19 | "header" => "Metadata-Flavor: Google" 20 | ] 21 | ]; 22 | $context = stream_context_create($opts); 23 | $content = file_get_contents("http://metadata/computeMetadata/v1/$value", false, $context); 24 | return $content; 25 | } 26 | ?> 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Frontend Web Server 37 | 38 | 39 |
40 |
41 |
 
42 |
43 | 44 | 45 |
46 |
47 |
Backend that serviced this request
48 |
49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
Name
ID
Hostname
Zone
Machine Type
Project
Internal IP
External IP
86 |
87 |
88 | 89 |
90 |
91 |
Proxy that handled this request
92 |
93 |
94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 |
Address
102 |
103 | 104 |
105 |
106 |
 
107 |
108 |
109 | 110 | EOF 111 | sudo mv /var/www/html/index.html /var/www/html/index.html.old 112 | 113 | [[ -n "${PROXY_PATH}" ]] && mkdir -p /var/www/html/${PROXY_PATH} && cp /var/www/html/index.php /var/www/html/${PROXY_PATH}/index.php 114 | 115 | systemctl enable apache2 116 | systemctl restart apache2 -------------------------------------------------------------------------------- /examples/autoscaling/main.tf: -------------------------------------------------------------------------------- 1 | variable "region" { 2 | default = "us-central1" 3 | } 4 | 5 | provider "google" { 6 | region = "${var.region}" 7 | } 8 | 9 | variable "network_name" { 10 | default = "mig-autoscale-example" 11 | } 12 | 13 | resource "google_compute_network" "default" { 14 | name = "${var.network_name}" 15 | auto_create_subnetworks = "false" 16 | } 17 | 18 | resource "google_compute_subnetwork" "default" { 19 | name = "${var.network_name}" 20 | ip_cidr_range = "10.127.0.0/20" 21 | network = "${google_compute_network.default.self_link}" 22 | region = "${var.region}" 23 | private_ip_google_access = true 24 | } 25 | 26 | data "template_file" "startup-script" { 27 | template = "${file("${format("%s/gceme.sh.tpl", path.module)}")}" 28 | 29 | vars { 30 | PROXY_PATH = "" 31 | } 32 | } 33 | 34 | module "mig1" { 35 | source = "../../" 36 | region = "${var.region}" 37 | zonal = false 38 | name = "${var.network_name}" 39 | wait_for_instances = true 40 | autoscaling = true 41 | http_health_check = true 42 | 43 | autoscaling_cpu = [{ 44 | target = 0.8 45 | }] 46 | 47 | size = 1 48 | min_replicas = 1 49 | max_replicas = 5 50 | cooldown_period = 120 51 | target_tags = ["${var.network_name}"] 52 | service_port = 80 53 | service_port_name = "http" 54 | startup_script = "${data.template_file.startup-script.rendered}" 55 | target_pools = ["${module.gce-lb-fr.target_pool}"] 56 | network = "${google_compute_subnetwork.default.name}" 57 | subnetwork = "${google_compute_subnetwork.default.name}" 58 | } 59 | 60 | module "gce-lb-fr" { 61 | source = "GoogleCloudPlatform/lb/google" 62 | version = "1.0.2" 63 | region = "${var.region}" 64 | name = "${var.network_name}-lb" 65 | service_port = "${module.mig1.service_port}" 66 | target_tags = ["${module.mig1.target_tags}"] 67 | network = "${google_compute_subnetwork.default.name}" 68 | } 69 | 70 | // null resource used to create dependency with the instance group data source to trigger a refresh. 71 | resource "null_resource" "template" { 72 | triggers { 73 | instance_template = "${element(module.mig1.instance_template, 0)}" 74 | } 75 | } 76 | 77 | data "google_compute_region_instance_group" "mig1" { 78 | self_link = "${module.mig1.region_instance_group}" 79 | depends_on = ["null_resource.template"] 80 | } 81 | 82 | output "instance_self_link" { 83 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[0], "instance")}" 84 | } 85 | 86 | output "instance_status" { 87 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[0], "status")}" 88 | } 89 | -------------------------------------------------------------------------------- /examples/autoscaling/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | set -e 5 | 6 | ### Verify instance status ### 7 | 8 | SELF_LINK=$(terraform output "instance_self_link") 9 | STATUS=$(terraform output "instance_status") 10 | if [[ "${SELF_LINK}" != "" && "${STATUS}" == "RUNNING" ]]; then 11 | echo "INFO: Regional instance ${i} created and running." 12 | else 13 | echo "ERROR: Regional instance not found or is not running" 14 | exit 1 15 | fi 16 | -------------------------------------------------------------------------------- /examples/blue-green/README.md: -------------------------------------------------------------------------------- 1 | # Blue-green Deployment with Managed Instance Group Example 2 | 3 | [![button](http://gstatic.com/cloudssh/images/open-btn.png)](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/terraform-google-managed-instance-group&working_dir=examples/blue-green&page=shell&tutorial=README.md) 4 | 5 | This example shows how to perform a blue/green deployment using the managed instance group module. 6 | 7 | ## Change to the example directory 8 | 9 | ``` 10 | [[ `basename $PWD` != blue-green ]] && cd examples/blue-green 11 | ``` 12 | 13 | ## Install Terraform 14 | 15 | 1. Install Terraform if it is not already installed (visit [terraform.io](https://terraform.io) for other distributions): 16 | 17 | ``` 18 | ../terraform-install.sh 19 | ``` 20 | 21 | ## Set up the environment 22 | 23 | 1. Set the project, replace `YOUR_PROJECT` with your project ID: 24 | 25 | ``` 26 | PROJECT=YOUR_PROJECT 27 | ``` 28 | 29 | ``` 30 | gcloud config set project ${PROJECT} 31 | ``` 32 | 33 | 2. Configure the environment for Terraform: 34 | 35 | ``` 36 | [[ $CLOUD_SHELL ]] || gcloud auth application-default login 37 | export GOOGLE_PROJECT=$(gcloud config get-value project) 38 | ``` 39 | 40 | ## Run Terraform 41 | 42 | ``` 43 | terraform init 44 | terraform apply 45 | ``` 46 | 47 | ## Testing 48 | 49 | 1. Wait for the load balancer to be provisioned: 50 | 51 | ``` 52 | ./test.sh 53 | ``` 54 | 55 | 2. Open the URL of the load balancer in your browser: 56 | 57 | ``` 58 | echo http://$(terraform output load-balancer-ip) 59 | ``` 60 | 61 | You should see the example web page with __blue__ color. 62 | 63 | 3. Change the deployment color to green and perform update: 64 | 65 | ``` 66 | TF_VAR_deploy_color=green terraform apply 67 | ``` 68 | 69 | 4. Wait for blue-green deployment to complete: 70 | 71 | ``` 72 | ./test.sh 73 | ``` 74 | 75 | 5. Open the URL of the load balancer in your browser: 76 | 77 | ``` 78 | echo http://$(terraform output load-balancer-ip) 79 | ``` 80 | 81 | You should see the example web page with __green__ color. 82 | 83 | ## Cleanup 84 | 85 | 1. Remove all resources created by terraform: 86 | 87 | ``` 88 | terraform destroy 89 | ``` 90 | -------------------------------------------------------------------------------- /examples/blue-green/gceme.sh.tpl: -------------------------------------------------------------------------------- 1 | #!/bin/bash -xe 2 | 3 | apt-get update 4 | apt-get install -y apache2 libapache2-mod-php 5 | 6 | # install lookbusy script to test autoscaling 7 | apt-get install -y build-essential git 8 | cd /tmp 9 | git clone https://github.com/beloglazov/cpu-load-generator.git 10 | ./cpu-load-generator/install-lookbusy.sh 11 | cd - 12 | 13 | cat > /var/www/html/index.php <<'EOF' 14 | [ 18 | "method" => "GET", 19 | "header" => "Metadata-Flavor: Google" 20 | ] 21 | ]; 22 | $context = stream_context_create($opts); 23 | $content = file_get_contents("http://metadata/computeMetadata/v1/$value", false, $context); 24 | return $content; 25 | } 26 | ?> 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Frontend Web Server 37 | 38 | 39 |
40 |
41 |
 
42 |
43 | 44 | 45 |
46 |
47 |
Backend that serviced this request
48 |
49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
Name
ID
Hostname
Zone
Machine Type
Project
Internal IP
External IP
86 |
87 |
88 | 89 |
90 |
91 |
Proxy that handled this request
92 |
93 |
94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 |
Address
102 |
103 | 104 |
105 |
106 |
 
107 |
108 |
109 | 110 | EOF 111 | sudo mv /var/www/html/index.html /var/www/html/index.html.old 112 | 113 | [[ -n "${PROXY_PATH}" ]] && mkdir -p /var/www/html/${PROXY_PATH} && cp /var/www/html/index.php /var/www/html/${PROXY_PATH}/index.php 114 | 115 | systemctl enable apache2 116 | systemctl restart apache2 -------------------------------------------------------------------------------- /examples/blue-green/main.tf: -------------------------------------------------------------------------------- 1 | variable "rolling_update_policy" { 2 | type = "map" 3 | 4 | default = { 5 | type = "PROACTIVE" 6 | minimal_action = "REPLACE" 7 | max_surge_fixed = 2 8 | max_unavailable_fixed = 0 9 | min_ready_sec = 50 10 | } 11 | } 12 | 13 | variable "deploy_color" { 14 | default = "blue" 15 | } 16 | 17 | variable "region" { 18 | default = "us-central1" 19 | } 20 | 21 | variable "zone" { 22 | default = "us-central1-b" 23 | } 24 | 25 | provider "google" { 26 | region = "${var.region}" 27 | } 28 | 29 | variable "network_name" { 30 | default = "mig-blue-green-example" 31 | } 32 | 33 | resource "google_compute_network" "default" { 34 | name = "${var.network_name}" 35 | auto_create_subnetworks = "false" 36 | } 37 | 38 | resource "google_compute_subnetwork" "default" { 39 | name = "${var.network_name}" 40 | ip_cidr_range = "10.127.0.0/20" 41 | network = "${google_compute_network.default.self_link}" 42 | region = "${var.region}" 43 | private_ip_google_access = true 44 | } 45 | 46 | data "template_file" "startup-script" { 47 | template = "${file("${format("%s/gceme.sh.tpl", path.module)}")}" 48 | 49 | vars { 50 | PROXY_PATH = "" 51 | COLOR = "${var.deploy_color}" 52 | } 53 | } 54 | 55 | data "google_compute_zones" "available" { 56 | region = "${var.region}" 57 | } 58 | 59 | module "mig1" { 60 | source = "../../" 61 | region = "${var.region}" 62 | zone = "${var.zone}" 63 | name = "${var.network_name}" 64 | size = 2 65 | target_tags = ["${var.network_name}"] 66 | service_port = 80 67 | service_port_name = "http" 68 | startup_script = "${data.template_file.startup-script.rendered}" 69 | wait_for_instances = true 70 | http_health_check = false 71 | network = "${google_compute_subnetwork.default.name}" 72 | subnetwork = "${google_compute_subnetwork.default.name}" 73 | target_pools = ["${module.gce-lb-fr.target_pool}"] 74 | 75 | update_strategy = "ROLLING_UPDATE" 76 | rolling_update_policy = ["${var.rolling_update_policy}"] 77 | } 78 | 79 | module "gce-lb-fr" { 80 | source = "GoogleCloudPlatform/lb/google" 81 | version = "1.0.3" 82 | region = "${var.region}" 83 | name = "${var.network_name}" 84 | service_port = "${module.mig1.service_port}" 85 | target_tags = ["${module.mig1.target_tags}"] 86 | network = "${google_compute_subnetwork.default.name}" 87 | } 88 | 89 | output "load-balancer-ip" { 90 | value = "${module.gce-lb-fr.external_ip}" 91 | } 92 | 93 | output "deploy-color" { 94 | value = "${var.deploy_color}" 95 | } 96 | -------------------------------------------------------------------------------- /examples/blue-green/terraform.tfplan: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/terraform-google-managed-instance-group/b1557722d6ea188d2378ac4ef9d109eac46b47f0/examples/blue-green/terraform.tfplan -------------------------------------------------------------------------------- /examples/blue-green/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | set -e 5 | 6 | URL="http://$(terraform output load-balancer-ip)" 7 | status=0 8 | count=0 9 | while [[ $count -lt 120 && $status -ne 200 ]]; do 10 | echo "INFO: Waiting for load balancer..." 11 | status=$(curl -sf -m 5 -o /dev/null -w "%{http_code}" "${URL}" || true) 12 | ((count=count+1)) 13 | sleep 5 14 | done 15 | if [[ $count -lt 120 ]]; then 16 | echo "INFO: PASS" 17 | else 18 | echo "ERROR: Failed" 19 | exit 1 20 | fi 21 | 22 | DEPLOY_COLOR="$(terraform output deploy-color)" 23 | count=0 24 | while [[ $count -lt 120 ]]; do 25 | RES=$(curl -s -m 5 -w '%{http_code}\n' "${URL}" |grep "class=\"card ${DEPLOY_COLOR}" | tail -1 || true) 26 | echo "INFO: Current deployment: ${RES}" 27 | if [[ "${RES}" =~ $DEPLOY_COLOR ]]; then 28 | echo "INFO: PASS. Current deployment color: ${DEPLOY_COLOR}" 29 | exit 0 30 | fi 31 | ((count=count+1)) 32 | sleep 5 33 | done 34 | echo "ERROR: Failed" 35 | exit 1 36 | 37 | (while true; do ; sleep 1; done) -------------------------------------------------------------------------------- /examples/regional/README.md: -------------------------------------------------------------------------------- 1 | # Managed instance group regional example 2 | 3 | This example shows how to create a regional instance group and perform a rolling upgrade. 4 | 5 | ## Setup Environment 6 | 7 | ``` 8 | gcloud auth application-default login 9 | export GOOGLE_PROJECT=$(gcloud config get-value project) 10 | ``` 11 | 12 | ## Run Terraform 13 | 14 | ``` 15 | terraform init 16 | terraform plan 17 | terraform apply 18 | ``` 19 | 20 | The output variables display the 3 instances that were created and their current status. 21 | 22 | ``` 23 | terraform output 24 | ``` 25 | 26 | ## Rolling upgrade 27 | 28 | Add a label to the instance template, this will trigger a rolling update. 29 | 30 | ``` 31 | cat > terraform.tfvars < /var/www/html/index.php <<'EOF' 14 | [ 18 | "method" => "GET", 19 | "header" => "Metadata-Flavor: Google" 20 | ] 21 | ]; 22 | $context = stream_context_create($opts); 23 | $content = file_get_contents("http://metadata/computeMetadata/v1/$value", false, $context); 24 | return $content; 25 | } 26 | ?> 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | Frontend Web Server 37 | 38 | 39 |
40 |
41 |
 
42 |
43 | 44 | 45 |
46 |
47 |
Backend that serviced this request
48 |
49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
Name
ID
Hostname
Zone
Machine Type
Project
Internal IP
External IP
86 |
87 |
88 | 89 |
90 |
91 |
Proxy that handled this request
92 |
93 |
94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 |
Address
102 |
103 | 104 |
105 |
106 |
 
107 |
108 |
109 | 110 | EOF 111 | sudo mv /var/www/html/index.html /var/www/html/index.html.old 112 | 113 | [[ -n "${PROXY_PATH}" ]] && mkdir -p /var/www/html/${PROXY_PATH} && cp /var/www/html/index.php /var/www/html/${PROXY_PATH}/index.php 114 | 115 | systemctl enable apache2 116 | systemctl restart apache2 -------------------------------------------------------------------------------- /examples/regional/main.tf: -------------------------------------------------------------------------------- 1 | variable "labels" { 2 | type = "map" 3 | default = {} 4 | } 5 | 6 | variable "region" { 7 | default = "us-central1" 8 | } 9 | 10 | provider "google" { 11 | region = "${var.region}" 12 | } 13 | 14 | variable "network_name" { 15 | default = "mig-regional-example" 16 | } 17 | 18 | resource "google_compute_network" "default" { 19 | name = "${var.network_name}" 20 | auto_create_subnetworks = "false" 21 | } 22 | 23 | resource "google_compute_subnetwork" "default" { 24 | name = "${var.network_name}" 25 | ip_cidr_range = "10.127.0.0/20" 26 | network = "${google_compute_network.default.self_link}" 27 | region = "${var.region}" 28 | private_ip_google_access = true 29 | } 30 | 31 | data "template_file" "startup-script" { 32 | template = "${file("${format("%s/gceme.sh.tpl", path.module)}")}" 33 | 34 | vars { 35 | PROXY_PATH = "" 36 | } 37 | } 38 | 39 | data "google_compute_zones" "available" { 40 | region = "${var.region}" 41 | } 42 | 43 | module "mig1" { 44 | source = "../../" 45 | region = "${var.region}" 46 | distribution_policy_zones = ["${data.google_compute_zones.available.names}"] 47 | zonal = false 48 | name = "${var.network_name}" 49 | size = 3 50 | target_tags = ["${var.network_name}"] 51 | service_port = 80 52 | service_port_name = "http" 53 | startup_script = "${data.template_file.startup-script.rendered}" 54 | wait_for_instances = true 55 | http_health_check = false 56 | network = "${google_compute_subnetwork.default.name}" 57 | subnetwork = "${google_compute_subnetwork.default.name}" 58 | instance_labels = "${var.labels}" 59 | update_strategy = "ROLLING_UPDATE" 60 | 61 | rolling_update_policy = [{ 62 | type = "PROACTIVE" 63 | minimal_action = "REPLACE" 64 | max_surge_fixed = 4 65 | max_unavailable_fixed = 4 66 | min_ready_sec = 50 67 | }] 68 | } 69 | 70 | // null resource used to create dependency with the instance group data source to trigger a refresh. 71 | resource "null_resource" "template" { 72 | triggers { 73 | instance_template = "${element(module.mig1.instance_template, 0)}" 74 | } 75 | } 76 | 77 | data "google_compute_region_instance_group" "mig1" { 78 | self_link = "${module.mig1.region_instance_group}" 79 | depends_on = ["null_resource.template"] 80 | } 81 | 82 | output "instance_self_link_1" { 83 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[0], "instance")}" 84 | } 85 | 86 | output "instance_status_1" { 87 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[0], "status")}" 88 | } 89 | 90 | output "instance_self_link_2" { 91 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[1], "instance")}" 92 | } 93 | 94 | output "instance_status_2" { 95 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[1], "status")}" 96 | } 97 | 98 | output "instance_self_link_3" { 99 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[2], "instance")}" 100 | } 101 | 102 | output "instance_status_3" { 103 | value = "${lookup(data.google_compute_region_instance_group.mig1.instances[2], "status")}" 104 | } 105 | -------------------------------------------------------------------------------- /examples/regional/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | set -e 5 | 6 | function usage() { 7 | echo "USAGE: $0