Replies: 4 comments 5 replies
-
@vavallee This is doable but will need some hacking. We have a scripts/cleanup.sh file. You need to have installed the hcloud cli (see readme), with your current project activated. First, create a new state with Then modify the above cleanup script so that instead of deleting the resources, it runs the terraform import command for each resource, in the form of It should work! After that, please do not hesitate to PR the import script, it would be really appreciated. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@vavalleem, I asked GPT-4 for help, here's what it proposes: It looks like the name collisions are occurring because the resources already exist in Hetzner Cloud, but they haven't been imported into the Terraform state correctly. To resolve this issue, you can follow these steps: Ensure all resources are imported into the Terraform state. Double-check the modified cleanup.sh script to make sure that it imports all the necessary resources as mentioned in your previous advice. If the resources have been imported, but Terraform is still attempting to create new resources with the same names, you need to verify that the resource names in the Terraform configuration files match the names of the resources in Hetzner Cloud. You can use the hcloud CLI tool or the Hetzner Cloud Console to confirm the names of the existing resources. If the resource names in Terraform configuration files are correct and match the names of the existing resources in Hetzner Cloud, but the error persists, you can try adding the -target flag while running terraform apply. This flag allows you to selectively apply changes to specific resources:
This command will apply changes only to the specified resources, so Terraform won't attempt to recreate any other resources that might cause name collisions. If none of the above steps work, you might need to manually remove the resources from the Terraform configuration files that are causing the name collisions, import the existing resources with the correct names, and then add the resources back to the configuration files. This will ensure that Terraform recognizes the existing resources and doesn't try to create new ones with the same names. After resolving the uniqueness issue, run terraform apply again to check if everything works as expected. If Terraform still tries to recreate resources or change configurations, inspect the output of terraform plan to determine the cause and address it accordingly. |
Beta Was this translation helpful? Give feedback.
-
@vavallee @kldzj This may not be straight forward, but you could try OpenAI o1 to recreate the state for you for instance, based on import. Or give you a script that would do that. |
Beta Was this translation helpful? Give feedback.
-
I lost my tfstate file in a drive failure. Is there an easy way to import existing resources into terraform so that I can manage them again?
Beta Was this translation helpful? Give feedback.
All reactions