Error Azure VM was not found while running Terraform through Jenkins pipeline


I had a linux VM in Azure cloud, which was showing error in the Azure portal that “VM agent is not ready”, which was created using the Terraform file and Jenkins pipeline. So, I decided to delete the VM through Terraform, so I changed the count to 0 in terraform file and ran the Jenkins pipeline but received an error that “VM agent is not ready” can’t delete it, so I decided to delete the VM directly from the Azure portal and did it.

After this I again ran the Pipeline and received an error that VM is not found, I understand that VM is deleted, its general sense, but my Jenkins pipeline will not run cause I am getting this error, and this is stopping me from building other servers which are configured in the terraform file.

So, is there any way we can change any configuration in the terraform state file,so it gets to know that, yeah VM is deleted, I don’t have to look for it.


It sounds like you might want to use terraform state rm


Thanks @stuart-c

I have analysed your response but its not possible to run, cause I have multiple modules present in a to build a server and all of it is using a single groovy file encoded with terraform commands and everything is automated, I just have to change the file and run the pipeline and servers are built or destroyed.

I have attached the exact error I am facing, it would be helpful, if anyone can help.

Error: Error refreshing state: 1 error occurred:
*module.app15_murex_db_001.azurerm_virtual_machine_data_disk_attachment.app_db_disk_attachment: Virtual Machine () was not found

There are situations that you may need to do manual changes to your state file. For example importing existing resources or removing resources that were destroyed outside of Terraform.

The error is saying that Terraform is trying to fetch information about the VM with the ID you highlighted, but it is not found. You either need to have that VM exist (which is probably not possible as the ID will be randomly generated) or remote the entry from your state file (using terraform state rm, probably from your computer rather than via CI).

Thank you so much for you help.