boltops-tools / terraspace

Terraspace: The Terraform Framework
https://terraspace.cloud
Apache License 2.0
678 stars 46 forks source link

.terraspace-cache folder doesn't contain the module template file #138

Closed pie-r closed 3 years ago

pie-r commented 3 years ago

terraspace version:

bundle exec terraspace version
0.6.13

I have a module like this, with a test_ssh folder and a .tpl file on it:

  infra
   ├── app
    │   ├── modules
    │   │   └── ec2
    │   │       ├── ec2.tf
    │   │       ├── iam.tf
    │   │       ├── output.tf
    │   │       ├── test_ssh
    │   │       │   └── user_data.tpl. --> this file is missing in .terraspace-cache
    │   │       ├── variables.tf
    │   │       └── versions.tf

But in the .terraspace-cache folder there isn't the template file after the plan:

└── infra
    ├── .gitignore
    ├── .terraspace-cache
    │   └── eu-central-1
    │       └── sandbox
    │           ├── modules
    │           │   └── ec2
    │           │       ├── ec2.tf
    │           │       ├── iam.tf
    │           │       ├── output.tf
    │           │       ├── variables.tf
    │           │       └── versions.tf

In Fact, I tried with:

resource "null_resource" "temporary" {
  triggers = {
    path = "${path.module}/test_ssh/user_data.tpl",
    path1 = "${path.cwd}/test_ssh/user_data.tpl",
    path2 = "${path.root}/test_ssh/user_data.tpl"
  }

}

The plan is:

  # module.ec2-mice-instance.null_resource.temporary will be created
  + resource "null_resource" "temporary" {
      + id       = (known after apply)
      + triggers = {
          + "path"  = "../../modules/ec2/test_ssh/user_data.tpl"
          + "path1" = "/Users/xxx/projects/git/terraform-services/terraspace/infra/.terraspace-cache/eu-central-1/sandbox/stacks/mice-service/test_ssh/user_data.tpl"
          + "path2" = "./test_ssh/user_data.tpl"
        }
    }

The template example I'm working on is:

data "template_file" "test_user_data" {
  # No way to fix it
  template = file("${path.module}/test_ssh/user_data.tpl")
}

If I use ${path.module} I get:

│ Error: Invalid function argument
│ 
│   on ../../modules/ec2/ec2.tf line 3, in data "template_file" "test_user_data":
│    3:   template = file("${path.module}/test_ssh/user_data.tpl")
│     ├────────────────
│     │ path.module is "../../modules/ec2"
│ 
│ Invalid value for "path" parameter: no file exists at
│ ../../modules/ec2/test_ssh/user_data.tpl; this function works only with
│ files that are distributed as part of the configuration source code, so if
│ this file will be created by a resource in this configuration you must
│ instead obtain this result from an attribute of that resource.

With the full original path ("/Users/xxx/projects/git/terraform-services/terraspace/infra/.terraspace-cache/eu-central-1/sandbox/stacks/mice-service/test_ssh/user_data.tpl") outside the .terraspace-cache folder, it works.

tongueroo commented 3 years ago

This might be because terraspace cleans up the .terraspace-cache folder between commands. You can turn off this behavior with build.clean_cache = false

config/app.rb

Terraspace.configure do |config|
  ...
  config.build.clean_cache = false
end

Thinking that may be it. Am considering changing the default from true to false.

pie-r commented 3 years ago

Hi, no I already tested this flag without a good effect. I found this flag from another issue but in this case it doesn't fix the problem. I'd like to contribute if you can put me in the right path.

OT: I'm comparing terraspace and terragrunt in 4 articles, the next one will be terraspace, are you interested to review it before the release? https://medium.com/geekculture/from-terralith-to-terraservice-with-terraform-acf990e65578

tongueroo commented 3 years ago

Figured out what's going on. The builder was checking for any path with the word test in it. The test_ssh folder was being skipped. Fixed #142 and released in 0.6.17

RE: article

Very cool! Can read it after its published. Hope it's good. Good luck with the article! 🎉