How to use modules output for resources?

Dear Experts,

I successfully imported custom Terraform Modules in Terraform CDK. However, I can’t use the output to create for example an instance with a security group:

            const moduleSg = new AwsSg(this, `AwsSg${vpcIndex}`, { namePrefix: config.user_id, resourceTag: `resourceTag${vpcIndex}`, vpcId: vpc.id })

            new ec2.Instance(this, `compute${subnetIndex}${vpcIndex}`, {
              subnetId: subnet.id,
              ami: "ami-089950bc622d39ed8",
              instanceType: "t2.micro",
              keyName: "Key_MBP_fdervisi",
              securityGroups: [moduleSg.serviceSecurityGroupIdOutput],
              tags: {
                Name: "instance1",
                Owner: ""
              }
            });

I got this error:

Inappropriate value for attribute "security_groups": element 0: string required.
type or paste code here

The problem is that the security is not resolved and therefore it is not a string. Is this the limitation? If yes do we have any workarounds?

Hi @fdervisi :wave:

Could you do a console.log(moduleSg.serviceSecurityGroupIdOutput) and confirm that there’s some kind of token in there?

What is AwsSg based on? Is it based on a Terraform module?

@ansgarm yes it is a Terraform Module which has been integrated via cdktf get. The token is there but I guess this is a limitation when you use Terraform modules in Terraform CDK!?

Hey, could you send us a bit more of the program / plan or synth output / cdktf.json so we can debug this further. It should work, there is no limitation in modules besides every output being of type string (which means sometimes you have to cast the token to another data type).

You likely want to be setting vpcSecurityGroupIds instead of securityGroups, although the error does seem like something else may be going wrong.