Forcing order in ressource creation

Hello,

Is it possible to create ressources in specific order ?

I am using a YAML file with content in list :

cloudlogs_view:
  - name: "1_exemple-view"
    query: "severity=\"Warning\""
  - name: "2_exemple-view"
    query: "severity=\"Critical\""
  - name: "3_exemple-view"
    query: "severity=\"Warning\""

With the Terraform config :

locals {
  input_cloudlogs_view = yamldecode(file(var.config_cloudlogs_view)).cloudlogs_view
}

resource "ibm_logs_view" "logs_view_instance_yaml" {
  instance_id = var.instance_id
  region      = var.region

  for_each = { for x in local.input_cloudlogs_view : x.name => x }

  name        = each.value.name
 
  search_query {
    query = each.value.query
  }

  time_selection {
    quick_selection {
      caption = "Last 24 hours"
      seconds = 86400
    }
  }
}

 output "view_created" {
  value = [
    for k in ibm_logs_view.logs_view_instance_yaml : "${k.name}"
  ]
  }

   output "view_yaml" {
  value = [
    for k in local.input_cloudlogs_view : "${k.name}"
  ]
  }

After executing, we can verify the ressources get from the yaml and in the ressource list “ibm_logs_view.logs_view_instance_yaml”, the are well ingered in the right order

view_created = [
  "1_exemple-view",
  "2_exemple-view",
  "3_exemple-view",
]
view_yaml = [
  "1_exemple-view",
  "2_exemple-view",
  "3_exemple-view",
]

But in reality the order is not respected :

If I understand well, this is due to parallelism execution of Terraform.

But can we force the order ?

Thanks for your help.

Dominique

Force order in same item is not possible because this need the resource depends on the previous resource in same “list”, but when you decide for “for_each” you choose for parallell creation of the itens.

This order is because the responde of the provider for your resources.

Try access resources result using set(…) and your itens will respect your order, ignoring order of creation.

Hello,

Thanks for your response.

You propose to use the toset() function ?

In the doc, they say the order is removing (toset - Functions - Configuration Language | Terraform | HashiCorp Developer)

When you created for_each expression, the order respect the same rule from toset() function. Because map/set aways order your list with same rule.

So, if your order of the creation don’t respect the order of for_each, toset will fix it.

Remember, using toset() to read the list… the order of creation will not change, but you can control how to read this list.

Normally, you can use implicit dependencies (via reference) or explicit depends_on to force ordering (though explicit depends are less preferred). But in the case where you’re using an iterator, I think you can’t really control order of resource creation, even if you force order on the data structure because of the reason you mentioned (Terraform’s internal parallelism).

I’m unsure what the earlier reply about toset is intending to convey, but sets and maps are unordered data structures. You cannot use toset to create order via something which inherently has no order.

As for ordering of operations, it is correct that there is no order imposed within resources expanded via count or for_each, nor is there any way to define such an order. In fact multiple instance changes are not only unordered, but they may be run concurrently as well.