Detach some ebs volume from EC2 Instance (aws)

Hi,
I need to detach a volume from ec2 instance, in the documentation I only find aws_volume_attachment. Does exist something like aws_volume_deattachment ?

Just run the plan without the aws_volume_attachment and Terraform will remove the volume.

Terraform makes all the necessary changes to have the same infrastructure in code as in AWS. Anything missing in AWS will be created and any thing missing in code will be removed from AWS.

Hi,
I tried and it doesn’t work, when I run the plan I get as output: “Plan: 1 to add, 0 to change, 0 to destroy.”

You can only delete resources that were created with Terraform or that have been imported from the IaaS into the Terraform state file.

Did you attached the volume using Terraform? Is the volume still attached? Did you create the EC2 instance using Terraform?

Maybe you can share the code and the execution result.

I have two files, my main where I use to create my instance and have other file I specify my Ec2 resources. I will let the code below.

resource “aws_instance” “web” {
ami = “{var.ami_id}" instance_type = "{var.instance_type}”
availability_zone = “{var.availability_zoneEc2}" subnet_id = "{var.subnet_id}”
security_groups = ["${aws_security_group.allow_ssh.name}"]

tags = {
Name = “terraformEc2”
}
}

resource “aws_ebs_volume” “example” {
availability_zone = “{var.availability_zoneEbs}" size = "{var.size}”
tags = {
Name = “terraformEBS”
}
}

resource “aws_volume_attachment” “ebs_att” {
device_name = “/dev/sdh”
volume_id = “{aws_ebs_volume.example.id}" instance_id = "{aws_instance.web.id}”
}

resource “aws_security_group” “allow_ssh” {
name = “allow_ssh”
ingress {
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = [“0.0.0.0/0”]
}
}