Skip to content

Commit e07828d

Browse files
feat: Add option to specify zone in compute_instance (#162)
* Add option to specify zone in compute_instance Also added a test that creates two instances and ensure they're in the same specified zone. * Update docs * Update examples/compute_instance/simple/variables.tf Co-authored-by: Bharath KKB <[email protected]> * Update modules/compute_instance/variables.tf Co-authored-by: Bharath KKB <[email protected]> * Remove newlines * Update docs Co-authored-by: Bharath KKB <[email protected]>
1 parent a535e13 commit e07828d

File tree

14 files changed

+201
-1
lines changed

14 files changed

+201
-1
lines changed

.kitchen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ suites:
5050
name: terraform
5151
command_timeout: 1800
5252
root_module_directory: test/fixtures/compute_instance/simple
53+
- name: instance_simple_zone
54+
driver:
55+
name: terraform
56+
command_timeout: 1800
57+
root_module_directory: test/fixtures/compute_instance/simple_zone
5358
- name: disk_snapshot
5459
driver:
5560
name: terraform

examples/compute_instance/simple/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This is a simple, minimal example of how to use the compute_instance module
1414
| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no |
1515
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account. | <pre>object({<br> email = string,<br> scopes = set(string)<br> })</pre> | `null` | no |
1616
| subnetwork | The subnetwork selflink to host the compute instances in | `any` | n/a | yes |
17+
| zone | The GCP zone to create resources in | `string` | `null` | no |
1718

1819
## Outputs
1920

examples/compute_instance/simple/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module "instance_template" {
3030
module "compute_instance" {
3131
source = "../../../modules/compute_instance"
3232
region = var.region
33+
zone = var.zone
3334
subnetwork = var.subnetwork
3435
num_instances = var.num_instances
3536
hostname = "instance-simple"

examples/compute_instance/simple/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ variable "region" {
2727
default = "us-central1"
2828
}
2929

30+
variable "zone" {
31+
description = "The GCP zone to create resources in"
32+
type = string
33+
default = null
34+
}
35+
3036
variable "subnetwork" {
3137
description = "The subnetwork selflink to host the compute instances in"
3238
}

modules/compute_instance/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
2424
| static\_ips | List of static IPs for VM instances | `list(string)` | `[]` | no |
2525
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no |
2626
| subnetwork\_project | The project that subnetwork belongs to | `string` | `""` | no |
27+
| zone | Zone where the instances should be created. If not specified, instances will be spread across available zones in the region. | `string` | `null` | no |
2728

2829
## Outputs
2930

modules/compute_instance/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ resource "google_compute_instance_from_template" "compute_instance" {
4343
count = local.num_instances
4444
name = "${local.hostname}-${format("%03d", count.index + 1)}"
4545
project = local.project_id
46-
zone = data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)]
46+
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
4747

4848
network_interface {
4949
network = var.network

modules/compute_instance/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ variable "region" {
6464
default = null
6565
}
6666

67+
variable "zone" {
68+
type = string
69+
description = "Zone where the instances should be created. If not specified, instances will be spread across available zones in the region."
70+
default = null
71+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
module "instance_simple" {
19+
source = "../../../../examples/compute_instance/simple"
20+
project_id = var.project_id
21+
region = "us-central1"
22+
zone = "us-central1-b"
23+
subnetwork = google_compute_subnetwork.main.self_link
24+
num_instances = 2
25+
service_account = var.service_account
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../shared/network.tf
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "instances_self_links" {
18+
description = "List of instance self-links"
19+
value = module.instance_simple.instances_self_links
20+
}
21+
22+
output "project_id" {
23+
description = "The GCP project to use for integration tests"
24+
value = var.project_id
25+
}
26+

0 commit comments

Comments
 (0)