首页 > 解决方案 > Terraform Destroy Command 显示不正确的计划

问题描述

我是 Terraform 的新手,现在正在学习。我使用有效的 terraform 代码创建了一个 aws 实例(我有测试环境)。我用“”清除了同一个实例terraform destroy,它成功了。现在,当我尝试创建新实例时,“ terraform plan”显示要添加的 2 个资源而不是 1 个。下面是我的计划输出。

C:\terraform>terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions: 

 + aws_instance.example
      id:                           <computed>
      ami:                          "ami-051f75c651d856381"
      arn:                          <computed>
      associate_public_ip_address:  <computed>
      availability_zone:            <computed>
      cpu_core_count:               <computed>
      cpu_threads_per_core:         <computed>
      ebs_block_device.#:           <computed>
      ephemeral_block_device.#:     <computed>
      get_password_data:            "false"
      host_id:                      <computed>
      instance_state:               <computed>
      instance_type:                "t2.micro"
      ipv6_address_count:           <computed>
      ipv6_addresses.#:             <computed>
      key_name:                     <computed>
      network_interface.#:          <computed>
      network_interface_id:         <computed>
      password_data:                <computed>
      placement_group:              <computed>
      primary_network_interface_id: <computed>
      private_dns:                  <computed>
      private_ip:                   <computed>
      public_dns:                   <computed>
      public_ip:                    <computed>
      root_block_device.#:          <computed>
      security_groups.#:            <computed>
      source_dest_check:            "true"
      subnet_id:                    <computed>
      tenancy:                      <computed>
      volume_tags.%:                <computed>
      vpc_security_group_ids.#:     <computed>
  + aws_key_pair.deployer
      id:                           <computed>
      fingerprint:                  <computed>
      key_name:                     "key-pair"
      public_key:                   "XXX"

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

标签: amazon-web-servicesterraformterraform-provider-aws

解决方案


您的 Terraform 计划显示它将添加 2 个资源:一个aws_instance和一个aws_key_pair. 一个aws_key_pair允许您控制对您的 EC2 实例的登录访问。

这是因为当您运行时terraform plan,Terraform会查看当前目录中的所有.tf文件并尝试创建它找到的所有资源。您可以通过从当前目录中删除来解决此问题aws_key_pair.tf,这使得它terraform plan只能找到aws_instance要创建的目录。

有关更多详细信息,请参阅terraform 计划文档:

默认情况下,plan 不需要标志,并在当前目录中查找要刷新的配置和状态文件。


推荐阅读