Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [2.2.1] - 2025-12-15

### Fixed
- **Terraform module**: Added `skip_vars` parameter to `tf_run` function for commands that don't support `-var-file`
- Commands that now correctly skip `-var-file`: `init`, `validate`, `output`, `state list`
- New test: `test_modules_terraform_run_skip_vars`

## [2.2.0] - 2025-12-15

### Added
Expand Down
17 changes: 10 additions & 7 deletions modules/infrastructure/terraform/terraform.mk
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,27 @@ endef

## @function tf_run
## @desc Execute terraform command in environment directory
## @desc Automatically includes shared tfvars if tf_shared_vars is set
## @desc Automatically includes shared tfvars if tf_shared_vars is set (unless skip_vars is true)
## @arg 1: env (required) - Environment name (e.g., local, dev, prod)
## @arg 2: command (required) - Terraform command (e.g., init, plan, apply)
## @arg 3: extra_args (optional) - Additional arguments
## @arg 4: skip_vars (optional) - Set to $(mb_true) to skip -var-file inclusion (for commands that don't support it)
## @returns Command output via mb_invoke
## @group terraform
## @example $(call tf_run,local,init)
## @example $(call tf_run,local,init,,,$(mb_true))
## @example $(call tf_run,dev,plan,-detailed-exitcode)
## @example $(call tf_run,prod,apply,-auto-approve)
## @note Commands that don't support -var-file: init, validate, output, state
define tf_run
$(strip \
$(if $(value 1),,$(call mb_printf_error,$0: env required)) \
$(if $(value 2),,$(call mb_printf_error,$0: command required)) \
$(eval $0_arg1_env := $(strip $1)) \
$(eval $0_arg2_cmd := $(strip $2)) \
$(eval $0_arg3_extra := $(if $(value 3),$(strip $3))) \
$(eval $0_arg4_skip_vars := $(if $(value 4),$(strip $4))) \
$(eval $0_chdir := $(call tf_build_chdir,$($0_arg1_env))) \
$(eval $0_var_file := $(call tf_build_var_file)) \
$(eval $0_var_file := $(if $(filter $(mb_true),$($0_arg4_skip_vars)),,$(call tf_build_var_file))) \
$(call mb_invoke,$(tf_bin) $($0_chdir) $($0_arg2_cmd) $($0_var_file) $($0_arg3_extra)) \
)
endef
Expand All @@ -107,7 +110,7 @@ ifndef __MB_TEST_DISCOVERY__

terraform/init/%: ## Initialize Terraform for environment (usage: make terraform/init/local)
$(call mb_printf_info,Initializing Terraform for $* environment...)
$(call tf_run,$*,init)
$(call tf_run,$*,init,,$(mb_true))

terraform/plan/%: ## Plan Terraform changes (usage: make terraform/plan/dev)
$(call mb_printf_info,Planning Terraform changes for $* environment...)
Expand All @@ -131,13 +134,13 @@ terraform/destroy/%: ## Destroy Terraform infrastructure (usage: make terraform/

terraform/validate/%: ## Validate Terraform configuration (usage: make terraform/validate/local)
$(call mb_printf_info,Validating Terraform configuration for $*...)
$(call tf_run,$*,validate)
$(call tf_run,$*,validate,,$(mb_true))

terraform/output/%: ## Show Terraform outputs (usage: make terraform/output/local)
$(call tf_run,$*,output)
$(call tf_run,$*,output,,$(mb_true))

terraform/state/list/%: ## List Terraform state resources (usage: make terraform/state/list/local)
$(call tf_run,$*,state list)
$(call tf_run,$*,state list,,$(mb_true))

terraform/refresh/%: ## Refresh Terraform state (usage: make terraform/refresh/local)
$(call mb_printf_info,Refreshing Terraform state for $*...)
Expand Down
19 changes: 19 additions & 0 deletions modules/infrastructure/terraform/terraform_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ define test_modules_terraform_run_with_shared_vars
$(eval mb_invoke_silent := $(mb_off))
endef

define test_modules_terraform_run_skip_vars
$(eval mb_invoke_silent := $(mb_on))
$(eval tf_bin := terraform)
$(eval tf_chdir_flag := $(mb_true))
$(eval tf_env_dir := terraform/environments)
$(eval tf_shared_vars := ../../shared/common.tfvars)

## With skip_vars=$(mb_true), should NOT include -var-file
$(eval $0_result := $(call tf_run,local,validate,,$(mb_true)))
$(call mb_assert_contains,terraform,$($0_result))
$(call mb_assert_contains,validate,$($0_result))
$(call mb_assert_not_empty,$(findstring -chdir,$($0_result)))
## Verify -var-file is NOT present
$(call mb_assert_empty,$(findstring -var-file,$($0_result)),skip_vars should exclude -var-file)

$(eval tf_shared_vars :=)
$(eval mb_invoke_silent := $(mb_off))
endef

define test_modules_terraform_run_error_without_env
$(eval mb_invoke_silent := $(mb_on))

Expand Down