1. Packages
  2. Google Cloud (GCP) Classic
  3. API Docs
  4. compute
  5. ResourcePolicyAttachment
Google Cloud v8.28.0 published on Tuesday, Apr 29, 2025 by Pulumi

gcp.compute.ResourcePolicyAttachment

Explore with Pulumi AI

gcp logo
Google Cloud v8.28.0 published on Tuesday, Apr 29, 2025 by Pulumi

    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 resource_policy 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")
    
    package main
    
    import (
    	"github.com/pulumi/pulumi-gcp/sdk/v8/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
    	})
    }
    
    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 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.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.List;
    import java.util.ArrayList;
    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
    

    Create ResourcePolicyAttachment Resource

    Resources are created with functions called constructors. To learn more about declaring and configuring resources, see Resources.

    Constructor syntax

    new ResourcePolicyAttachment(name: string, args: ResourcePolicyAttachmentArgs, opts?: CustomResourceOptions);
    @overload
    def ResourcePolicyAttachment(resource_name: str,
                                 args: ResourcePolicyAttachmentArgs,
                                 opts: Optional[ResourceOptions] = None)
    
    @overload
    def ResourcePolicyAttachment(resource_name: str,
                                 opts: Optional[ResourceOptions] = None,
                                 instance: Optional[str] = None,
                                 name: Optional[str] = None,
                                 project: Optional[str] = None,
                                 zone: Optional[str] = None)
    func NewResourcePolicyAttachment(ctx *Context, name string, args ResourcePolicyAttachmentArgs, opts ...ResourceOption) (*ResourcePolicyAttachment, error)
    public ResourcePolicyAttachment(string name, ResourcePolicyAttachmentArgs args, CustomResourceOptions? opts = null)
    public ResourcePolicyAttachment(String name, ResourcePolicyAttachmentArgs args)
    public ResourcePolicyAttachment(String name, ResourcePolicyAttachmentArgs args, CustomResourceOptions options)
    
    type: gcp:compute:ResourcePolicyAttachment
    properties: # The arguments to resource properties.
    options: # Bag of options to control resource's behavior.
    
    

    Parameters

    name string
    The unique name of the resource.
    args ResourcePolicyAttachmentArgs
    The arguments to resource properties.
    opts CustomResourceOptions
    Bag of options to control resource's behavior.
    resource_name str
    The unique name of the resource.
    args ResourcePolicyAttachmentArgs
    The arguments to resource properties.
    opts ResourceOptions
    Bag of options to control resource's behavior.
    ctx Context
    Context object for the current deployment.
    name string
    The unique name of the resource.
    args ResourcePolicyAttachmentArgs
    The arguments to resource properties.
    opts ResourceOption
    Bag of options to control resource's behavior.
    name string
    The unique name of the resource.
    args ResourcePolicyAttachmentArgs
    The arguments to resource properties.
    opts CustomResourceOptions
    Bag of options to control resource's behavior.
    name String
    The unique name of the resource.
    args ResourcePolicyAttachmentArgs
    The arguments to resource properties.
    options CustomResourceOptions
    Bag of options to control resource's behavior.

    Constructor example

    The following reference example uses placeholder values for all input properties.

    var resourcePolicyAttachmentResource = new Gcp.Compute.ResourcePolicyAttachment("resourcePolicyAttachmentResource", new()
    {
        Instance = "string",
        Name = "string",
        Project = "string",
        Zone = "string",
    });
    
    example, err := compute.NewResourcePolicyAttachment(ctx, "resourcePolicyAttachmentResource", &compute.ResourcePolicyAttachmentArgs{
    	Instance: pulumi.String("string"),
    	Name:     pulumi.String("string"),
    	Project:  pulumi.String("string"),
    	Zone:     pulumi.String("string"),
    })
    
    var resourcePolicyAttachmentResource = new ResourcePolicyAttachment("resourcePolicyAttachmentResource", ResourcePolicyAttachmentArgs.builder()
        .instance("string")
        .name("string")
        .project("string")
        .zone("string")
        .build());
    
    resource_policy_attachment_resource = gcp.compute.ResourcePolicyAttachment("resourcePolicyAttachmentResource",
        instance="string",
        name="string",
        project="string",
        zone="string")
    
    const resourcePolicyAttachmentResource = new gcp.compute.ResourcePolicyAttachment("resourcePolicyAttachmentResource", {
        instance: "string",
        name: "string",
        project: "string",
        zone: "string",
    });
    
    type: gcp:compute:ResourcePolicyAttachment
    properties:
        instance: string
        name: string
        project: string
        zone: string
    

    ResourcePolicyAttachment Resource Properties

    To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.

    Inputs

    In Python, inputs that are objects can be passed either as argument classes or as dictionary literals.

    The ResourcePolicyAttachment resource accepts the following input properties:

    Instance string
    The name of the instance in which the resource policies are attached to.


    Name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    Project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    Zone string
    A reference to the zone where the instance resides.
    Instance string
    The name of the instance in which the resource policies are attached to.


    Name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    Project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    Zone string
    A reference to the zone where the instance resides.
    instance String
    The name of the instance in which the resource policies are attached to.


    name String
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project String
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone String
    A reference to the zone where the instance resides.
    instance string
    The name of the instance in which the resource policies are attached to.


    name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone string
    A reference to the zone where the instance resides.
    instance str
    The name of the instance in which the resource policies are attached to.


    name str
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project str
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone str
    A reference to the zone where the instance resides.
    instance String
    The name of the instance in which the resource policies are attached to.


    name String
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project String
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone String
    A reference to the zone where the instance resides.

    Outputs

    All input properties are implicitly available as output properties. Additionally, the ResourcePolicyAttachment resource produces the following output properties:

    Id string
    The provider-assigned unique ID for this managed resource.
    Id string
    The provider-assigned unique ID for this managed resource.
    id String
    The provider-assigned unique ID for this managed resource.
    id string
    The provider-assigned unique ID for this managed resource.
    id str
    The provider-assigned unique ID for this managed resource.
    id String
    The provider-assigned unique ID for this managed resource.

    Look up Existing ResourcePolicyAttachment Resource

    Get an existing ResourcePolicyAttachment resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

    public static get(name: string, id: Input<ID>, state?: ResourcePolicyAttachmentState, opts?: CustomResourceOptions): ResourcePolicyAttachment
    @staticmethod
    def get(resource_name: str,
            id: str,
            opts: Optional[ResourceOptions] = None,
            instance: Optional[str] = None,
            name: Optional[str] = None,
            project: Optional[str] = None,
            zone: Optional[str] = None) -> ResourcePolicyAttachment
    func GetResourcePolicyAttachment(ctx *Context, name string, id IDInput, state *ResourcePolicyAttachmentState, opts ...ResourceOption) (*ResourcePolicyAttachment, error)
    public static ResourcePolicyAttachment Get(string name, Input<string> id, ResourcePolicyAttachmentState? state, CustomResourceOptions? opts = null)
    public static ResourcePolicyAttachment get(String name, Output<String> id, ResourcePolicyAttachmentState state, CustomResourceOptions options)
    resources:  _:    type: gcp:compute:ResourcePolicyAttachment    get:      id: ${id}
    name
    The unique name of the resulting resource.
    id
    The unique provider ID of the resource to lookup.
    state
    Any extra arguments used during the lookup.
    opts
    A bag of options that control this resource's behavior.
    resource_name
    The unique name of the resulting resource.
    id
    The unique provider ID of the resource to lookup.
    name
    The unique name of the resulting resource.
    id
    The unique provider ID of the resource to lookup.
    state
    Any extra arguments used during the lookup.
    opts
    A bag of options that control this resource's behavior.
    name
    The unique name of the resulting resource.
    id
    The unique provider ID of the resource to lookup.
    state
    Any extra arguments used during the lookup.
    opts
    A bag of options that control this resource's behavior.
    name
    The unique name of the resulting resource.
    id
    The unique provider ID of the resource to lookup.
    state
    Any extra arguments used during the lookup.
    opts
    A bag of options that control this resource's behavior.
    The following state arguments are supported:
    Instance string
    The name of the instance in which the resource policies are attached to.


    Name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    Project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    Zone string
    A reference to the zone where the instance resides.
    Instance string
    The name of the instance in which the resource policies are attached to.


    Name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    Project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    Zone string
    A reference to the zone where the instance resides.
    instance String
    The name of the instance in which the resource policies are attached to.


    name String
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project String
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone String
    A reference to the zone where the instance resides.
    instance string
    The name of the instance in which the resource policies are attached to.


    name string
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project string
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone string
    A reference to the zone where the instance resides.
    instance str
    The name of the instance in which the resource policies are attached to.


    name str
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project str
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone str
    A reference to the zone where the instance resides.
    instance String
    The name of the instance in which the resource policies are attached to.


    name String
    The resource policy to be attached to the instance for scheduling start/stop operations. Do not specify the self link.
    project String
    The ID of the project in which the resource belongs. If it is not provided, the provider project is used.
    zone String
    A reference to the zone where the instance resides.

    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}}
    

    To learn more about importing existing cloud resources, see Importing resources.

    Package Details

    Repository
    Google Cloud (GCP) Classic pulumi/pulumi-gcp
    License
    Apache-2.0
    Notes
    This Pulumi package is based on the google-beta Terraform Provider.
    gcp logo
    Google Cloud v8.28.0 published on Tuesday, Apr 29, 2025 by Pulumi