How to sort key/value pairs in terraform?

We are storing the properties of a device as name-value pair in file,e.g.

custom_properties = [
name = “addr”
value = “”
name = “host”
value = “localhost”
name = “snmp.version”
value = “v2c”
On the creation of the device all properties are set in the order. The issue arises when there is any update on a terraform file in this environment. Due to the fact the properties are not stored in alphabetically sorted order in State file, Terraform is showing a lot of changes if finds (but which are in fact just a mixed due to sorting). How can we store properties in sorted order in tfstate file after every updation or creation ?

You should explain which Terraform resource you are using, and provide some sample terraform plan output showing the undesirable diff presentation.

Without that, too much is unknown to offer a reliable suggestion.

We use Terraform to deploy our Cloud resources, including registering them with several properties ,these properties are called custom_properties and need to be presented as key-value pairs.This works fine on the creation part. But when there is any update on a Terraform File, it wants to change a lot of properties because they look different, which is caused by presenting them in a sorted order, and not in the way they have been entered… So every Terraform plan run gives a lot of potential changes which are not changes at all, but it only looks like. This make us identifying the real changes quite difficult.

Hi @lm-madhvi,

In Terraform’s architecture it’s the responsibility of a provider to determine whether a difference between the configuration and the current system settings is a meaningful difference that needs to be fixed or just the remote API presenting the same information in a different way.

Therefore what you’ve seen here seems like a bug in the “logicmonitor” provider, which should be treating these values as equal if they differ only in ordering, assuming that the remote API does not consider the ordering to be significant. I suggest opening a bug report issue in the provider’s GitHub repository, which you can find from its page in the Terraform Registry.

To work around this until the bug is fixed, one relatively easy answer is to project the list into a map whose keys can be sorted by Terraform to achieve the correct order, and then use values to take the values of that map sorted by key.

For example, if the “name” attribute values are unique and are what the remote API is sorting by:

  for p in var.custom_properties : => p

values implicitly sorts the map elements by their keys before taking just the values, so the above would have the effect of sorting the elements by their names.

If I’m reading the plan diff correctly, it may not be possible to fix this without a code change in the logicmonitor provider, because I think the plan diff is showing the existing state of the resource (presumably read from the server?) as not being ordered by name alphabetically, and the diff is already proposing to move it into alphabetic order - but apparently this is not sticking.

It appears that this issue has already been reported in the provider’s issue tracker: Provider can not handle change in order of custom properties · Issue #53 · logicmonitor/terraform-provider-logicmonitor · GitHub but it doesn’t look like anyone is working on it :-/