Reputation: 23771
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
Upvotes: 8
Views: 25163
Reputation: 30498
Very late answer, but worth noting that from Terraform 1.7. The removed
block can handle this scenario.
Make sure you plan before apply to check the resources have been removed as expected.
See 'Removing Resources' in the following article: https://developer.hashicorp.com/terraform/language/resources/syntax
Upvotes: 0
Reputation: 4749
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
Upvotes: 2
Reputation: 4491
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
Upvotes: 13
Reputation: 301047
You might want to look at Terraform modules - https://www.terraform.io/docs/modules/
You can then create a set of resources by "instantiating" a module, and create another set by doing so again.
Upvotes: 1
Reputation: 1171
I don't think Terraform will work that way. One of the key stated features of Terraform is as follows.
Infrastructure as Code: Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.
If you start having multiple 'actual' resources existing from a single Terraform resource, you've lost the defined nature of your infrastructure. Is this Terraform resource... one actual resource? Five? Ten? You'd have no way to tell from reading the code.
Getting back to your question, I think you have a few options but ultimately it will still come back to defining the resources specifically. Your best option, especially if you want to cut down on boilerplate, will probably be modules.
Upvotes: 1