Terraform Destroy: It’s Easy!

Riddhi Shree
4 min readDec 21, 2020

--

https://olohmann.github.io/azure-hands-on-labs/labs/07_iac/terraform.html

Whenever I use AWS services, my biggest concern after I am done using the services has always been —

Did I destroy all the resources properly, or, is there anything still left behind?

This was one of the main reasons why I got attracted towards Terraform. Terraform allows us to build and destroy (neatly) the entire infrastructure using just four simple commands —

terraform init

terraform plan -out planfile

terraform apply planfile

terraform destroy

While working with Terraform, there are two important things that need to be taken care of:

  1. All script files must end with the extension .tf
  2. Resource blocks must be used to describe one or more infrastructure objects

The syntax to create a resource is —

resource “<PROVIDER>_<TYPE>” “<NAME>” {[Configuration…]}

For example, if we want to create an EC2 instance, the corresponding resource block would be something similar to following —

AWS Instance Resource Block

In order to make the above code functional, a few more resource blocks would be required to be created for security group, IAM instance profile, IAM role inline policy, VPC, VPC subnet, VPC Internet Gateway, or for creating an association between a route table and a subnet, or, a route table and an internet gateway or virtual private gateway.

Security Group
IAM Instance Profile and IAM Role Policy
VPC Resource Block
VPC Subnet
VPC Internet Gateway
Resource to create an association between a route table and a subnet

Variables can be defined inside a variable block in two ways —

  1. With a default value
  2. Without a default value
Define variables with or without a default value

If a default value has not been provided for a variable, then the user will be prompted to enter its value at runtime.

Once defined, the variables can be referenced inside a resource block or a provider block by using the var keyword.

Access variables

When you feel the script is ready, or if you just want to test your script, get ready to run a few commands.

  1. Initialize the working directory by running terraform init command.
  2. Run terraform plan -out planfile to create an execution plan and save it in a file. After the command is run, any error in the Terraform script would be highlighted on the command line output.
  3. When you are happy with the generated plan, apply these changes by running the command terraform apply planfile
  4. Whenever you wish to tear down the infrastructure that was created by Terraform, without leaving behind any debris, run the command terraform destroy
Highlighted Error Messages
$ terraform plan -out planfile
$ terraform apply planfile
$ terraform destroy
The End!

References

Before I sign off, I am sharing below a few references that helped me in getting started with Terraform —

Troubleshooting

--

--

Riddhi Shree
Riddhi Shree

Written by Riddhi Shree

Inquisitive by nature, I tend to drift towards things that are seemingly difficult, yet, interesting.

No responses yet