To destroy a specific EC2 instance resource using Terraform, you can use the terraform destroy -target command followed by the resource identifier.

Here are the steps to destroy a specific EC2 instance resource using Terraform:

1. Locate the Resource Identifier: The resource identifier is the way you named your resource in your Terraform configuration file. or example, if your configuration file includes a resource defined as: resource “aws_instance” “ec3” {

ami = “ami-0c94855ba95c71c99”

instance_type = “t3.micro”

}

The resource identifier for this EC2 instance is “aws_instance.ec3”.

2. Initialize Terraform: If you haven't already, navigate to the folder containing your  .tf file and run the command: terraform init

Destroy Specific Resource: Use the “terraform destroy -target” command followed by the resource identifier: terraform destroy -target=aws_instance.ec3 Replace aws_instance.ec3 with your resource identifier. Terraform will then destroy only the specified resource.

3. Confirm Destruction: You will be prompted to confirm that you want to destroy the resource. Type yes and press Enter.

Terraform will then destroy the specified resource. Make sure to check the output to ensure that the resource was destroyed successfully.

You can see in AWS console that the instance has terminated.

Note that the terraform destroy -target command will destroy only the specified resource and any dependent resources. Other resources in your configuration file will not be affected. Be cautious when using this command, as it can lead to an incomplete or inconsistent state. It is recommended to use this command only in exceptional situations.

Note: Please keep in your mind while entering code of “terraform destroy” If you are not mentioned the target for specific resource. It will destroy all the Resources.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *