30.Delete EC2 Instance Using Terraform

Published: (January 31, 2026 at 01:30 AM EST)
5 min read
Source: Dev.to

Source: Dev.to

Cover image for 30.Delete EC2 Instance Using Terraform

Thu Kha Kyawe

Lab Information

During the migration process, several resources were created under the AWS account. Some of these test resources are no longer needed at the moment, so we need to clean them up temporarily. One such instance is currently unused and should be deleted.

  1. Delete the EC2 instance named datacenter-ec2 present in the us-east-1 region using Terraform. Keep the provisioning code, as we might need to provision this instance again later.
  2. Before submitting your task, make sure the instance is in a terminated state.

The Terraform working directory is /home/bob/terraform.

Note: Right‑click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.

Lab Solutions

Step 1 – Check Main Terraform Configuration

# main.tf

# Provision EC2 instance
resource "aws_instance" "ec2" {
  ami           = "ami-0c101f26f147fa7fd"
  instance_type = "t2.micro"
  vpc_security_group_ids = [
    "sg-4e8e273eca9981051"
  ]

  tags = {
    Name = "datacenter-ec2"
  }
}

Step 2 – Deploy This Configuration

cd /home/bob/terraform

Initialize Terraform

terraform init

Output

bob@iac-server ~/terraform via 💠 default ➜  terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/aws versions matching "5.91.0"...
- Installing hashicorp/aws v5.91.0...
- Installed hashicorp/aws v5.91.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Destroy the configuration

terraform destroy

When prompted, type yes to confirm the destruction.

Output

bob@iac-server ~/terraform via 💠 default ➜  terraform destroy
aws_instance.ec2: Refreshing state... [id=i-5e6bb7fd83c9f0057]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.ec2 will be destroyed
  - resource "aws_instance" "ec2" {
      - ami                                  = "ami-0c101f26f147fa7fd" -> null
      - arn                                  = "arn:aws:ec2:us-east-1::instance/i-5e6bb7fd83c9f0057" -> null
      - associate_public_ip_address          = true -> null
      - availability_zone                    = "us-east-1a" -> null
      - disable_api_stop                     = false -> null
      - disable_api_termination              = false -> null
      - ebs_optimized                        = false -> null
      - get_password_data                    = false -> null
      - hibernation                          = false -> null
      - id                                   = "i-5e6bb7fd83c9f0057" -> null
      - instance_initiated_shutdown_behavior = "stop" -> null
      - instance_state                       = "running" -> null
      - instance_type                        = "t2.micro" -> null
      - ipv6_address_count                   = 0 -> null
      - ipv6_addresses                       = [] -> null
      - monitoring                           = false -> null
      - placement_partition_number           = 0 -> null
      - primary_network_interface_id         = "eni-c31c4e92a1acbff85" -> null
      - private_dns                          = "ip-10-53-157-57.ec2.internal" -> null
      - private_ip                           = "10.53.157.57" -> null
      - public_dns                           = "ec2-54-214-90-237.compute-1.amazonaws.com" -> null
      - public_ip                            = "54.214.90.237" -> null
      - secondary_private_ips                = [] -> null
      - security_groups                      = [
          - "default",
        ] -> null
      - source_dest_check                    = true -> null
      - subnet_id                            = "subnet-41298989f52a321cc" -> null
      - tags                                 = {
          - "Name" = "datacenter-ec2"
        } -> null
      - tags_all                             = {
          - "Name" = "datacenter-ec2"
        } -> null
      - tenancy                              = "default" -> null
      - user_data_replace_on_change          = false -> null
      - vpc_security_group_ids               = [
          - "sg-4e8e273eca9981051"
        ] -> null
    }

Plan: 1 to destroy, 0 to add, 0 to change.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.ec2: Destroying... [id=i-5e6bb7fd83c9f0057]
aws_instance.ec2: Destruction complete after 1s

Destroy complete! Resources: 1 destroyed.

The EC2 instance datacenter-ec2 is now terminated, and the Terraform configuration files remain untouched for future provisioning.

  = [
    - "sg-4e8e273eca9981051",
  ] -> null
  # (8 unchanged attributes hidden)

  - metadata_options {
      - http_endpoint               = "enabled" -> null
      - http_protocol_ipv6          = "disabled" -> null
      - http_put_response_hop_limit = 1 -> null
      - http_tokens                 = "optional" -> null
      - instance_metadata_tags      = "disabled" -> null
    }

  - root_block_device {
      - delete_on_termination = true -> null
      - device_name           = "/dev/sda1" -> null
      - encrypted             = false -> null
      - iops                  = 0 -> null
      - tags                  = {} -> null
      - tags_all              = {} -> null
      - throughput            = 0 -> null
      - volume_id             = "vol-aecbbe533be995fe4" -> null
      - volume_size           = 8 -> null
      - volume_type           = "gp2" -> null
        # (1 unchanged attribute hidden)
    }
}

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value: yes
aws_instance.ec2: Destroying... [id=i-5e6bb7fd83c9f0057]
aws_instance.ec2: Still destroying... [id=i-5e6bb7fd83c9f0057, 10s elapsed]
aws_instance.ec2: Destruction complete after 10s

Destroy complete! Resources: 1 destroyed.

Resources & Next Steps

Credits

  • All labs are from: KodeKloud
  • I sincerely appreciate your provision of these valuable resources.
Back to Blog

Related posts

Read more »

34.Copy Data to S3 Using Terraform

Lab Information The Nautilus DevOps team is currently performing data migrations, moving data from on‑premise storage systems to AWS S3 buckets. They have rece...