Terraform: Feature Request: [Destroy|Plan|Apply] by specifying the file

Created on 8 Nov 2016  ยท  4Comments  ยท  Source: hashicorp/terraform

Terraform Version

MisaKondo-no-iMac% terraform -v
Terraform v0.7.7

Affected Resource(s)

No specific resource. This issue with Terraform's core.

Terraform Configuration Files

resource "google_compute_instance" "development" {
  name         = "development"
  machine_type = "n1-standard-1"
  zone         = "us-east1-d"
  description  = "mass-remote"
  tags         = ["development", "mass"]

  disk {
    image = "ubuntu-os-cloud/ubuntu-1404-lts"
  }

  // Local SSD disk
  disk {
    type        = "local-ssd"
    scratch     = true
    auto_delete = true
  }

  network_interface {
    subnetwork = "development"
  }

  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro", "bigquery", "monitoring"]
  }

  scheduling {
    on_host_maintenance = "MIGRATE"
    automatic_restart   = true
  }
}

Debug Output

MisaKondo-no-iMac% terraform destroy Terraform/gcp_instances.tf
Error loading config: configuration path must be a directory: Terraform/gcp_instances.tf
MisaKondo-no-iMac% git rm Terraform/gcp_instances.tf
rm 'Terraform/gcp_instances.tf'
MisaKondo-no-iMac% terraform plan Terraform
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.

google_compute_instance.development: Refreshing state... (ID: development)
google_compute_network.remote: Refreshing state... (ID: remote)
google_compute_subnetwork.development: Refreshing state... (ID: us-east1/development)

The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.

Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.

- google_compute_instance.development


Plan: 0 to add, 0 to change, 1 to destroy.
MisaKondo-no-iMac% git commit -m 'remove instance setting(for destory'

Expected Behavior

I expected, terraform destroy #{target_file} is destroy #{target_file} resource.

Actual Behavior

But, destroy|plan|apply COMMAND no support file. Only directory.
I want to delete specific file resource.

Steps to Reproduce

  1. git clone -b set_gcp_environments [email protected]:MisaKondo/remote_machine.git && cd remote_machine
  2. terraform apply Terraform
  3. terraform destroy Terraform/gcp_instances.tf
  4. rm Terraform/gcp_instances.tf
  5. Terraform apply Terraform

References

Most helpful comment

@apparentlymart
Thank you for your reply!

-target option is good.
When multi resources are testing, If erasable by specifying the file, I thought it would be convenient.
However, how to delete the specified resource was understanding.

All 4 comments

Hi @MisaKondo,

Terraform doesn't support exactly what you expected here, but you can get a similar result using the -target argument:

terraform plan -destroy -target=google_compute_instance.development

Note that targets are resource-oriented rather than file-oriented. Terraform generally doesn't operate on individual files, but rather just treats an entire directory as a single configuration.

The -target option lets you focus operations on particular resources _and their dependencies_: in the above example, if another resource were depending on the google_compute_instance.development resource then it too would be targeted for destruction.

@apparentlymart
Thank you for your reply!

-target option is good.
When multi resources are testing, If erasable by specifying the file, I thought it would be convenient.
However, how to delete the specified resource was understanding.

terraform state list is a great way to see all the modules that are available as targets for terraform plan -target=module.name.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings