Handling non-existent GCP VM during Pulumi state deletion?TypeScript
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 up. You might see an error like:
google-native:compute/v1:Instance 'instance-x' resource not found.
To resolve this, you can use the
lifecycleproperties in your resource definition. The
ignoreChangesproperty allows you to specify parts of the resource that Pulumi should ignore when detecting changes, and lifecycle's
protectproperty, when set to
true, safeguards the resource from accidental deletion.
Here is an example where I define a Google Cloud VM instance:
In the code above, the
ignoreChangesproperty is set to
all, which means all changes to this resource will be ignored, and the
protectproperty is set to
trueto 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
ignoreChangesparameters 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:
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 refreshwon'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