1. Handling non-existent GCP VM during Pulumi state deletion?


    In Pulumi, when a resource that was created by Pulumi and part of its state is manually deleted (such as a Google Cloud VM), it can cause issues when running pulumi destroy or pulumi up. You might see an error like: google-native:compute/v1:Instance 'instance-x' resource not found.

    To resolve this, you can use the ignoreChanges and lifecycle properties in your resource definition. The ignoreChanges property allows you to specify parts of the resource that Pulumi should ignore when detecting changes, and lifecycle's protect property, when set to true, safeguards the resource from accidental deletion.

    Here is an example where I define a Google Cloud VM instance:

    import * as pulumi from "@pulumi/pulumi"; import * as gcp from "@pulumi/gcp"; const instance = new gcp.compute.Instance("instance", { machineType: "e2-medium", bootDisk: { initializeParams: { imageFamily: "ubuntu-1804-lts", imageProject: "gce-uefi-images", }, }, networkInterfaces: [{ network: "default", }], lifecyclePolicy: { ignoreChanges: ["all"], protect: true, }, });

    In the code above, the ignoreChanges property is set to all, which means all changes to this resource will be ignored, and the protect property is set to true to prevent accidental deletion.

    If a VM has been deleted manually, and you just want to get your Pulumi stack back to a clean state, you can remove references to the resource in your Pulumi code and then run pulumi refresh, which will align the Pulumi state with the actual state of the infrastructure.

    Please note that the lifecyclePolicy and ignoreChanges parameters have a purpose of protecting infrastructure from accidental changes or deletion but they won't recreate an already deleted resource.

    Your final program may look something like:

    import * as pulumi from "@pulumi/pulumi"; import * as gcp from "@pulumi/gcp"; // Create a GCP resource (Storage Bucket) const mybucket = new gcp.storage.Bucket("my-bucket"); // Export the bucket selfLink export const bucketSelfLink = mybucket.selfLink;

    This program creates a Google Cloud Storage bucket and exports the selfLink. In this revised program, the VM instance resource is removed, so a pulumi up or pulumi refresh won't fail. Please make sure the remaining resources in the program are intact in your GCP project.

    Find more details for GCP resource in Pulumi from here.

    Remember to replace "my-bucket" with an appropriate globally unique name for your bucket. If you remove the bucket, please also remove the exported bucketSelfLink line.