|
1 | | -# This file is for you! Edit it to implement your own Terraform make targets. |
| 1 | +# Terraform Make Targets for Shared Modules |
| 2 | +# This repository contains only Terraform modules (no components or tfscaffold) |
| 3 | +# Modules are located in infrastructure/modules/ |
2 | 4 |
|
3 | 5 | # ============================================================================== |
4 | | -# Custom implementation - implementation of a make target should not exceed 5 lines of effective code. |
5 | | -# In most cases there should be no need to modify the existing make targets. |
| 6 | +# Formatting and Validation |
6 | 7 |
|
7 | | -terraform-fmt: # Format Terraform files - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform fmt command, default is '-recursive'] @Quality |
8 | | - make _terraform cmd="fmt" \ |
9 | | - dir=$(or ${terraform_dir}, ${dir}) \ |
10 | | - opts=$(or ${terraform_opts}, ${opts}) |
| 8 | +terraform-fmt: # Format Terraform module files @Quality |
| 9 | + # Example: make terraform-fmt |
| 10 | + @cd infrastructure && terraform fmt -recursive modules |
11 | 11 |
|
12 | | -_terraform: # Terraform command wrapper - mandatory: cmd=[command to execute]; optional: dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], opts=[options to pass to the Terraform command, default is none/empty] |
13 | | - # 'TERRAFORM_STACK' is passed to the functions as environment variable |
14 | | - TERRAFORM_STACK=$(or ${TERRAFORM_STACK}, $(or ${terraform_stack}, $(or ${STACK}, ${stack}))) |
15 | | - dir=$(or ${dir}, ${TERRAFORM_STACK}) |
16 | | - . "scripts/terraform/terraform.lib.sh"; \ |
17 | | - terraform-${cmd} # 'dir' and 'opts' are accessible by the function as environment variables, if set |
| 12 | +terraform-fmt-check: # Check Terraform module formatting @Quality |
| 13 | + # Example: make terraform-fmt-check |
| 14 | + @cd infrastructure && terraform fmt -check -recursive modules |
18 | 15 |
|
19 | | -# ============================================================================== |
20 | | -# Quality checks - please DO NOT edit this section! |
| 16 | +terraform-validate: # Validate a specific Terraform module - mandatory: module=[module_name] @Quality |
| 17 | + # Example: make terraform-validate module=mymodule |
| 18 | + # Note: Validation does not require environment/group as it checks syntax only |
| 19 | + cd infrastructure/modules/$(module) && \ |
| 20 | + terraform init -backend=false && \ |
| 21 | + terraform validate |
21 | 22 |
|
22 | | -terraform-shellscript-lint: # Lint all Terraform module shell scripts @Quality |
23 | | - for file in $$(find scripts/terraform -type f -name "*.sh"); do |
24 | | - file=$${file} scripts/shellscript-linter.sh |
| 23 | +terraform-validate-all: # Validate all Terraform modules @Quality |
| 24 | + # Example: make terraform-validate-all |
| 25 | + @for dir in infrastructure/modules/*; do \ |
| 26 | + if [ -d "$$dir" ]; then \ |
| 27 | + echo "Validating $$(basename $$dir)..."; \ |
| 28 | + temp_provider=false; \ |
| 29 | + if grep -q "configuration_aliases.*us-east-1" "$$dir/versions.tf" 2>/dev/null; then \ |
| 30 | + echo "provider \"aws\" { alias = \"us-east-1\"; region = \"us-east-1\" }" > "$$dir/.tmp_providers.tf"; \ |
| 31 | + temp_provider=true; \ |
| 32 | + fi; \ |
| 33 | + cd $$dir && \ |
| 34 | + terraform init -backend=false && \ |
| 35 | + terraform validate; \ |
| 36 | + validation_result=$$?; \ |
| 37 | + cd - > /dev/null; \ |
| 38 | + if [ "$$temp_provider" = "true" ]; then \ |
| 39 | + rm -f "$$dir/.tmp_providers.tf"; \ |
| 40 | + fi; \ |
| 41 | + if [ $$validation_result -ne 0 ]; then \ |
| 42 | + exit $$validation_result; \ |
| 43 | + fi; \ |
| 44 | + fi; \ |
25 | 45 | done |
26 | 46 |
|
27 | | -terraform-sec: # TFSEC check against Terraform files - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform fmt command, default is '-recursive'] @Quality |
28 | | - tfsec infrastructure/modules \ |
29 | | - --force-all-dirs \ |
30 | | - --exclude-downloaded-modules \ |
31 | | - --config-file scripts/config/tfsec.yaml |
| 47 | +terraform-sec: # Run Trivy IaC security scanning on Terraform modules @Quality |
| 48 | + # Example: make terraform-sec |
| 49 | + ./scripts/terraform/trivy-scan.sh --mode iac infrastructure/modules |
32 | 50 |
|
33 | | -terraform-docs: # Terraform-docs check against Terraform files - optional: terraform_dir|dir=[path to a directory where the command will be executed, relative to the project's top-level directory, default is one of the module variables or the example directory, if not set], terraform_opts|opts=[options to pass to the Terraform fmt command, default is '-recursive'] @Quality |
34 | | - for dir in ./infrastructure/modules/*; do \ |
35 | | - if [ -d "$$dir" ]; then \ |
36 | | - ./scripts/terraform/terraform-docs.sh $$dir; \ |
37 | | - fi \ |
38 | | - done |
| 51 | +terraform-docs: # Generate Terraform module documentation - optional: module=[specific module, or all if omitted] @Quality |
| 52 | + # Example: make terraform-docs module=mymodule |
| 53 | + # Example: make terraform-docs (generates for all modules) |
| 54 | + @if [ -n "$(module)" ]; then \ |
| 55 | + ./scripts/terraform/terraform-docs.sh infrastructure/modules/$(module); \ |
| 56 | + else \ |
| 57 | + for dir in infrastructure/modules/*; do \ |
| 58 | + if [ -d "$$dir" ]; then \ |
| 59 | + ./scripts/terraform/terraform-docs.sh $$dir; \ |
| 60 | + fi; \ |
| 61 | + done; \ |
| 62 | + fi |
39 | 63 |
|
40 | 64 | # ============================================================================== |
41 | | -# Configuration - please DO NOT edit this section! |
| 65 | +# Cleanup |
| 66 | + |
| 67 | +clean:: # Remove Terraform build artifacts and cache @Operations |
| 68 | + # Example: make clean |
| 69 | + rm -rf infrastructure/modules/*/.terraform |
| 70 | + rm -rf infrastructure/modules/*/.terraform.lock.hcl |
42 | 71 |
|
43 | | -terraform-install: # Install Terraform @Installation |
| 72 | +# ============================================================================== |
| 73 | +# Installation |
| 74 | + |
| 75 | +terraform-install: # Install Terraform using asdf @Installation |
| 76 | + # Example: make terraform-install |
44 | 77 | make _install-dependency name="terraform" |
45 | 78 |
|
46 | 79 | # ============================================================================== |
47 | 80 |
|
48 | 81 | ${VERBOSE}.SILENT: \ |
49 | | - _terraform \ |
50 | 82 | clean \ |
51 | | - terraform-apply \ |
52 | | - terraform-destroy \ |
53 | | - terraform-example-clean \ |
54 | | - terraform-example-destroy-aws-infrastructure \ |
55 | | - terraform-example-provision-aws-infrastructure \ |
56 | | - terraform-fmt \ |
57 | 83 | terraform-docs \ |
58 | | - terraform-init \ |
| 84 | + terraform-fmt \ |
| 85 | + terraform-fmt-check \ |
59 | 86 | terraform-install \ |
60 | | - terraform-plan \ |
61 | | - terraform-shellscript-lint \ |
| 87 | + terraform-sec \ |
62 | 88 | terraform-validate \ |
| 89 | + terraform-validate-all |
0 commit comments