ResourcePolicyAttachment class

Adds existing resource policies to a compute instance. You can only add one policy which will be applied to this instance for scheduling start/stop operations.

This resource can be used instead of setting the resourcePolicy directly in the compute instance resource to avoid dependency issues when using instance-level IAM permissions.

Example Usage

Compute Resource Policy Attachment Basic

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const instance = new gcp.compute.Instance("instance", {
    networkInterfaces: [{
        accessConfigs: [{}],
        network: "default",
    }],
    name: "my-instance",
    machineType: "e2-medium",
    zone: "us-central1-a",
    bootDisk: {
        initializeParams: {
            image: "debian-cloud/debian-11",
        },
    },
});
const policy = new gcp.compute.ResourcePolicy("policy", {
    name: "my-resource-policy",
    region: "us-central1",
    instanceSchedulePolicy: {
        vmStartSchedule: {
            schedule: "0 8 * * *",
        },
        vmStopSchedule: {
            schedule: "0 18 * * *",
        },
        timeZone: "America/New_York",
    },
});
const attachment = new gcp.compute.ResourcePolicyAttachment("attachment", {
    name: policy.name,
    instance: instance.name,
    zone: "us-central1-a",
});
import pulumi
import pulumi_gcp as gcp

instance = gcp.compute.Instance("instance",
    network_interfaces=[{
        "access_configs": [{}],
        "network": "default",
    }],
    name="my-instance",
    machine_type="e2-medium",
    zone="us-central1-a",
    boot_disk={
        "initialize_params": {
            "image": "debian-cloud/debian-11",
        },
    })
policy = gcp.compute.ResourcePolicy("policy",
    name="my-resource-policy",
    region="us-central1",
    instance_schedule_policy={
        "vm_start_schedule": {
            "schedule": "0 8 * * *",
        },
        "vm_stop_schedule": {
            "schedule": "0 18 * * *",
        },
        "time_zone": "America/New_York",
    })
attachment = gcp.compute.ResourcePolicyAttachment("attachment",
    name=policy.name,
    instance=instance.name,
    zone="us-central1-a")
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;

return await Deployment.RunAsync(() =>
{
    var instance = new Gcp.Compute.Instance("instance", new()
    {
        NetworkInterfaces = new[]
        {
            new Gcp.Compute.Inputs.InstanceNetworkInterfaceArgs
            {
                AccessConfigs = new[]
                {
                    null,
                },
                Network = "default",
            },
        },
        Name = "my-instance",
        MachineType = "e2-medium",
        Zone = "us-central1-a",
        BootDisk = new Gcp.Compute.Inputs.InstanceBootDiskArgs
        {
            InitializeParams = new Gcp.Compute.Inputs.InstanceBootDiskInitializeParamsArgs
            {
                Image = "debian-cloud/debian-11",
            },
        },
    });

    var policy = new Gcp.Compute.ResourcePolicy("policy", new()
    {
        Name = "my-resource-policy",
        Region = "us-central1",
        InstanceSchedulePolicy = new Gcp.Compute.Inputs.ResourcePolicyInstanceSchedulePolicyArgs
        {
            VmStartSchedule = new Gcp.Compute.Inputs.ResourcePolicyInstanceSchedulePolicyVmStartScheduleArgs
            {
                Schedule = "0 8 * * *",
            },
            VmStopSchedule = new Gcp.Compute.Inputs.ResourcePolicyInstanceSchedulePolicyVmStopScheduleArgs
            {
                Schedule = "0 18 * * *",
            },
            TimeZone = "America/New_York",
        },
    });

    var attachment = new Gcp.Compute.ResourcePolicyAttachment("attachment", new()
    {
        Name = policy.Name,
        Instance = instance.Name,
        Zone = "us-central1-a",
    });

});
package main

import (
	"github.com/pulumi/pulumi-gcp/sdk/v9/go/gcp/compute"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		instance, err := compute.NewInstance(ctx, "instance", &compute.InstanceArgs{
			NetworkInterfaces: compute.InstanceNetworkInterfaceArray{
				&compute.InstanceNetworkInterfaceArgs{
					AccessConfigs: compute.InstanceNetworkInterfaceAccessConfigArray{
						&compute.InstanceNetworkInterfaceAccessConfigArgs{},
					},
					Network: pulumi.String("default"),
				},
			},
			Name:        pulumi.String("my-instance"),
			MachineType: pulumi.String("e2-medium"),
			Zone:        pulumi.String("us-central1-a"),
			BootDisk: &compute.InstanceBootDiskArgs{
				InitializeParams: &compute.InstanceBootDiskInitializeParamsArgs{
					Image: pulumi.String("debian-cloud/debian-11"),
				},
			},
		})
		if err != nil {
			return err
		}
		policy, err := compute.NewResourcePolicy(ctx, "policy", &compute.ResourcePolicyArgs{
			Name:   pulumi.String("my-resource-policy"),
			Region: pulumi.String("us-central1"),
			InstanceSchedulePolicy: &compute.ResourcePolicyInstanceSchedulePolicyArgs{
				VmStartSchedule: &compute.ResourcePolicyInstanceSchedulePolicyVmStartScheduleArgs{
					Schedule: pulumi.String("0 8 * * *"),
				},
				VmStopSchedule: &compute.ResourcePolicyInstanceSchedulePolicyVmStopScheduleArgs{
					Schedule: pulumi.String("0 18 * * *"),
				},
				TimeZone: pulumi.String("America/New_York"),
			},
		})
		if err != nil {
			return err
		}
		_, err = compute.NewResourcePolicyAttachment(ctx, "attachment", &compute.ResourcePolicyAttachmentArgs{
			Name:     policy.Name,
			Instance: instance.Name,
			Zone:     pulumi.String("us-central1-a"),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    gcp = {
      source = "pulumi/gcp"
    }
  }
}

resource "gcp_compute_instance" "instance" {
  network_interfaces {
    access_configs {
    }
    network = "default"
  }
  name         = "my-instance"
  machine_type = "e2-medium"
  zone         = "us-central1-a"
  boot_disk = {
    initialize_params = {
      image = "debian-cloud/debian-11"
    }
  }
}
resource "gcp_compute_resourcepolicy" "policy" {
  name   = "my-resource-policy"
  region = "us-central1"
  instance_schedule_policy = {
    vm_start_schedule = {
      schedule = "0 8 * * *"
    }
    vm_stop_schedule = {
      schedule = "0 18 * * *"
    }
    time_zone = "America/New_York"
  }
}
resource "gcp_compute_resourcepolicyattachment" "attachment" {
  name     = gcp_compute_resourcepolicy.policy.name
  instance = gcp_compute_instance.instance.name
  zone     = "us-central1-a"
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.compute.Instance;
import com.pulumi.gcp.compute.InstanceArgs;
import com.pulumi.gcp.compute.inputs.InstanceNetworkInterfaceArgs;
import com.pulumi.gcp.compute.inputs.InstanceNetworkInterfaceAccessConfigArgs;
import com.pulumi.gcp.compute.inputs.InstanceBootDiskArgs;
import com.pulumi.gcp.compute.inputs.InstanceBootDiskInitializeParamsArgs;
import com.pulumi.gcp.compute.ResourcePolicy;
import com.pulumi.gcp.compute.ResourcePolicyArgs;
import com.pulumi.gcp.compute.inputs.ResourcePolicyInstanceSchedulePolicyArgs;
import com.pulumi.gcp.compute.inputs.ResourcePolicyInstanceSchedulePolicyVmStartScheduleArgs;
import com.pulumi.gcp.compute.inputs.ResourcePolicyInstanceSchedulePolicyVmStopScheduleArgs;
import com.pulumi.gcp.compute.ResourcePolicyAttachment;
import com.pulumi.gcp.compute.ResourcePolicyAttachmentArgs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var instance = new Instance("instance", InstanceArgs.builder()
            .networkInterfaces(InstanceNetworkInterfaceArgs.builder()
                .accessConfigs(InstanceNetworkInterfaceAccessConfigArgs.builder()
                    .build())
                .network("default")
                .build())
            .name("my-instance")
            .machineType("e2-medium")
            .zone("us-central1-a")
            .bootDisk(InstanceBootDiskArgs.builder()
                .initializeParams(InstanceBootDiskInitializeParamsArgs.builder()
                    .image("debian-cloud/debian-11")
                    .build())
                .build())
            .build());

        var policy = new ResourcePolicy("policy", ResourcePolicyArgs.builder()
            .name("my-resource-policy")
            .region("us-central1")
            .instanceSchedulePolicy(ResourcePolicyInstanceSchedulePolicyArgs.builder()
                .vmStartSchedule(ResourcePolicyInstanceSchedulePolicyVmStartScheduleArgs.builder()
                    .schedule("0 8 * * *")
                    .build())
                .vmStopSchedule(ResourcePolicyInstanceSchedulePolicyVmStopScheduleArgs.builder()
                    .schedule("0 18 * * *")
                    .build())
                .timeZone("America/New_York")
                .build())
            .build());

        var attachment = new ResourcePolicyAttachment("attachment", ResourcePolicyAttachmentArgs.builder()
            .name(policy.name())
            .instance(instance.name())
            .zone("us-central1-a")
            .build());

    }
}
resources:
  instance:
    type: gcp:compute:Instance
    properties:
      networkInterfaces:
        - accessConfigs:
            - {}
          network: default
      name: my-instance
      machineType: e2-medium
      zone: us-central1-a
      bootDisk:
        initializeParams:
          image: debian-cloud/debian-11
  policy:
    type: gcp:compute:ResourcePolicy
    properties:
      name: my-resource-policy
      region: us-central1
      instanceSchedulePolicy:
        vmStartSchedule:
          schedule: 0 8 * * *
        vmStopSchedule:
          schedule: 0 18 * * *
        timeZone: America/New_York
  attachment:
    type: gcp:compute:ResourcePolicyAttachment
    properties:
      name: ${policy.name}
      instance: ${instance.name}
      zone: us-central1-a

Import

ResourcePolicyAttachment can be imported using any of these accepted formats:

  • projects/{{project}}/zones/{{zone}}/instances/{{instance}}/{{name}}
  • {{project}}/{{zone}}/{{instance}}/{{name}}
  • {{zone}}/{{instance}}/{{name}}
  • {{instance}}/{{name}}

When using the pulumi import command, ResourcePolicyAttachment can be imported using one of the formats above. For example:

$ pulumi import gcp:compute/resourcePolicyAttachment:ResourcePolicyAttachment default projects/{{project}}/zones/{{zone}}/instances/{{instance}}/{{name}}
$ pulumi import gcp:compute/resourcePolicyAttachment:ResourcePolicyAttachment default {{project}}/{{zone}}/{{instance}}/{{name}}
$ pulumi import gcp:compute/resourcePolicyAttachment:ResourcePolicyAttachment default {{zone}}/{{instance}}/{{name}}
$ pulumi import gcp:compute/resourcePolicyAttachment:ResourcePolicyAttachment default {{instance}}/{{name}}

Constructors

ResourcePolicyAttachment(String name, {ResourcePolicyAttachmentArgs? args, CustomResourceOptions? options})
Creates a new ResourcePolicyAttachment. name The Pulumi resource name. args Arguments used to configure this ResourcePolicyAttachment. The set of arguments for ResourcePolicyAttachment. options Resource options controlling this resource's behavior.
ResourcePolicyAttachment.reference(String urn)
Creates a typed reference to an existing ResourcePolicyAttachment resource.

Properties

childResources → Set<Resource>
finalinherited
completionSources ↔ Map<String, IOutputCompletionSource>
latefinalinherited
deletionPolicy ↔ Output<String>
Whether Terraform will be prevented from destroying the resource. Defaults to DELETE. When a 'terraform destroy' or 'pulumi up' would delete the resource, the command will fail if this field is set to "PREVENT" in Terraform state. When set to "ABANDON", the command will remove the resource from Terraform management without updating or deleting the resource in the API. When set to "DELETE", deleting the resource is allowed.
latefinal
hashCode → int
The hash code for this object.
no setterinherited
id ↔ Output<String>
getter/setter pairinherited
instance ↔ Output<String>
The name of the instance in which the resource policies are attached to.
latefinal
isCustom → bool
Returns whether this resource is provider-managed.
no setterinherited
isProtected → bool
Returns whether this resource is protected from deletion.
no setterinherited
isRemote → bool
Whether this resource is registered as remote.
no setterinherited
isResourceReference → bool
Whether this instance represents a resource value returned over RPC.
finalinherited
name ↔ Output<String>
The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
latefinal
project ↔ Output<String>
The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
latefinal
resourceTransforms → List<ResourceTransform>
Inherited/explicit async transforms.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
transformations → List<ResourceTransformation>
Inherited/explicit legacy transformations.
no setterinherited
urn ↔ Output<String>
latefinalinherited
zone ↔ Output<String>
A reference to the zone where the instance resides.
latefinal

Methods

failId(Object error) → void
Completes this resource ID with an error when registration fails.
inherited
failOutputs(Object error) → void
Completes all output properties with error.
inherited
failUrn(Object error) → void
Completes this resource URN with an error when registration fails.
inherited
getProvider(String moduleMember) → ProviderResource?
Returns provider for moduleMember's package, if configured.
inherited
getResourceName() → String
Returns this resource's logical name.
inherited
getResourceType() → String
Returns this resource's Pulumi type token.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerOutput<T>(String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output<T>
Registers a dynamic output property for this resource.
inherited
resolveId(String? value, {required bool isKnown}) → void
Resolves the provider-assigned ID for this resource.
inherited
resolveOutputs(Struct outputs) → void
Resolves all output properties from a monitor response payload.
inherited
resolveUrn(String value) → void
Resolves this resource's URN once assigned by the engine.
inherited
serializeProperties(Map<String, dynamic> properties) → Future<Struct>
Serializes resource properties for RPC transmission.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

get(String name, Input<String> id, {ResourcePolicyAttachmentState? state, CustomResourceOptions? options}) → ResourcePolicyAttachment
Gets an existing ResourcePolicyAttachment resource's state with the given name and id.