From 98f4e1aa9888e2701814f427d8daf359f6ccbc4b Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 17 Apr 2025 22:21:04 +0000 Subject: [PATCH] Resource policy attachment (#13059) Co-authored-by: Nick Elliot [upstream:1d77827f11e7e98cb95b42acdf059e05bf18603f] Signed-off-by: Modular Magician --- .../backing_file.tf | 15 ++++ .../main.tf | 43 ++++++++++ compute_resource_policy_attachment_basic/motd | 7 ++ .../tutorial.md | 79 +++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 compute_resource_policy_attachment_basic/backing_file.tf create mode 100644 compute_resource_policy_attachment_basic/main.tf create mode 100644 compute_resource_policy_attachment_basic/motd create mode 100644 compute_resource_policy_attachment_basic/tutorial.md diff --git a/compute_resource_policy_attachment_basic/backing_file.tf b/compute_resource_policy_attachment_basic/backing_file.tf new file mode 100644 index 00000000..c60b1199 --- /dev/null +++ b/compute_resource_policy_attachment_basic/backing_file.tf @@ -0,0 +1,15 @@ +# This file has some scaffolding to make sure that names are unique and that +# a region and zone are selected when you try to create your Terraform resources. + +locals { + name_suffix = "${random_pet.suffix.id}" +} + +resource "random_pet" "suffix" { + length = 2 +} + +provider "google" { + region = "us-central1" + zone = "us-central1-c" +} diff --git a/compute_resource_policy_attachment_basic/main.tf b/compute_resource_policy_attachment_basic/main.tf new file mode 100644 index 00000000..34c043b7 --- /dev/null +++ b/compute_resource_policy_attachment_basic/main.tf @@ -0,0 +1,43 @@ +resource "google_compute_instance" "instance" { + name = "my-instance-${local.name_suffix}" + machine_type = "e2-medium" + zone = "us-central1-a" + + boot_disk { + initialize_params { + image = "debian-cloud/debian-11" + } + } + + network_interface { + network = "default" + access_config {} + } + + lifecycle { + ignore_changes = [resource_policies] + } +} + +resource "google_compute_resource_policy" "policy" { + name = "my-resource-policy-${local.name_suffix}" + region = "us-central1" + + instance_schedule_policy { + vm_start_schedule { + schedule = "0 8 * * *" + } + + vm_stop_schedule { + schedule = "0 18 * * *" + } + + time_zone = "America/New_York" + } +} + +resource "google_compute_resource_policy_attachment" "attachment" { + name = google_compute_resource_policy.policy.name + instance = google_compute_instance.instance.name + zone = "us-central1-a" +} diff --git a/compute_resource_policy_attachment_basic/motd b/compute_resource_policy_attachment_basic/motd new file mode 100644 index 00000000..45a906e8 --- /dev/null +++ b/compute_resource_policy_attachment_basic/motd @@ -0,0 +1,7 @@ +=== + +These examples use real resources that will be billed to the +Google Cloud Platform project you use - so make sure that you +run "terraform destroy" before quitting! + +=== diff --git a/compute_resource_policy_attachment_basic/tutorial.md b/compute_resource_policy_attachment_basic/tutorial.md new file mode 100644 index 00000000..6213af8c --- /dev/null +++ b/compute_resource_policy_attachment_basic/tutorial.md @@ -0,0 +1,79 @@ +# Compute Resource Policy Attachment Basic - Terraform + +## Setup + + + +Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform. + + + +Terraform provisions real GCP resources, so anything you create in this session will be billed against this project. + +## Terraforming! + +Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command +to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up +the project name from the environment variable. + +```bash +export GOOGLE_CLOUD_PROJECT={{project-id}} +``` + +After that, let's get Terraform started. Run the following to pull in the providers. + +```bash +terraform init +``` + +With the providers downloaded and a project set, you're ready to use Terraform. Go ahead! + +```bash +terraform apply +``` + +Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan. + +```bash +yes +``` + + +## Post-Apply + +### Editing your config + +Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed. + +```bash +terraform plan +``` + +So let's make a change! Try editing a number, or appending a value to the name in the editor. Then, +run a 'plan' again. + +```bash +terraform plan +``` + +Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes +at the 'yes' prompt. + +```bash +terraform apply +``` + +```bash +yes +``` + +## Cleanup + +Run the following to remove the resources Terraform provisioned: + +```bash +terraform destroy +``` +```bash +yes +```