Add multiple block volumes to multiple VM instances nd be able to scale in terms of VM instances and block volumes

I’m new to terraform and have been using this blog to help with some of my coding,
currently I’m trying to create multiple VM instances, block volumes and attaching them to instance(s).
While I’m able to do so, the current code does not allow scaling up or down in terms of both VM instances or block volumes.
So for ex. if 2 VM’s are created initially with 2 block volumes attached to each of them, I need the code to handle if a new block volume is created,
also if it is removed only the specific one should be removed, same goes with the instances.

So far I have written below code, it works fine for scaling up or down volumes, however when I change the no_of_instances value,
it re-builts the VM to block association which messes up the attachments.
I’m not sure how everyone else is handling this situation and if this is the right way to handle attachments, I’m trying to be careful here since this is very critical
and a destruction of wrong volume can result in data loss.

variable “vol_size” {
description = “Size of Disks”
type = list
default = [

variable “no_of_instances” {
description = “Number of VMs”
default = “2”

creates 4 volumes

resource “oci_core_volume” “volume_os” {
count = length(var.vol_size) * var.no_of_instances
availability_domain = “AD-1”
compartment_id = data.oci_identity_compartments.test_compartment.compartments[0][“id”]
display_name = Volume${format("%01d", count.index + 1)}"
size_in_gbs = element(var.vol_size, count.index)

resource “oci_core_volume_attachment” “volume_attachment1” {
count = var.no_of_instances * length(var.vol_size)
attachment_type = var.volume_attachment_type
is_pv_encryption_in_transit_enabled = “false”
use_chap = “false”
instance_id = oci_core_instance.instance[count.index % var.no_of_instances].id
volume_id = oci_core_volume.volume_os[count.index].id