Using Terraform to destroy unused resources
Although Terraform’s primary purpose is to create infrastructure, it can also be used to delete infrastructure as well. This function is known as a Terraform destroy. With destroy
, Terraform will attempt to remove all infrastructure that’s defined in your configuration file. At this point, our configuration file creates an EC2 instance, as well as a security group. If we run destroy
against it, then both resources will be removed.
Removing infrastructure with Terraform will likely be a use case you won’t utilize as often as creating resources. One of the values of the destroy
functionality, though, is that you can use it to “reset” a test environment, by removing everything defined in the file. Then you’re free to use the same script to create everything again. On my end, I learn a lot faster by breaking things and fixing them repeatedly. You really shouldn’t run a destroy
job against...