The Terragrunt Reference Architecture utilizes .env files for managing environment variables. This approach provides a straightforward way to configure settings for different environments. Typically, you will copy the provided .env.example file to a new file named .env (which should be excluded from version control via .gitignore) and then customize the variables within your .env file as needed.
The justfile in this project is configured with set dotenv-load, which means that just commands will automatically load variables from a .env file found in the project root.
- Simple Key-Value Pairs:
.envfiles use a simpleKEY=valueformat. - Easy Customization: Users can easily override or set variables by creating and editing a
.envfile. - Gitignored by Default: User-specific
.envfiles are typically gitignored to keep sensitive data or local preferences out of version control. - Automatic Loading:
justrecipes automatically source variables from.envfiles.
The primary configuration for environment variables involves:
/
├── .env.example # Example file showing available variables and default/template values
└── .env # User-specific, gitignored file where actual variable values are set
Environment variables are typically defined in a .env file in the project root. This file is loaded by just when recipes are executed.
.env.example: Serves as a template and documentation for required or optional environment variables..env: This is your local, untracked file where you set the actual values for your development or deployment environment.- Loading: Variables are made available to the execution context of
justrecipes.
To customize environment variables for your local setup or specific deployments:
- Copy
.env.example: If a.envfile does not already exist in the project root, copy the.env.examplefile to a new file named.env.cp .env.example .env
- Edit
.env: Open the.envfile and modify the variable values as needed for your environment. For example:# .env (example content) DEFAULT_REGION=us-west-2 TG_STACK_APP_PRODUCT_NAME=my-cool-app # Add any other custom variables required MY_CUSTOM_API_KEY=xxxxxxxxxxxx
- Ensure
.envis Gitignored: The.envfile typically contains sensitive information or local-specific settings and should not be committed to version control. Make sure.envis listed in your project's.gitignorefile.
Variables defined in the .env file will be automatically loaded by just when you run recipes defined in the justfile.
This section provides a comprehensive list of environment variables used throughout the Terragrunt Reference Architecture, including those commonly defined in .env files (based on .env.example) and those utilized within HCL configurations.
| Category | Variable Name | Description | Default Value (.env.example or HCL) |
Primary Source/Usage |
|---|---|---|---|---|
| Project & Metadata | PROJECT_ROOT |
Absolute path to the project root directory. | Dynamically set by scripts if needed | Shell environment, Scripts |
TG_STACK_APP_AUTHOR |
Author of the configuration. | Your Name (from .env.example) |
.env, infra/terragrunt/_shared/_config/tags.hcl |
|
TG_STACK_APP_PRODUCT_NAME |
Project/application name for identification. | your-app-name (from .env.example) |
.env, infra/terragrunt/_shared/_config/app.hcl, infra/terragrunt/_shared/_config/tags.hcl |
|
| Cloud Provider & Region | DEFAULT_REGION |
Default cloud provider region. | us-east-1 (from .env.example) |
.env |
TG_STACK_DEPLOYMENT_REGION |
AWS region for deployments, overrides DEFAULT_REGION for Terragrunt. |
us-east-1 (from HCL) |
infra/terragrunt/config.hcl |
|
| Terraform & Terragrunt | ||||
| General | TG_NON_INTERACTIVE |
Replaces TF_INPUT. If true, disables interactive prompts. |
true (from .env.example) |
.env (Terragrunt Standard) |
TG_STACK_TF_VERSION |
Enforced Terraform version. | 1.9.0 (from .env.example), 1.11.3 (HCL) |
.env, infra/terragrunt/config.hcl |
|
| Terragrunt Performance | TERRAGRUNT_DOWNLOAD_DIR |
Directory where Terragrunt downloads remote sources. | ${HOME}/.terragrunt-cache/... (from .env.example) |
.env (Terragrunt Standard) |
TERRAGRUNT_CACHE_MAX_AGE |
Max age for items in Terragrunt cache. | 168h (from .env.example) |
.env (Terragrunt Standard) |
|
| Terragrunt Behavior | TG_LOG_LEVEL |
Terragrunt logging verbosity. Replaces TERRAGRUNT_LOG_LEVEL. |
info (from .env.example) |
.env (Terragrunt Standard) |
TERRAGRUNT_DISABLE_CONSOLE_OUTPUT |
If true, disables Terragrunt console output. | false (from .env.example) |
.env (Terragrunt Standard) |
|
TG_NO_AUTO_INIT |
Replaces TERRAGRUNT_AUTO_INIT (inverted). If true, disables auto-init. |
false (meaning auto-init is enabled, from .env.example) |
.env (Terragrunt Standard) |
|
TG_NO_AUTO_RETRY |
Replaces TERRAGRUNT_AUTO_RETRY (inverted). If true, disables auto-retry. |
false (meaning auto-retry is enabled, from .env.example) |
.env (Terragrunt Standard) |
|
| Terragrunt Flags (HCL) | TG_STACK_FLAG_ENABLE_PROVIDERS_OVERRIDE |
Controls dynamic provider file generation. | true (from HCL) |
infra/terragrunt/config.hcl |
TG_STACK_FLAG_ENABLE_VERSIONS_OVERRIDE |
Controls dynamic version file generation. | true (from HCL) |
infra/terragrunt/config.hcl |
|
TG_STACK_FLAG_ENABLE_TERRAFORM_VERSION_FILE_OVERRIDE |
Controls .terraform-version file generation. |
false (from HCL) |
infra/terragrunt/config.hcl |
|
| Logging & Diagnostics | LOG_LEVEL |
General logging level for scripts. | info (from .env.example) |
.env |
LOG_DIR |
Directory for storing logs. | ${HOME}/.logs/... (derived, from .env.example) |
.env |
|
| Remote State Config | TG_STACK_REMOTE_STATE_BUCKET_NAME |
S3 bucket for Terraform remote state. | terraform-state-makemyinfra (from .env.example) |
.env, infra/terragrunt/_shared/_config/remote_state.hcl |
TG_STACK_REMOTE_STATE_LOCK_TABLE |
DynamoDB table for state locking. | terraform-state-makemyinfra (from .env.example) |
.env, infra/terragrunt/_shared/_config/remote_state.hcl |
|
TG_STACK_REMOTE_STATE_REGION |
AWS region for remote state storage. | us-east-1 (from HCL) |
infra/terragrunt/_shared/_config/remote_state.hcl |
|
TG_STACK_REMOTE_STATE_OBJECT_BASENAME |
Basename for the Terraform state object file. | terraform.tfstate.json (from HCL) |
infra/terragrunt/_shared/_config/remote_state.hcl |
|
TG_STACK_REMOTE_STATE_BACKEND_TF_FILENAME |
Filename for the generated backend configuration. | backend.tf (from HCL) |
infra/terragrunt/_shared/_config/remote_state.hcl |
|
| Module Version Overrides | TG_STACK_TF_MODULE_DNI_GENERATOR_VERSION_DEFAULT |
Default version for the DNI generator module. | v0.1.0 (from HCL) |
infra/terragrunt/_shared/_units/dni_generator.hcl |
TG_STACK_TF_MODULE_NAME_GENERATOR_VERSION_DEFAULT |
Default version for the name generator module. | v0.1.0 (from HCL) |
infra/terragrunt/_shared/_units/name_generator.hcl |
|
TG_STACK_TF_MODULE_AGE_GENERATOR_VERSION_DEFAULT |
Default version for the age generator module. | v0.1.0 (from HCL) |
infra/terragrunt/_shared/_units/age_generator.hcl |
|
TG_STACK_TF_MODULE_LASTNAME_GENERATOR_VERSION_DEFAULT |
Default version for the lastname generator module. | v0.1.0 (from HCL) |
infra/terragrunt/_shared/_units/lastname_generator.hcl |
Note: Some Terragrunt standard variables like TF_INPUT, TERRAGRUNT_LOG_LEVEL, TERRAGRUNT_AUTO_INIT, TERRAGRUNT_AUTO_RETRY have been replaced by their TG_ prefixed counterparts (e.g., TG_NON_INTERACTIVE, TG_LOG_LEVEL, TG_NO_AUTO_INIT, TG_NO_AUTO_RETRY) in the .env (via .env.example) for consistency and to align with newer Terragrunt practices where applicable. The TG_NO_AUTO_INIT and TG_NO_AUTO_RETRY variables have inverted logic compared to their predecessors.
- Use the
.env.examplefile as a template for your.envfile. - Store all local environment-specific configurations in your
.envfile. - Ensure
.envis included in your project's.gitignorefile to avoid committing local configurations or sensitive data. - Keep
.env.exampleup-to-date with all variables required or commonly customized for the project. - Use descriptive variable names in your
.envfiles and document their purpose if they are custom additions not found in.env.example.
-
Variables Not Loading
- Ensure you have a
.envfile in the project root. - If you copied from
.env.example, ensure the file is named exactly.env. - Check for syntax errors in your
.envfile (e.g., ensure there are no unquoted spaces in values if the value itself shouldn't contain leading/trailing spaces, thoughjust's dotenv loader is generally robust). - Verify that
set dotenv-loadis present in yourjustfile.
- Ensure you have a
-
Incorrect Variable Values
- Double-check the variable names and values in your
.envfile for typos. - Remember that variables loaded from the shell environment may override those in
.envfiles, depending on the system and shell configuration (thoughjust'sdotenv-loadtypically gives.envprecedence over pre-existing shell environment variables of the same name unlessdotenv-overrideis also set).
- Double-check the variable names and values in your
# Show all environment variables available to just (run from a just recipe)
# Example just recipe:
# show-env:
# @echo "Current Environment Variables:"
# @env
# Show a specific variable's value (run from a just recipe)
# Example just recipe for a variable MY_VAR:
# show-my-var:
# @echo "MY_VAR is: {{MY_VAR}}"
# Alternatively, to check outside of just, after running a just command that should load the .env:
# (This depends on your shell and if just exports the variables to its parent shell)
# echo $MY_VARIABLE_NAME