amazon-web-servicesterraformterraform-provider-awsterraform-template-file

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.


Solution

  • 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                           = example1@example.com
              AccountName                         = example1
              ManagedOrganizationalUnit           = example_ou1
              SSOUserEmail                        = example1@example.com
              SSOUserFirstName                    = Daniel
              SSOUserLastName                     = Wor
              ou_id                               = ou_id1
          },
          {
              AccountEmail                           = example2@example.com
              AccountName                         = example2
              ManagedOrganizationalUnit           = example_ou2
              SSOUserEmail                        = example2@example.com
              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 ~}