Skip to content

Commit 98605e0

Browse files
Merge pull request #149 from NHSDigital/CCM-14510_FixTestAndTerraformMakTargets
CCM-14510 Fix Test and TF Make Targets
2 parents f89fb8b + dff3b57 commit 98605e0

11 files changed

Lines changed: 116 additions & 126 deletions

File tree

.github/actions/lint-terraform/action.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ inputs:
77
runs:
88
using: "composite"
99
steps:
10+
- name: "Install Terraform binary"
11+
shell: bash
12+
run: |
13+
asdf plugin add terraform || true
14+
asdf install terraform || true
1015
- name: "Check Terraform format"
1116
shell: bash
1217
run: |
1318
check_only=true scripts/githooks/check-terraform-format.sh
1419
- name: "Validate Terraform"
1520
shell: bash
1621
run: |
17-
stacks=${{ inputs.root-modules }}
18-
for dir in $(find infrastructure/environments -maxdepth 1 -mindepth 1 -type d; echo ${stacks//,/$'\n'}); do
19-
dir=$dir opts='-backend=false' make terraform-init
20-
dir=$dir make terraform-validate
21-
done
22+
make terraform-validate-all

.github/workflows/stage-1-commit.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ jobs:
144144
steps:
145145
- name: "Checkout code"
146146
uses: actions/checkout@v4
147+
- name: "Setup ASDF"
148+
uses: asdf-vm/actions/setup@v4
147149
- name: "Lint Terraform"
148150
uses: ./.github/actions/lint-terraform
149151
trivy:

infrastructure/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Local .terraform directories
44
**/.terraform/*
5+
**/.terraform.lock.hcl
56

67
# .tfstate files
78
*.tfstate

infrastructure/modules/kms/kms_replica_key_replica.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_kms_replica_key" "replica" {
2-
provider = aws.us-east-1
3-
count = var.is_multi_region ? 1 : 0
2+
provider = aws.us-east-1
3+
count = var.is_multi_region ? 1 : 0
44

55
description = "Multi-Region replica key"
66
deletion_window_in_days = var.deletion_window
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
provider "aws" {
2+
region = var.region
3+
4+
allowed_account_ids = [
5+
var.aws_account_id,
6+
]
7+
8+
default_tags {
9+
tags = local.default_tags
10+
}
11+
}
12+
13+
provider "aws" {
14+
alias = "us-east-1"
15+
region = "us-east-1"
16+
17+
default_tags {
18+
tags = local.default_tags
19+
}
20+
21+
allowed_account_ids = [
22+
var.aws_account_id,
23+
]
24+
}

infrastructure/terraform/.gitignore

Lines changed: 0 additions & 67 deletions
This file was deleted.

scripts/docker/examples/python/assets/hello_world/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ MarkupSafe==2.1.3
88
pip==23.3
99
setuptools==65.5.1
1010
Werkzeug==3.0.6
11-
wheel==0.41.1
11+
wheel==0.46.2
1212
WTForms==3.0.1

scripts/githooks/check-terraform-format.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function main() {
2929
# check_only=[do not format, run check only]
3030
function terraform-fmt() {
3131

32-
local opts=
3332
if is-arg-true "$check_only"; then
34-
opts="-check"
33+
make terraform-fmt-check
34+
else
35+
make terraform-fmt
3536
fi
36-
opts=$opts make terraform-fmt
3737
}
3838

3939
# ==============================================================================

scripts/init.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ _install-dependency: # Install asdf dependency - mandatory: name=[listed in the
4747

4848
_install-dependencies: # Install all the dependencies listed in .tool-versions
4949
for plugin in $$(grep ^[a-z] .tool-versions | sed 's/[[:space:]].*//'); do
50-
make _install-dependency name="$${plugin}"
50+
$(MAKE) _install-dependency name=$${plugin}; \
5151
done
5252

5353
clean:: # Remove all generated and temporary files (common) @Operations

scripts/terraform/terraform.mk

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,89 @@
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/
24

35
# ==============================================================================
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
67

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
1111

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
1815

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
2122

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; \
2545
done
2646

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
3250

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
3963

4064
# ==============================================================================
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
4271

43-
terraform-install: # Install Terraform @Installation
72+
# ==============================================================================
73+
# Installation
74+
75+
terraform-install: # Install Terraform using asdf @Installation
76+
# Example: make terraform-install
4477
make _install-dependency name="terraform"
4578

4679
# ==============================================================================
4780

4881
${VERBOSE}.SILENT: \
49-
_terraform \
5082
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 \
5783
terraform-docs \
58-
terraform-init \
84+
terraform-fmt \
85+
terraform-fmt-check \
5986
terraform-install \
60-
terraform-plan \
61-
terraform-shellscript-lint \
87+
terraform-sec \
6288
terraform-validate \
89+
terraform-validate-all

0 commit comments

Comments
 (0)