Skip to content

Update QuickStart documentation with an additional example to control the output directory #4299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,42 @@ You can also see what to expect in your filesystem at each step [here](https://g

This is where that additional verbosity in Terragrunt logging is really handy. You can see that Terragrunt concurrently ran `apply -auto-approve` in both the `foo` and `bar` units. The extra logging for Terragrunt also included information on the names of the units that were processed, and disambiguated the output from each unit.

When Terragrunt runs these commands, it creates a `.terragrunt-cache` directory in each unit's directory. This cache directory serves as Terragrunt's scratch directory where it:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all good content!


- Downloads your remote OpenTofu/Terraform configurations
- Runs your OpenTofu/Terraform commands
- Stores downloaded modules and providers
- Stores generated files (in this case, the `hi.txt` file will be created under `.terragrunt-cache/[HASH]/[HASH]/hi.txt` rather than directly in the `foo` or `bar` directories)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can cut this. We mention this in the next line, and "generated files" is confusing in this context. OpenTofu/Terraform generates this file, not Terragrunt.


With this configuration, the `hi.txt` files will be created directly in the `foo` and `bar` directories instead of the `.terragrunt-cache` directory.

The `.terragrunt-cache` directory is typically added to `.gitignore` files, similar to the `.terraform` directory that OpenTofu generates. You can safely delete this folder at any time, and Terragrunt will recreate it as necessary.

If you want to control where the files are created, you can modify the module to accept an output directory parameter. For example, you can update the `shared/main.tf` file to:

```hcl
variable "content" {}
variable "output_dir" {}

resource "local_file" "file" {
content = var.content
filename = "${var.output_dir}/hi.txt"
}
```

Then in your `foo/terragrunt.hcl` and `bar/terragrunt.hcl` files, you can use the `get_terragrunt_dir()` built-in function to get the directory where the `terragrunt.hcl` file is located:

```hcl
terraform {
source = "../shared"
}

inputs = {
output_dir = get_terragrunt_dir()
content = "Hello from bar, Terragrunt!"
}
```

Similar to the `tofu` CLI, there is a prompt to confirm that you are sure you want to run the command in each unit when performing a command that's potentially destructive. You can skip this prompt by using the `--non-interactive` flag, just as you would with `-auto-approve` in OpenTofu.

```bash
Expand Down
36 changes: 36 additions & 0 deletions docs/_docs/01_getting-started/01-quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,42 @@ Are you sure you want to run 'terragrunt apply' in each folder of the stack desc

This is where that additional verbosity in Terragrunt logging is really handy. You can see that Terragrunt concurrently ran `apply -auto-approve` in both the `foo` and `bar` units. The extra logging for Terragrunt also included information on the names of the units that were processed, and disambiguated the output from each unit.

When Terragrunt runs these commands, it creates a `.terragrunt-cache` directory in each unit's directory. This cache directory serves as Terragrunt's scratch directory where it:

- Downloads your remote OpenTofu/Terraform configurations
- Runs your OpenTofu/Terraform commands
- Stores downloaded modules and providers
- Stores generated files (in this case, the `hi.txt` file will be created under `.terragrunt-cache/[HASH]/[HASH]/hi.txt` rather than directly in the `foo` or `bar` directories)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to replicate the edit between these two if you accept the suggestion above.


With this configuration, the `hi.txt` files will be created directly in the `foo` and `bar` directories instead of the `.terragrunt-cache` directory.

The `.terragrunt-cache` directory is typically added to `.gitignore` files, similar to the `.terraform` directory that OpenTofu generates.

If you want to control where the files are created, you can modify the module to accept an output directory parameter. For example, you can update the `shared/main.tf` file to:

```hcl
variable "content" {}
variable "output_dir" {}

resource "local_file" "file" {
content = var.content
filename = "${var.output_dir}/hi.txt"
}
```

Then in your `foo/terragrunt.hcl` and `bar/terragrunt.hcl` files, you can use the `get_terragrunt_dir()` built-in function to get the directory where the `terragrunt.hcl` file is located:

```hcl
terraform {
source = "../shared"
}

inputs = {
output_dir = get_terragrunt_dir()
content = "Hello from bar, Terragrunt!"
}
```

Similar to the `tofu` CLI, there is a prompt to confirm that you are sure you want to run the command in each unit when performing a command that's potentially destructive. You can skip this prompt by using the `--non-interactive` flag, just as you would with `-auto-approve` in OpenTofu.

```bash
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/docs/01-quick-start/step-05.1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.terragrunt-cache
hi.txt
3 changes: 3 additions & 0 deletions test/fixtures/docs/01-quick-start/step-05.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This example demonstrates how to control output file locations using get_terragrunt_dir().

The hi.txt files will be created directly in the foo and bar directories instead of the .terragrunt-cache directory.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
source = "../shared"
}

inputs = {
output_dir = get_terragrunt_dir()
content = "Hello from bar, Terragrunt!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
source = "../shared"
}

inputs = {
output_dir = get_terragrunt_dir()
content = "Hello from foo, Terragrunt!"
}
7 changes: 7 additions & 0 deletions test/fixtures/docs/01-quick-start/step-05.1/shared/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "content" {}
variable "output_dir" {}

resource "local_file" "file" {
content = var.content
filename = "${var.output_dir}/hi.txt"
}
Loading