Terminating an EC2 Instance using Terraform

Terminating an EC2 Instance using Terraform

Say Goodbye to Manual EC2 Instance Deletion with Terraform

ยท

3 min read

Intro ๐Ÿ

Managing your cloud infrastructure manually can be a daunting task, especially when deleting instances that are no longer needed. Manually deleting instances is not only time-consuming, but it also increases the risk of human error. This is where infrastructure as code tools like Terraform comes in handy.

In this blog post, I will show you how to use Terraform to delete AWS instances using the null_resource and local-exec provisioner.

If you don't know what AWS and Terraform are and how you can create an instance and wait until the instance is running, then check out my previous blog1 and blog2 in which I mentioned them.

Here is a video about this topic ๐Ÿš€

Set Up Your Environment ๐Ÿ 

Before I can begin, you'll need to have the following prerequisites installed on your local machine:

  • Terraform

  • AWS Account

You will also need to have an existing EC2 instance running in your AWS account.

Create a Terraform Configuration ๐Ÿ“œ

First, create a new Terraform configuration file called main.tf and write the configuration data into it.

resource "null_resource" "delete_instances" {
  triggers = {
    instance_id = "enter the instance id"
  }

  provisioner "local-exec" {
    command = "aws ec2 terminate-instances --instance-ids enter the instance id"
  }
}

I created a null_resource which is a resource that doesn't create any infrastructure in AWS. Instead, I am using it to trigger a local-exec provisioner. This provisioner allows us to execute a command on our local machine after the resource is created, which in this case is a command to terminate an EC2 instance using the AWS CLI.

The triggers block allows us to define an input for our null_resource. In this case, I've defined an input called instance_id with a default value of "enter the instance id". You'll need to replace this default value with the ID of the EC2 instance that you want to delete.

The provisioner block allows us to specify the command to run on our local machine. In this case, I am using the aws ec2 terminate-instances command to terminate the specified instance. I am passing the instance ID as an argument to the command using the ${var.instance_id} syntax.

Initialize and Apply Your Configuration ๐Ÿƒ

Next, navigate to the directory containing your Terraform configuration file and run the following commands:

terraform init
terraform apply
  • The terraform init command will download any required plugins and modules.

  • The terraform apply command will execute your Terraform configuration and delete the specified EC2 instance.

Verify the Instance Has Been Deleted ๐Ÿคฉ

After running the terraform apply command, you can verify that the EC2 instance has been deleted by logging into the AWS Management Console and checking the status of your instances.

Destroy the Terraform Configuration ๐Ÿ”ฅ

Once you have verified that the instance has been deleted, you can destroy the Terraform configuration using the following command:

terraform destroy

This will delete any resources created by your Terraform configuration, including the null_resource that you used to delete the EC2 instance.

Conclusion ๐Ÿ™‹โ€โ™€๏ธ

In this blog post, I have shown you how to use Terraform to delete an AWS EC2 instance using the AWS CLI. By using Terraform to manage your infrastructure, you can ensure that your resources are consistent, repeatable, and easy to manage.

Additionally, using a declarative language to manage infrastructure provides many benefits, such as easy collaboration, version control, and the ability to manage multiple cloud providers with a single tool.

That's it for now. Did you like this blog? Please let me know.

You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.

Happy Learning!

ย