Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions terraform-modules/docker-instance-data-disk/instance.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ data "google_compute_subnetwork" "instance-subnetwork" {
region = var.instance_region
}

# instance resource policy for adding start/stop schedule
resource "google_compute_resource_policy" "resource-policy" {
count = var.enable_resource_policy ? 1 : 0
provider = google.target
project = var.project
region = var.instance_region
name = "${var.instance_name}-resource-policy"

instance_schedule_policy {
vm_start_schedule {
schedule = var.instance_schedule_vm_start
}
vm_stop_schedule {
schedule = var.instance_schedule_vm_stop
}
time_zone = var.instance_schedule_time_zone
}
}

# GCE instance
resource "google_compute_instance" "instance" {
provider = google.target
Expand Down Expand Up @@ -62,6 +81,9 @@ resource "google_compute_instance" "instance" {
device_name = var.instance_data_disk_name
}

# instance resource policies
resource_policies = var.enable_resource_policy ? [ google_compute_resource_policy.resource-policy[0].self_link ] : null

lifecycle {
prevent_destroy = false
}
Expand Down
30 changes: 26 additions & 4 deletions terraform-modules/docker-instance-data-disk/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ variable "instance_scopes" {
type = list(string)
description = "The default scopes for instance"
default = [
"userinfo-email",
"compute-ro",
"storage-ro",
"https://www.googleapis.com/auth/monitoring.write",
"userinfo-email",
"compute-ro",
"storage-ro",
"https://www.googleapis.com/auth/monitoring.write",
"logging-write" ]
}

Expand Down Expand Up @@ -139,3 +139,25 @@ variable "instance_data_disk_name" {
default = "data"
description = "default disk type for docker volume"
}

# control adding resource policy to GCE instances
# sets instance schedules to start/stop VMs automatically via cron schedule
# see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#example-usage---resource-policy-instance-schedule-policy
variable "enable_resource_policy" {
default = false
}

variable "instance_schedule_vm_start" {
description = "Cron schedule for starting GCE instances using policy; default 9AM daily"
default = "0 9 * * *"
}

variable "instance_schedule_vm_stop" {
description = "Cron schedule for stopping GCE instances using policy; default 5PM daily"
default = "0 17 * * *"
}

variable "instance_schedule_time_zone" {
description = "Timezone for GCE instance schedule policies"
default = "US/Central"
}