Reference External Local File

I have a directory structure as follows:

|- packer
| vpn
|------ ami
|- terraform
|— env
|------ dev
|------------ networking
|— stacks
|------ networking
|------ modules
|------------ vpn

With terragrunt I’m trying to reference a file in the packer/vpn/ami directory, from the terraform/modules/vpn directory, but cannot figure out the pathing.

Can this be acomplished with regular terraform path.root or is there something I need to do in terragrunt to correlate?

Thanks!

Hi @privomark,

It’s a bit hard to tell what the intended behavior is without seeing the code, or knowing what the exact repository structure is (e.g., is the packer folder in the same repo as terraform folder?), but in general there are two generic ways to pass through files to terraform:

  • If the file is in the same repository as the module, you want to build a relative path from the module to the file using path.module. Be sure to use // to make sure the entire repository is cloned, including the file you need. Otherwise, terragrunt/terraform might only clone the subfolder.

  • If the file is outside the repository, then the best way is to have terragrunt copy the file into the terraform module (and reference the path as if the file exists in the module dir). This happens automatically if the file already exists in the folder containing the terragrunt.hcl file, but you can also force terragrunt to do this using before_hook. Note that the before_hook approach also works if the file is outside the terragrunt repository, as you can have the hook fetch the remote file before calling terraform.

Hope this helps!

Yori

Thanks for the reply, here is my folder structure in a simplified format that may help represent my question.

├── packer
│   └── vpn
│       └── ami
├── stacks
│   └── networking
│       └── main.tf
└── terraform
   └── env
       └── dev
           └── networking

In the following structure, in the stacks/networking/main.tf I’m trying to archive the ami file in the packer/vpn directory using the terraform data "archive_file" resource.

data "archive_file" "ami" {
  type = "zip"
  source_dir = "${path.module}/../../packer/vpn."
  output_path = "${path.module}/ami.zip"
}

Of course I’m calling the “networking” stack from the terraform/env/dev/networking directory.

Every attempt to try to locate this folder has failed. All these directories are in the same git repository.

I’m guessing it might have to do with the // as you mentioned, but not sure where I would place those.

Thanks!

If you are using Terragrunt with remote Terraform configurations, then when you run terragrunt apply, Terragrunt will check out (or copy) the code from your source URL into a temporary folder (default: .terragrunt-cache). My guess is that this temporary folder is what’s causing you problems, as now the relative path between your stacks/networking/main.tf and the ami.zip are completely different.

One solution that may work is the double slash (//) thing mentioned by Yori. This works if the packer and stacks folders are in the same repo or parent folder. Let’s say they are both in the same parent folder called /infrastructure. You would then set the source URL in your terragrunt.hcl as follows:

terraform {
  source = "/infrastructure//stacks/networking"
}

When Terragrunt sees the double-slash after /infrastructure, it’ll know to copy that entire folder into .terragrunt-cache, including the stacks and packer sub-folders, so that when it runs your Terraform command in the stacks/networking folder, the relative path to the packer folder should work as you expect.