How can you prevent certain resources from being destroyed in Terraform?

Master the HashiCorp Terraform Test with these flashcards and multiple choice questions. Each question includes hints and explanations to prepare you effectively. Set yourself up for success!

To prevent certain resources from being destroyed in Terraform, using the 'prevent_destroy' attribute within the resource block is the effective approach. When this attribute is set to true in the lifecycle configuration of a resource, it instructs Terraform to disallow the destruction of that resource, even if a destroy action is requested. This is particularly useful for critical resources that should not be deleted accidentally or without careful consideration, thus providing an added layer of protection against unintended deletion.

For instance, in a typical use case, if you have a resource that manages essential data or services, applying 'prevent_destroy' ensures that even if you run terraform destroy or if Terraform determines during a plan that the resource should be removed, it will raise an error instead of proceeding with the destruction. This makes it a powerful safeguard for maintaining important reconfiguration of your infrastructure.

The other methods listed do not directly address the prevention of resource destruction as effectively as the 'prevent_destroy' attribute. For instance, 'ignore_changes' does not prevent destruction; it merely tells Terraform to ignore changes to specific arguments of a resource, which can be useful in different scenarios but not for preventing deletion. Setting resource dependencies ensures correct order of operations but does not prevent a resource from being destroyed. Deleting

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy