Terraform
Destroy infrastructure
When you no longer need the infrastructure managed by your workspace, use
Terraform to destroy it. Terraform destroys resources when you remove them from
your configuration and apply the change. You can also destroy all of the
resources managed by your configuration with the terraform destroy
command.
Prerequisites
To follow this tutorial you will need:
- The Terraform CLI (1.2.0+) installed.
- The AWS CLI installed.
- An AWS account and associated
credentials
that allow you to create resources in the
us-west-2
region, including an EC2 instance, VPC, and security groups. - The configuration and infrastructure from the previous tutorials tutorials in this collection.
Remove a resource
Open main.tf
and comment out the aws_instance.app_server
resource block to
remove the EC2 instance from your configuration.
main.tf
/*
resource "aws_instance" "app_server" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
vpc_security_group_ids = [module.vpc.default_security_group_id]
subnet_id = module.vpc.private_subnets[0]
tags = {
Name = var.instance_name
}
}
*/
Because the instance_hostname
output value in outputs.tf
refers to the EC2
instance you removed from main.tf
, you must also comment out the output value,
or your configuration would be invalid.
outputs.tf
/*
output "instance_hostname" {
description = "Private DNS name of the EC2 instance."
value = aws_instance.app_server.private_dns
}
*/
Apply this change by running terraform apply
. Approve Terraform’s plan to
destroy your instance and remove your output value by responding yes
to the
confirmation prompt.
$ terraform apply
data.aws_ami.ubuntu: Reading...
module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-01e157ec1af2d7314]
aws_instance.app_server: Refreshing state... [id=i-0226232d8b6e9eea6]
data.aws_ami.ubuntu: Read complete after 1s [id=ami-0a605bc2ef5707a18]
module.vpc.aws_default_route_table.default[0]: Refreshing state... [id=rtb-0c1f047c07a84c278]
module.vpc.aws_default_security_group.this[0]: Refreshing state... [id=sg-04f350f66843618db]
## ...
Plan: 0 to add, 0 to change, 1 to destroy.
Changes to Outputs:
- instance_hostname = "ip-10-0-1-75.us-west-2.compute.internal" -> null
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_instance.app_server: Destroying... [id=i-0226232d8b6e9eea6]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 00m10s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 00m20s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 00m30s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 00m40s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 00m50s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 01m00s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 01m10s elapsed]
aws_instance.app_server: Still destroying... [id=i-0226232d8b6e9eea6, 01m20s elapsed]
aws_instance.app_server: Destruction complete after 1m22s
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
Destroy workspace
Removing individual resources in a workspace is a normal part of your infrastructure management workflow. Once you no longer need any of the infrastructure in your workspace, use Terraform to destroy it. For example, you may remove an application environment from service, or use Terraform to manage short-lived environments such as build or testing systems.
Destroy your workspace by running terraform destroy
. Approve Terraform’s plan
to remove your resources by responding yes
to the confirmation prompt.
$ terraform destroy
data.aws_ami.ubuntu: Reading...
module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-01e157ec1af2d7314]
data.aws_ami.ubuntu: Read complete after 1s [id=ami-0a605bc2ef5707a18]
module.vpc.aws_default_security_group.this[0]: Refreshing state... [id=sg-04f350f66843618db]
module.vpc.aws_default_route_table.default[0]: Refreshing state... [id=rtb-0c1f047c07a84c278]
module.vpc.aws_subnet.private[1]: Refreshing state... [id=subnet-0d2376b2fad1af4a6]
module.vpc.aws_subnet.private[0]: Refreshing state... [id=subnet-0d2219235033fe9d0]
## ...
Plan: 0 to add, 0 to change, 15 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
module.vpc.aws_route_table_association.private[1]: Destroying... [id=rtbassoc-0ea0b41646a73659c]
module.vpc.aws_default_security_group.this[0]: Destroying... [id=sg-04f350f66843618db]
module.vpc.aws_default_route_table.default[0]: Destroying... [id=rtb-0c1f047c07a84c278]
module.vpc.aws_route.public_internet_gateway[0]: Destroying... [id=r-rtb-0cde73c077eadf7e61080289494]
## ...
module.vpc.aws_subnet.private[1]: Destruction complete after 1s
module.vpc.aws_subnet.public[0]: Destruction complete after 1s
module.vpc.aws_subnet.private[0]: Destruction complete after 1s
module.vpc.aws_internet_gateway.this[0]: Destruction complete after 0s
module.vpc.aws_route_table.private[1]: Destruction complete after 1s
module.vpc.aws_route_table.private[0]: Destruction complete after 1s
module.vpc.aws_route_table.public[0]: Destruction complete after 1s
module.vpc.aws_vpc.this[0]: Destroying... [id=vpc-01e157ec1af2d7314]
module.vpc.aws_vpc.this[0]: Destruction complete after 1s
Destroy complete! Resources: 15 destroyed.
Now you have used Terraform to create, manage, and destroy infrastructure. In the next tutorial, you will use HCP Terraform to store your state and execute Terraform operations remotely.