30. 使用 Terraform 删除 EC2 实例

发布: (2026年1月31日 GMT+8 14:30)
6 min read
原文: Dev.to

Source: Dev.to

30.Delete EC2 Instance Using Terraform 的封面图片

Thu Kha Kyawe

实验信息

在迁移过程中,已在 AWS 账户下创建了多个资源。其中一些测试资源目前已不再需要,因此我们需要暂时清理它们。当前有一个实例未被使用,应该将其删除。

  1. 使用 Terraform 删除位于 us-east-1 区域、名称为 datacenter-ec2 的 EC2 实例。保留其配置代码,因为以后可能需要重新创建该实例。
  2. 在提交任务之前,确保该实例已处于 terminated(已终止)状态。

Terraform 工作目录为 /home/bob/terraform

注意: 在 VS Code 的 EXPLORER 区域右键单击,选择 Open in Integrated Terminal 以打开终端。

实验解决方案

第 1 步 – 检查主 Terraform 配置

# 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"
  }
}

第 2 步 – 部署此配置

导航到 Terraform 目录

cd /home/bob/terraform

初始化 Terraform

terraform init

输出

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.

销毁配置

terraform destroy

当出现提示时,输入 yes 以确认销毁。

输出

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.

EC2 实例 datacenter-ec2 现已终止,Terraform 配置文件保持不变,可用于将来的部署。

  = [
    - "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.

资源与后续步骤

致谢

  • 所有实验均来自: KodeKloud
  • 我衷心感谢您提供这些宝贵的资源。
Back to Blog

相关文章

阅读更多 »