Answers: <p>This may be what you are doing already (in which case this sounds like a bug), yet it seems just based on the limited information in the question that you may be forgetting a step: Run <code>plan -destroy -target=... -target=...</code> first.  The <code>terraform destroy</code> command will try to operate on changes listed in the <code>.plan</code> file.</p>
  
  <p>The following example assumes a Terraform module named <code>foo</code>, with 3 EBS volumes and attachments named as: <code>dev-sdb</code>, <code>dev-sdf</code>, <code>dev-sdg</code>.  All resources are assumed to have <code>count = local.enabled ? 1 : 0</code>.  So this is the more advanced case you may encounter where each resource would have to be targeted with the array syntax <code>[0]</code>.</p>
  
  <ol>
  <li><p>First create a targeted plan file with <code>terraform plan -destroy -target=...</code>. 
  Do this before trying to run <code>terraform destroy</code></p>
  
  <pre><code>terraform plan -destroy \
                 -target=module.foo.aws_volume_attachment.dev-sdb-attachments[0] \
                 -target=module.foo.aws_volume_attachment.dev-sdf-attachments[0] \
                 -target=module.foo.aws_volume_attachment.dev-sdg-attachments[0] \
                 -target=module.foo.aws_instance.foo[0]  \
                 -target=module.foo.aws_iam_instance_profile.foo[0] \
                 -out=intermediate.plan
  </code></pre></li>
  <li><p>Run <code>terraform destroy</code>, optionally also using <code>-target=...</code> for safety precautions if you wish:</p>
  
  <pre><code>terraform destroy -target=module.foo.aws_volume_attachment.dev-sdb-attachments[0] \
                    -target=module.foo.aws_volume_attachment.dev-sdf-attachments[0] \
                    -target=module.foo.aws_volume_attachment.dev-sdg-attachments[0] \
                    -target=module.foo.aws_instance.foo[0]  \
                    -target=module.foo.aws_iam_instance_profile.foo[0] \
                    intermediate.plan
  </code></pre></li>
  </ol>
  
  <p>If you need this to be unattended for automation, add <code>-auto-approve</code> flag to the <code>terraform destroy</code> command.  Otherwise, it assumes an interactive terminal session and will prompt before destroying any resources.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-20T22:24:38.287Z
Answers: 2020-03-20T22:24:38.287Z
Answers: TrinitronX
Answers: <p>A workaround to your issue is to create a snapshot of the volume(s) before deleting them.  The snapshot will live on after the volume is deleted and can be recreated if necessary.</p>
  
  <blockquote>
    <p>After you no longer need an Amazon EBS volume, you can delete it. After deletion, its data is gone and the volume can't be attached to any instance. However, before deletion, you can store a snapshot of the volume, which you can use to re-create the volume later.</p>
  </blockquote>
  
  <p><strong>Reference</strong></p>
  
  <p><a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-deleting-volume.html" rel="nofollow noreferrer">EBS Deleting Volume</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-01T17:48:30.780Z
Answers: That is more or less what I ended up doing before reaching out to stackexchange, but this is an involved workaround at best.
Answers: cueedee
Answers: CC BY-SA 4.0
Answers: 2020-02-28T13:24:42.377Z
Answers: 2020-02-28T13:24:42.377Z
Answers: kenlukas
Body: <pre><code>$ terraform -version
  </code></pre>
  
  <blockquote>
  <pre><code>Terraform v0.12.21
  + provider.aws v2.34.0
  </code></pre>
  </blockquote>
  
  <p>Given a terraform configuration for one set of single <code>aws_instance</code>, <code>aws_ebs_volume</code> and <code>aws_volume_attachment</code> resources, each named, say,<code>foo</code>;</p>
  
  <p>How would one <code>terraform destroy -target=aws_instance.foo</code> <em>without</em> terraform also destroying the <code>aws_ebs_volume.foo</code> as the resulting plan insists that it will?</p>
  
  <ul>
  <li>The aws console reports <em><code>Delete on termination</code></em> for the volume's block device to be <strong><code>false</code></strong>;</li>
  <li>I've tried to <code>terraform destroy -target=aws_volume_attachement.foo</code> first;<br>
  ... no-change.</li>
  <li>I've tried removing the <code>aws_volume_attachment</code> from the configuration, <code>terraform apply</code> it and <em>then</em> <code>terraform destroy -target=aws_instance.foo</code>;<br>
  ... didn't help either</li>
  </ul>
  
  <p>Somehow terraform really wants to destroy the <code>aws_ebs_volume</code> resource together  with the <code>aws_instance</code> resource. </p>
  
  <p>I, otoh, would very much like to preserve that <code>aws_ebs_volume</code> for later attachment to a re-created <code>aws_instance</code>...</p>
  
  <p>Can it be done?</p>
  
Tags: <amazon-web-services><terraform>
Title: How to `terraform destroy` an `aws_instance` without also destroying its `aws_ebs_volume` (separate resource)
UserDisplayName: cueedee
ThreadText: # How to `terraform destroy` an `aws_instance` without also destroying its `aws_ebs_volume` (separate resource)
  
  (Asked by: username_0 on 2020-02-25)
  
  ```
  $ terraform -version
  
  ```
  
  > ```
  > Terraform v0.12.21
  > + provider.aws v2.34.0
  >
  > ```
  
  Given a terraform configuration for one set of single `aws_instance`, `aws_ebs_volume` and `aws_volume_attachment` resources, each named, say,`foo`;
  
  How would one `terraform destroy -target=aws_instance.foo` *without* terraform also destroying the `aws_ebs_volume.foo` as the resulting plan insists that it will?
  
  * The aws console reports *`Delete on termination`* for the volume's block device to be **`false`**;
  * I've tried to `terraform destroy -target=aws_volume_attachement.foo` first;  
    ... no-change.
  * I've tried removing the `aws_volume_attachment` from the configuration, `terraform apply` it and *then* `terraform destroy -target=aws_instance.foo`;  
    ... didn't help either
  
  Somehow terraform really wants to destroy the `aws_ebs_volume` resource together with the `aws_instance` resource.
  
  I, otoh, would very much like to preserve that `aws_ebs_volume` for later attachment to a re-created `aws_instance`...
  
  Can it be done?
  
  **Tags:** amazon-web-services, terraform
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-03-20. Score: 2
  
  This may be what you are doing already (in which case this sounds like a bug), yet it seems just based on the limited information in the question that you may be forgetting a step: Run `plan -destroy -target=... -target=...` first. The `terraform destroy` command will try to operate on changes listed in the `.plan` file.
  
  The following example assumes a Terraform module named `foo`, with 3 EBS volumes and attachments named as: `dev-sdb`, `dev-sdf`, `dev-sdg`. All resources are assumed to have `count = local.enabled ? 1 : 0`. So this is the more advanced case you may encounter where each resource would have to be targeted with the array syntax `[0]`.
  
  1. First create a targeted plan file with `terraform plan -destroy -target=...`.
     Do this before trying to run `terraform destroy`
  
     ```
     terraform plan -destroy \
                    -target=module.foo.aws_volume_attachment.dev-sdb-attachments[0] \
                    -target=module.foo.aws_volume_attachment.dev-sdf-attachments[0] \
                    -target=module.foo.aws_volume_attachment.dev-sdg-attachments[0] \
                    -target=module.foo.aws_instance.foo[0]  \
                    -target=module.foo.aws_iam_instance_profile.foo[0] \
                    -out=intermediate.plan
  
     ```
  2. Run `terraform destroy`, optionally also using `-target=...` for safety precautions if you wish:
  
     ```
     terraform destroy -target=module.foo.aws_volume_attachment.dev-sdb-attachments[0] \
                       -target=module.foo.aws_volume_attachment.dev-sdf-attachments[0] \
                       -target=module.foo.aws_volume_attachment.dev-sdg-attachments[0] \
                       -target=module.foo.aws_instance.foo[0]  \
                       -target=module.foo.aws_iam_instance_profile.foo[0] \
                       intermediate.plan
  
     ```
  
  If you need this to be unattended for automation, add `-auto-approve` flag to the `terraform destroy` command. Otherwise, it assumes an interactive terminal session and will prompt before destroying any resources.
  
  
  ---
  
  ### Answer by username_2 on 2020-02-28. Score: 0
  
  A workaround to your issue is to create a snapshot of the volume(s) before deleting them. The snapshot will live on after the volume is deleted and can be recreated if necessary.
  
  > After you no longer need an Amazon EBS volume, you can delete it. After deletion, its data is gone and the volume can't be attached to any instance. However, before deletion, you can store a snapshot of the volume, which you can use to re-create the volume later.
  
  **Reference**
  
  [EBS Deleting Volume](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-deleting-volume.html)
  
  > **Comments:**
  > **username_0**: That is more or less what I ended up doing before reaching out to stackexchange, but this is an involved workaround at best.  
  
  ---
  
  