Data block not reading latest image version

I am creating VM using Ubuntu based custom image from Azure Compute Gallery. Following is the data block that I’m using to get latest image version and update the VM.

data "azurerm_shared_image_version" "stg_linux_image" {
  name                = "latest"
  image_name          = "my-ubuntu-image"
  gallery_name        = "my_custom_gallery"
  resource_group_name = "my_pe_rg"
}

Till yesterday, it was reading the correct version. I’m using $(Build.BuildId) to create image version names. The version names were like:

  • 1.0.99549
  • 1.0.99690

But yesterday the build id became six digits and the image version name is:

  • 1.0.100150

After this, unless I specify 1.0.100150, the data block is reading 1.0.99690 with latest tag.

What am I doing wrong or is there a limitation on name length which I don’t know about?

Well, I did some digging. TF_LOG_PROVIDER=trace helped me.
The provider is reading all the image versions. But unfortunately when sorting to get latest version looks like it is picking images with version starting with 9xxx over 1xxx as 9 comes after 1. It is ignoring the fact that the one with 1xxxx has 6 digits but 9xxxx has only 5.

Another thing that I didn’t try is setting sort_versions_by_semver. Will check it as well to see if it can figure it out correctly.

The issue is likely due to how version numbers are sorted lexicographically rather than numerically by Terraform, causing 1.0.99690 to be interpreted as more recent than 1.0.100150. To fix this, ensure consistent version naming that aligns numerically and lexicographically, or specify the exact version instead of using latest.