Repository: hashicorp/terraform-cdk

Issue #664: 0.3.0 breaking local module in subdirectory
Created: 2021-04-27T23:06:14Z
Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment


cdktf & Language Versions
cdktf version: 0.3.0
typescript: 4.2.4
Affected Resource(s)
TerraformHclModule
Debug Output

Expected Behavior
new TerraformHclModule(this, stackName, {
    source: "./terraform",
    variables: {
        ...
    },
});
Should result in module path being ./terraform
Actual Behavior
Module path is ../terraform
Running subsequent cdktf deploy fails since it can't find the module.
Steps to Reproduce
Create a subfolder with a terraform HCL module in it, then call cdktf deploy
Important Factoids

References
In cdktf.out/stacks/<stack_name>/cdk.tf.json, the rendered source is "../terraform"
Looks like it may have been introduced here: 
  
    
      terraform-cdk/packages/cdktf/lib/terraform-module.ts
    
    
         Line 31
      in
      e39f5e5
    
  
  
    

        
          
           if (options.source.startsWith('./') || options.source.startsWith('../')) {

Duplicate issue #946: Incorrect module source path generated for local modules
Created: 2021-09-02T13:40:07Z
I believe that the source path that is generated in cdk.tf.json is incorrect for local modules, it doesn't take into account the output folder structure being stack/<stack name> and it needs to back up one more folder. I believe the issue is on L34

  
    
      terraform-cdk/packages/cdktf/lib/terraform-module.ts
    
    
        Lines 33 to 35
      in
      337acff
    
  
  
    

        
          
           if (options.source.startsWith("./") || options.source.startsWith("../")) { 
        

        
          
             this.source = path.join("..", options.source); 
        

        
          
           } else { 
        
    
  


It should be
this.source = path.join("../..", options.source);

This is how I reproduce it:

create project folder and cd into it.
run cdktf init --template="python-pip" --local
create a tf module at modules/module1
add modules/module1/main.tf

resource "aws_s3_bucket" "bucket" {
  bucket = "cdktf-test-bucket"
  acl    = "private"
  force_destroy = true
}

your file structure should look like this
<project folder>
  |-->modules
  |       |-->module1
  |         |- main.tf
  |- cdktf.json
  |- main.py


add module reference to cdktf.json

"terraformModules": [
    {
      "name": "module1",
      "source": "./modules/module1"
    }
  ],  


run cdktf get
update stack to use module

#!/usr/bin/env python

from cdktf import App, TerraformStack
from constructs import Construct

from imports.aws import AwsProvider

from imports.module1 import Module1


class MyStack(TerraformStack):

    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)

        AwsProvider(self, 'Aws', region="us-east-1")

       Module1(
           self,
           "cdktf_test_bucket",
       )

app = App()

stack = MyStack(app, "cdktf_test_stack")
app.synth()


run cdktf diff

you should get errors similar to
2021-09-02T22:50:45.536] [ERROR] default -
│ Error: Unreadable module directory
│
│ Unable to evaluate directory symlink: CreateFile ..\modules: The system
│ cannot find the file specified.


│ Error: Failed to read module directory
│
│ Module directory  does not exist or cannot be read.

Closing comment (2021-09-09T03:51:22Z): Seems like a duplicate of #664

maybe was fixed for typescript, but currently I'm getting the same error using python.

Apologizes. I wasn't trying to imply that it was fixed; just that there is another issue that is also tracking the same problem.

the module binding generation is not working on this case, at least it should copy the hcl module into the .terraform/modules path as works for the modules that come from the terraform registry.

I don't think I'm completely following what is going wrong. Module binding generation is controlled by adding modules to cdktf.json and running cdktf get. No generation is done for TerraformHclModule. The value is just used as it is in Terraform, though with some complications for relative paths. If you could provide a snippet of your cdktf.json and module references in code, we'd be happy to look into it further.