Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform : for_each one by one

I have created a module on terraform, this module creates aws_servicecatalog_provisioned_product resources.
When I call this module from the root I am using for_each to run into a list of objects.
The module runs into this list of objects and creates the aws_servicecatalog_provisioned_product resources in parallel.
Is there a way to create the resources one by one? I want that the module will wait for the first iteration to be done and to create the next just after.

like image 495
Dani Wol. Avatar asked Oct 25 '25 03:10

Dani Wol.


2 Answers

I am using terraform templatefile that creates resources with a depends on order, and then terraform creates resources one by one.

Here is the code:

locals {

     expanded_accounts = [ 
      {
          AccountEmail                           = [email protected]
          AccountName                         = example1
          ManagedOrganizationalUnit           = example_ou1
          SSOUserEmail                        = [email protected]
          SSOUserFirstName                    = Daniel
          SSOUserLastName                     = Wor
          ou_id                               = ou_id1
      },
      {
          AccountEmail                           = [email protected]
          AccountName                         = example2
          ManagedOrganizationalUnit           = example_ou2
          SSOUserEmail                        = [email protected]
          SSOUserFirstName                    = Ben
          SSOUserLastName                     = John
          ou_id                               = ou_id2
     }
     ]

  previous_resource = [
    for acc in local.expanded_accounts :
    acc.AccountName
  ]

     resources = { res = local.expanded_accounts, previous = concat([""], local.previous_resource)
}

resource "local_file" "this" {
  content              = templatefile("./provisioned_accounts.tpl", local.resources)
  filename             = "./generated_provisioned_accounts.tf"
  directory_permission = "0777"
  file_permission      = "0777"

  lifecycle {
    ignore_changes = [directory_permission, file_permission, filename]
  }
}

provisioned_accounts.tpl configuration:

%{ for acc in res }
resource "aws_servicecatalog_provisioned_product" "${acc.AccountName}" {
  name                     = "${acc.AccountName}"
  product_id               = replace(data.local_file.product_name.content, "\n", "")
  provisioning_artifact_id = replace(data.local_file.pa_name.content, "\n", "")
  

  provisioning_parameters {
    key   = "SSOUserEmail"
    value = "${acc.SSOUserEmail}"
  }
  provisioning_parameters {
    key   = "AccountEmail"
    value = "${acc.AccountEmail}"
  }
  provisioning_parameters {
    key   = "AccountName"
    value = "${acc.AccountName}"
  }
  provisioning_parameters {
    key   = "ManagedOrganizationalUnit"
    value = "${acc.ManagedOrganizationalUnit} (${acc.ou_id})"
  }
  provisioning_parameters {
    key   = "SSOUserLastName"
    value = "${acc.SSOUserLastName}"
  }
  provisioning_parameters {
    key   = "SSOUserFirstName"
    value = "${acc.SSOUserFirstName}"
  }

  timeouts {
    create = "60m"
  }

%{if index != 0 }
  depends_on = [aws_servicecatalog_provisioned_product.${previous[index]}]
%{ endif }

}

%{~ endfor ~}
like image 155
Dani Wol. Avatar answered Oct 26 '25 18:10

Dani Wol.


Is there a way to create the resources one by one?

Sadly, there is not such way, unless you remove for_each and create all the modules separately with depends_on.

TF is not a procedural language, and it always will do things in parallel for for_each and count.

like image 41
Marcin Avatar answered Oct 26 '25 18:10

Marcin