Skip to content

Calling Invoke-EpsTemplate Inside for loop #36

Description

@jeffpatton1971

I know this repo is old, I'm really hoping someone is watching this, so here goes.

I'm using eps to create tf files that are passed to modules. For one resource there are a list of nested resources, so this script calls two templates, one to create the main tf and one to create the vars.tf that is passed in. When I run the vars eps template with a group of objects, it returns as expected.

When i run in the context of the script, the eps doesn't iterate over the objects passed in. I'm curious if that's because I'm calling invoke from inside a for loop and that's somehow messing things up?

Here's some stripped down samples, cant' really share everything sorry:

Calling script

$Groups = $Resource | Group-Object -Property ServerName;
$ListPath = $TemplatePath.Replace('.eps', '-list.eps');

  foreach ($Group in $Groups) {
   
    $ModuleName = "$($ServerName)-$([System.Guid]::NewGuid().Guid)";
   
      Invoke-EpsTemplate -Path $TemplatePath -binding @{
        ModuleName = $ModuleName;
        Source     = $Source;
        ResourceId = $ResourceId;
      } -Safe
      Invoke-EpsTemplate -Path $ListPath -Binding @{
        ModuleName = $ModuleName;
        Resource   = $Group.Group;
      }
    }
  }

Initial eps

module "<%= $ModuleName %>" {
  source             = "<%= $Source %>"

  list            = "<%= "var.$($ModuleName)_list" %>"

}

looped eps

variable "<%= "$($ModuleName)_list" %>" {
  default = [
<% $Count = 0 -%>
<% $Resource |ForEach-Object { -%>
    {
      name                  = "<%= $_.Name %>"
<% $Count++ -%>
    }<% if (!($Count -eq $Resource.Count)) { %>,<% } %>
<% } -%>
  ]
}

The resulting output when run through the script is below

module "sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2" {
  source             = "github"
  list            = "var.sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2_list"
}
variable "sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2_list" {
  default = [
    {
      name                  = "subname1"
    },
  ]
}
module "sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4" {
  source             = "github"
  list            = "var.sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4_list"
}
variable "sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4_list" {
  default = [
    {
      name                  = "subnamea"
    },
  ]
}

The resource that is passed in is an array of psobjects that have n+1 unique ServerNames.

ServerName resourceName
---------- ------------
server1    sample1
server1    sample2
server1    sample3
server1    sample4
server1    sample5
server2    samplea
server2    sampleb
server2    samplec
server2    sampled

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions