'Changed an ebs volume using Terraform, but seeing the original disk in virtual machine

I attached a new volume (from a snapshot) to /dev/sda2 using Terraform's aws_ebs_volume and aws_volume_attachment (as below). I see on AWS that it is a new volume from the right snapshot. But disk D in a virtual machine remains the same, and it makes all the following tasks with the contents of the disk totally wrong.

I tried the same workflow on different instances with different snapshots, and sometimes it does change, and most of the times it doesn't.

    resource "aws_ebs_volume" "ebs" {
    lifecycle {
      ignore_changes = [
        tags["mp_primary"],
        "size"
      ]
  }

  availability_zone = aws_instance.vm.availability_zone
  snapshot_id       = var.snapshot_id
  type              = "gp3"
  size              = 50
  encrypted         = true
  kms_key_id        = data.aws_kms_key.ebs_kms_key_arn.arn

  tags = merge(
    local.tags,
    {
      auto_snapshot = contains(["prod"], var.env_type)
      os_type       = "windows"
      mp_primary = "false"
    }
  )
}

resource "aws_volume_attachment" "ebs_attachment" {
  device_name  = "/dev/sda2"
  volume_id    = aws_ebs_volume.ebs.id
  instance_id  = aws_instance.vm.id
  force_detach = true
}

What can be done to make sure I can use the newly attached disk D after it was attached by Terraform and not the old one?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source