# Ebs volume re-attach

**URL:** <https://discuss.hashicorp.com/t/ebs-volume-re-attach/59477>\
**Category:** AWS\
**Created:** [October 25, 2023, 12:52pm UTC](https://discuss.hashicorp.com/t/ebs-volume-re-attach/59477 "2023-10-25T12:52:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lp\_terra](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@lp\_terra](https://discuss.hashicorp.com/u/lp_terra)\
**Post date:** [October 25, 2023, 12:52pm UTC](https://discuss.hashicorp.com/t/ebs-volume-re-attach/59477/1 "2023-10-25T12:52:09Z")

</div>

Hi,

I am not sure if this is possible, I have this scenario.

1. The initial build will create ec2 and ebs volume.
2. I would like to destroy ec2, without destroying ebs volume.
3. I would like to rebuild ec2 and attach the ebs volume that was created in step 1.

Reading the documentation I could use `skip_destroy` but on the next apply it will try to create the same ebs again that is defined in step 1, which makes sense and the code to create ebs volume is in the project.

Is there a way to achieve this?

Thanks

---

<div class="post-metadata">

**Author:** ![rba1-source](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rba1-source/32/9412_2.png) [@rba1-source](https://discuss.hashicorp.com/u/rba1-source)\
**Post date:** [October 27, 2023, 11:02am UTC](https://discuss.hashicorp.com/t/ebs-volume-re-attach/59477/2 "2023-10-27T11:02:47Z")

</div>

Hi,

You can achieve this by using the aws\_volume\_attachment resource.

Essentially, create an aws\_instance resource (with no volumes declared in this other than the root volume), and separately create your aws\_ebs\_volume resource.

Then, create an aws\_volume\_attachment resource which will attach the volume to the instance.

As the volume is declared independently of the instance, destroying the EC2 instance will not destroy your EBS volume - the volume will persist.

You can test this by deploying the above configuration, then re-running your terraform apply but using a different ami\_id. Your output will show you that it destroys the “attachment” resource (which detaches the volume from the instance), then it will destroy the instance, then create the new instance, then create the new “attachment” resource (which attaches the existing volume to the new instance). The volume will persist unchanged.

Skeleton code is:

```auto
resource "aws_instance" "fileserver" {
  ami = var.ami_id
  root_block_device {
    volume_type = "gp3"
    delete_on_termination = true
  }

  depends_on = [
    aws_ebs_volume.volume
  ]
}

resource "aws_ebs_volume" "volume" {
  ...
}

resource "aws_volume_attachment" "volume" {
  instance_id = aws_instance.fileserver.id
  device_name = "xvdi"
  volume_id = aws_ebs_volume.volume.id
}

```

I added the depends\_on because you want terraform to make any changes to the volume before it makes changes to the instance, specifically if you’re using Windows OS - otherwise if you have a change to your volume, such as a size increase, terraform will make the change after the instance is launched and the new size sometimes won’t be recognised by the OS if EC2Launch v2 has already run.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![lp\_terra](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@lp\_terra](https://discuss.hashicorp.com/u/lp_terra)\
**Post date:** [November 2, 2023, 1:24am UTC](https://discuss.hashicorp.com/t/ebs-volume-re-attach/59477/3 "2023-11-02T01:24:59Z")

</div>

thanks will give that a try.
