cdktf / cdktf-tf-module-stack

A drop-in replacement for cdktf.TerraformStack that lets you define Terraform modules as constructs
Mozilla Public License 2.0
19 stars 2 forks source link

Trying to use an object as TFModuleVariable fails on synthetizing #308

Closed froblesmartin closed 2 months ago

froblesmartin commented 4 months ago

Description

I have started using this project to build a module with CDKTF.

I have set up a CDKTF project which uses the CDKTF Module and it works fine with a basic test, even passing a variable with a TFModuleVariable.

Now I want to do a more complex use case, that would involve having a TFModuleVariable which would be a complex object. Then I want to loop over it, and that is what fails. My current CDKTF module code looks like this:

class GitHubModule extends TFModuleStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    new ProviderRequirement(this, "github", "6.0.0", "integrations/github")

    const memberships = new TFModuleVariable(this, "memberships", {
      description: "Admin and members of the organization",
      default: {
        admins: ["fake"],
        members: ["fake"]
      }
    })

    memberships.value.admins.forEach(function(user: string) {
      new Membership(scope, `github-membership-${user}", {
        username: user,
        role: "admin"
      })
    })
  }
}

And that fails with:

[ERROR] default - TypeError: Cannot read properties of undefined (reading ‘forEach’)

I understand that, at synth time the content is undefined and therefore it fails. But then, this does not allow to pass dynamic configurations like this? I tried adding default values so that it could have some value, but it still failed.

Versions

cdktf debug output:

language: typescript
cdktf-cli: 0.20.3
node: v18.17.1
cdktf: 0.20.4
constructs: 10.3.0
jsii: null
terraform: 1.7.2
arch: arm64
os: darwin 23.2.0
providers
integrations/github@6.0.0 (LOCAL)
        terraform provider version: 6.0.0
@cdktf/provider-github (PREBUILT)
        terraform provider version: 6.0.0
        prebuilt provider version: 14.0.0
        cdktf version: ^0.20.0

Providers

No response

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

Community Note

DanielMSchmidt commented 2 months ago

This is unfortunately not how iteration works in CDKTF, please see https://developer.hashicorp.com/terraform/cdktf/concepts/iterators for all the infos.