Let say I have route53 service in AWS redirected into some ip address.
I run terraform which redirects route53 into ALB created by that terraform.
If I run apply - everything works and route53 redirects into newly created ALB.
Then this record is destroyed if run terraform destroy command.
Can I restore previous value of record during destroy or run “aws_route53_record” to create new record during destroy command?
terraform destroy is not a command you’d generally run often. It is designed to totally remove everything back to a blank state. Instead the normal case is that you change your Terraform code to reflect what you are wanting, periodically running terraform apply to ensure AWS is in sync.
So if you are wanting to change the Route53 record you’d change the code and run apply rather than destroy.
I want to restore environment that was before running “terraform apply”
Actually , problem more common:
can I run something in “terraform destroy” which is not executed in “apply”?
It sounds like you’ve found an exception to the usual goal that a Terraform resource type will typically detect if it’s being asked to create an object that already exists and return an error, and instead for Route53 records in particular I expect the remote Route53 API doesn’t provide a way for the AWS provider to achieve that and so it’s just silently overwriting the record you already had.
Unfortunately if so then the original value is now totally gone and there is no way to recover it. As far as Terraform was concerned, it believes it just created an entirely new object and it has no knowledge of the existing object that was implicitly overwritten.
As a general rule, any object in your infrastructure should be either exclusively managed by Terraform or not managed by Terraform at all. Terraform doesn’t have any mechanisms to share ownership of an object or to remember an object that existed prior to Terraform taking any action. If you wish to retain the old value of this record then you must choose a different record name for the record that you are creating with Terraform.
Thanks.
I think would be very convenient in destroy command restore status before apply.
If object exist - do nothing, if exist- restore value.
It is simple to implement- terraform take care of the status.
Anyway , it is easy to implement manually by running some other terraform script.