Does map sort keys?

After several months of investigations, with breaks, I finally realized what the problems were.
Is it normal that the keys in the map are sorted alphabetically?

I expected that as variables are added in this order they are, although it is not an array of data.

If this is normal behavior, then how can I have something similar to the one shown in the example, but in the same sequence as I set?

variable "cp_ip_addresses" {
	type    = map(list(string))
	default = {
		"public"       = ["69.168.x.y"]
		"mgmt"         = [""]
		"appliance"    = [""]
		"provisioning" = [""]

terraform console
> var.cp_ip_addresses
  "appliance" = [
  "mgmt" = [
  "provisioning" = [
  "public" = [

As you can see, the key sequence is different from what was specified.

Yes, map keys are always iterated in lexicographical order.

To be able to suggest a solution, could you explain why the order of iteration is important? What are you trying to accomplish that is prevented by sorted map keys?

1 Like