Trying to edit a disk on one VM only using count option

Hello All ,

I am actually trying to edit only one disk defined on the count which has three VMs created
solr = {
count = 3
start_index = 35
size = “Standard_EC16as_v5”
os_disk = {
caching = “ReadWrite”
storage_type = “StandardSSD_LRS”
size = count.index == 1 ? 1500 : 1000
}
subnet_name = “snet-p-weu-egs-private”
adRegistration = true
adAdminGroup = “GBL_ADM_AZU_INFRAOPS”
custom_script_path = “scripts/install-custom.sh”
enable_auto_shutdown = false
backup_policy = “Silver-Enhanced”
custom_tags = {
AppRole = “solr”
}
}
}

I just want to edit the disk for 36 VM count.index[1]

Could you please suggest how can it be achieved

Hi @bhagawath098,

What exactly do you mean by this?

To help further, can you provide the complete resource block for the managed azure resource, as the code snippet you have provided seems to either be an extract of an attribute from a resource block or something like a local variable or input variable so is incomplete in context upon which the answer will depend.

Additionally, when you are posting the code please format it as a code block using 3xback-ticks ``` on the line before and the line after the code. This will format the code,

like this

preserving formatting and preventing the introduction of special characters. It will also make it easier for others to read and to copy/paste to reproduce elements if needed.

Happy Terraforming

Hello @ExtelligenceIT Thanks for your responce .

count               = 3
    start_index         = 35
    size                = "Standard_EC16as_v5"
    os_disk             = {
      caching       = "ReadWrite"
      storage_type  = "StandardSSD_LRS"
      size          = 1000

Since the tfvars defined has variable using count function to create 3 VMs
And is hardcoded to use 1000 GB as dfeault for all 3
I just want to modify the disk for middle one count.index value 2 , that is 36 as start index is set to 35
How can i acheive it
I have tried below
size = count.index == 1 ? 1500 : 1000
But since the acceptable value is only a number defined in the map of object variables on variables , need a way to get the value paased to size conditionally

    os_disk               = optional(object({
      caching       = optional(string, "ReadWrite")
      storage_type  = optional(string, "Standard_LRS")
      size          = optional(number)
    }),

Appreciate your help on this.!!!

Again, I am not clear on what you mean by the following as you have not provided broader context with regards to the resource block (or module) that this is feeding into:

On the face of it, and based upon your use of count (presuming it is being used on the resource/module attribute as opposed to within a .tfvars or similar), then the way you are applying the ternary operator appears correct, but is obviously not working as expected in your situation due to the broader code in the module.

The first thing I would say is that count is a good way to produce multiple identical instances of the same resource, but not so flexible when it comes to producing multiple instances of the resource that may vary based upon different input variables and criteria. Count can also have broader impacts when removing / adding anything except at the last index so typically it is only used if there is no better approach.

Therefore, my first suggestion would be to consider refactoring your code to use the for_each meta argument. You can still set the defaults in the input variable attributes as you have but would pass in a map-of-objects. Assuming the input variable and defaults are set as follows (Note this is a contrived example based upon what you have provided):

vms = map(object({
  size = optional(string, "Standard_EC16as_v5")
  os_disk           = optional(object({
      caching       = optional(string, "ReadWrite")
      storage_type  = optional(string, "Standard_LRS")
      size          = optional(number,1000)
    })
  }
})

Then your input variable could be:

vms=[
   vm1 = {}, # Uses all default vaules
   vm2 = {
      os_disk = {
         size = 1500 # Uses all default values except this attribute
   },
   vm3 = {} # Uses all default values.
]

This approach also allows flexibility to alter other attributes in other instances of the same resource in the future.

Another possible (but less flexible and likely more brittle) would be to change the size attribute to be a list: size = list(number) and then reference the index of that list. Passing in a list of numbers that correspond to the required disk sizes [1000,1500,1000].

Thanks a lot @ExtelligenceIT let me check and get back in case of any issues