I want to avoid terraform to destroy the resources that I have created previously but instead create the new one.
I am/want to using the terraform like this:
But when I tried to pass the new var file, I create the new resources and delete the previous one, so I want to create the new one but still want to keep the old one as well until I'll mentioned that I want to delete it.
It's really confusing for me, can someone point me that how I can achieve it?
Thanks in advance
Advance terraform state management is tricky so be careful. I have successfully destroyed other resources while keeping some.
Usage: terraform state rm [options] ADDRESS...
The command will remove all the items matched by the addresses given.
Items removed from the Terraform state are not physically destroyed. Items removed from the Terraform state are only no longer managed by Terraform. For example, if you remove an AWS instance from the state, the AWS instance will continue running, but terraform plan will no longer see that instance.
There are various use cases for removing items from a Terraform state file. The most common is refactoring a configuration to no longer manage that resource (perhaps moving it to another Terraform configuration/state).
The state will only be saved on successful removal of all addresses. If any specific address errors for any reason (such as a syntax error), the state will not be modified at all.
This command will output a backup copy of the state prior to saving any changes. The backup cannot be disabled. Due to the destructive nature of this command, backups are required.
terraform state rm resource_to_stay
terraform plan -destroy
<- will do a "dry run" of the destroy, make sure the resource is not there
terraform destroy
terraform import resource_to_stay
terraform plan
<- this should have the output of the import resources, make sure it not going to be remove, aka show up in red.
terraform apply
That is possible to trick the Terraform state file by executing
terraform state rm 'resourcetype.resourcename' format
I had to remove the Route53 entries which were created by Terraform earlier, but now we wanted to remove it as Terraform destroys it if current state not matches.
So the terraform state rm 'aws_route53_record.nameofmyroute53record'
this updated the state file in the S3 location
And next time when we triggered terraform scripts to plan / destroy and apply the R53 entry was untouched as terraform is not aware about it.
Detailed usage can be found at https://www.terraform.io/docs/commands/state/rm.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With