Skip to content

googleComputeResourcePolicy

A policy that can be attached to a resource to specify or schedule actions on that resource.

Example Usage - Resource Policy Basic

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "foo", {
  name: "gce-policy",
  region: "us-central1",
  snapshot_schedule_policy: [
    {
      schedule: [
        {
          daily_schedule: [
            {
              days_in_cycle: 1,
              start_time: "04:00",
            },
          ],
        },
      ],
    },
  ],
});

Example Usage - Resource Policy Full

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "bar", {
  name: "gce-policy",
  region: "us-central1",
  snapshot_schedule_policy: [
    {
      retention_policy: [
        {
          max_retention_days: 10,
          on_source_disk_delete: "KEEP_AUTO_SNAPSHOTS",
        },
      ],
      schedule: [
        {
          hourly_schedule: [
            {
              hours_in_cycle: 20,
              start_time: "23:00",
            },
          ],
        },
      ],
      snapshot_properties: [
        {
          guest_flush: true,
          labels: [
            {
              my_label: "value",
            },
          ],
          storage_locations: ["us"],
        },
      ],
    },
  ],
});

Example Usage - Resource Policy Placement Policy

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "baz", {
  group_placement_policy: [
    {
      collocation: "COLLOCATED",
      vm_count: 2,
    },
  ],
  name: "gce-policy",
  region: "us-central1",
});

Example Usage - Resource Policy Placement Policy Max Distance

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "baz", {
  group_placement_policy: [
    {
      collocation: "COLLOCATED",
      max_distance: 2,
      vm_count: 2,
    },
  ],
  name: "gce-policy",
  provider: "${google-beta}",
  region: "us-central1",
});

Example Usage - Resource Policy Instance Schedule Policy

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "hourly", {
  description: "Start and stop instances",
  instance_schedule_policy: [
    {
      time_zone: "US/Central",
      vm_start_schedule: [
        {
          schedule: "0 * * * *",
        },
      ],
      vm_stop_schedule: [
        {
          schedule: "15 * * * *",
        },
      ],
    },
  ],
  name: "gce-policy",
  region: "us-central1",
});

Example Usage - Resource Policy Snapshot Schedule Chain Name

/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as google from "./.gen/providers/google";
/*The following providers are missing schema information and might need manual adjustments to synthesize correctly: google.
For a more precise conversion please use the --provider flag in convert.*/
new google.computeResourcePolicy.ComputeResourcePolicy(this, "hourly", {
  description: "chain name snapshot",
  name: "gce-policy",
  region: "us-central1",
  snapshot_schedule_policy: [
    {
      retention_policy: [
        {
          max_retention_days: 14,
          on_source_disk_delete: "KEEP_AUTO_SNAPSHOTS",
        },
      ],
      schedule: [
        {
          hourly_schedule: [
            {
              hours_in_cycle: 20,
              start_time: "23:00",
            },
          ],
        },
      ],
      snapshot_properties: [
        {
          chain_name: "test-schedule-chain-name",
          guest_flush: true,
          labels: [
            {
              my_label: "value",
            },
          ],
          storage_locations: ["us"],
        },
      ],
    },
  ],
});

Argument Reference

The following arguments are supported:

  • name - (Required) The name of the resource, provided by the client when initially creating the resource. The resource name must be 1-63 characters long, and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression [aZ]([AZ09]*[aZ09])? which means the first character must be a lowercase letter, and all following characters must be a dash, lowercase letter, or digit, except the last character, which cannot be a dash.

  • description - (Optional) An optional description of this resource. Provide this property when you create the resource.

  • snapshotSchedulePolicy - (Optional) Policy for creating snapshots of persistent disks. Structure is documented below.

  • groupPlacementPolicy - (Optional) Resource policy for instances used for placement configuration. Structure is documented below.

  • instanceSchedulePolicy - (Optional) Resource policy for scheduling instance operations. Structure is documented below.

  • region - (Optional) Region where resource policy resides.

  • project - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

The snapshotSchedulePolicy block supports:

  • schedule - (Required) Contains one of an hourlySchedule, dailySchedule, or weeklySchedule. Structure is documented below.

  • retentionPolicy - (Optional) Retention policy applied to snapshots created by this resource policy. Structure is documented below.

  • snapshotProperties - (Optional) Properties with which the snapshots are created, such as labels. Structure is documented below.

The schedule block supports:

  • hourlySchedule - (Optional) The policy will execute every nth hour starting at the specified time. Structure is documented below.

  • dailySchedule - (Optional) The policy will execute every nth day at the specified time. Structure is documented below.

  • weeklySchedule - (Optional) Allows specifying a snapshot time for each day of the week. Structure is documented below.

The hourlySchedule block supports:

  • hoursInCycle - (Required) The number of hours between snapshots.

  • startTime - (Required) Time within the window to start the operations. It must be in an hourly format "HH:MM", where HH : [00-23] and MM : [00] GMT. eg: 21:00

The dailySchedule block supports:

  • daysInCycle - (Required) The number of days between snapshots.

  • startTime - (Required) This must be in UTC format that resolves to one of 00:00, 04:00, 08:00, 12:00, 16:00, or 20:00. For example, both 13:00-5 and 08:00 are valid.

The weeklySchedule block supports:

  • dayOfWeeks - (Required) May contain up to seven (one for each day of the week) snapshot times. Structure is documented below.

The dayOfWeeks block supports:

  • startTime - (Required) Time within the window to start the operations. It must be in format "HH:MM", where HH : [00-23] and MM : [00-00] GMT.

  • day - (Required) The day of the week to create the snapshot. e.g. MONDAY Possible values are monday, tuesday, wednesday, thursday, friday, saturday, and sunday.

The retentionPolicy block supports:

  • maxRetentionDays - (Required) Maximum age of the snapshot that is allowed to be kept.

  • onSourceDiskDelete - (Optional) Specifies the behavior to apply to scheduled snapshots when the source disk is deleted. Default value is keepAutoSnapshots. Possible values are keepAutoSnapshots and applyRetentionPolicy.

The snapshotProperties block supports:

  • labels - (Optional) A set of key-value pairs.

  • storageLocations - (Optional) Cloud Storage bucket location to store the auto snapshot (regional or multi-regional)

  • guestFlush - (Optional) Whether to perform a 'guest aware' snapshot.

  • chainName - (Optional) Creates the new snapshot in the snapshot chain labeled with the specified name. The chain name must be 1-63 characters long and comply with RFC1035.

The groupPlacementPolicy block supports:

  • vmCount - (Optional) Number of VMs in this placement group. Google does not recommend that you use this field unless you use a compact policy and you want your policy to work only if it contains this exact number of VMs.

  • availabilityDomainCount - (Optional) The number of availability domains instances will be spread across. If two instances are in different availability domain, they will not be put in the same low latency network

  • collocation - (Optional) Collocation specifies whether to place VMs inside the same availability domain on the same low-latency network. Specify collocated to enable collocation. Can only be specified with vmCount. If compute instances are created with a COLLOCATED policy, then exactly vmCount instances must be created at the same time with the resource policy attached. Possible values are collocated.

  • maxDistance - (Optional, Beta) Specifies the number of max logical switches.

The instanceSchedulePolicy block supports:

  • vmStartSchedule - (Optional) Specifies the schedule for starting instances. Structure is documented below.

  • vmStopSchedule - (Optional) Specifies the schedule for stopping instances. Structure is documented below.

  • timeZone - (Required) Specifies the time zone to be used in interpreting the schedule. The value of this field must be a time zone name from the tz database: http://en.wikipedia.org/wiki/Tz_database.

  • startTime - (Optional) The start time of the schedule. The timestamp is an RFC3339 string.

  • expirationTime - (Optional) The expiration time of the schedule. The timestamp is an RFC3339 string.

The vmStartSchedule block supports:

  • schedule - (Required) Specifies the frequency for the operation, using the unix-cron format.

The vmStopSchedule block supports:

  • schedule - (Required) Specifies the frequency for the operation, using the unix-cron format.

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

  • id - an identifier for the resource with format projects/{{project}}/regions/{{region}}/resourcePolicies/{{name}}
  • selfLink - The URI of the created resource.

Timeouts

This resource provides the following Timeouts configuration options:

  • create - Default is 20 minutes.
  • delete - Default is 20 minutes.

Import

ResourcePolicy can be imported using any of these accepted formats:

$ terraform import google_compute_resource_policy.default projects/{{project}}/regions/{{region}}/resourcePolicies/{{name}}
$ terraform import google_compute_resource_policy.default {{project}}/{{region}}/{{name}}
$ terraform import google_compute_resource_policy.default {{region}}/{{name}}
$ terraform import google_compute_resource_policy.default {{name}}

User Project Overrides

This resource supports User Project Overrides.