Skip to content

Makefile Examples

Terraform Makefile for Stacks

Use this Makefile if you follow stack-based Terraform layout

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
BACKEND_PATH=../config/XXXXXXXXXX/eu-central-1/s3.config

all: help

prepare:
    echo 'installing brew packages'
    brew update
    brew install tfsec tfenv tflint terraform-docs pre-commit coreutils checkov
    brew upgrade tfsec tfenv tflint terraform-docs pre-commit coreutils checkov

    echo 'installing pre-commit hooks'
    pre-commit install

    echo 'setting pre-commit hooks to auto-install on clone in the future'
    git config --global init.templateDir ~/.git-template
    pre-commit init-templatedir ~/.git-template

    echo 'installing terraform with tfenv'
    tfenv install min-required
    tfenv use min-required

    echo 'installing infracost'
    brew install infracost
    infracost register  

check-stack:
ifndef STACK
    $(error Please set STACK=[network-stack|data-stack])
endif

init: check-stack
    cd $(STACK) && \
    terraform init \
    -backend-config=$(BACKEND_PATH) \
    -backend-config="key=$(STACK).tfstate"

fmt:
    terraform fmt -recursive

validate: check-stack
    cd $(STACK) && \
    terraform validate

plan: check-stack
    cd $(STACK) && \
    terraform plan

deploy: check-stack
    cd $(STACK) && \
    terraform apply -auto-approve

output: check-stack
    cd $(STACK) && \
    terraform output

docs: check-stack
    cd $(STACK) && \
    terraform-docs markdown . --config=../.terraform-docs.yml >> README.md

tflint: check-stack
    cd $(STACK) && \
    tflint --config=../.tflint.hcl --module --deep

tfsec: check-stack
    cd $(STACK) && \
    tfsec .

checkov: check-stack
    cd $(STACK) && \
    checkov --framework terraform --output github_failed_only --soft-fail --directory . --file $(BACKEND_PATH)

tfcompliance: check-stack
    cd $(STACK) && \
    terraform plan -out=plan.out && \
    terraform-compliance --no-failure \
    -p plan.out \
    -f ../compliance
    rm -f plan.out
    rm -f plan.out.json

cost: check-stack
    cd $(STACK) && \
    infracost \
    --show-skipped \
    --tfdir . \
    --use-tfstate      

kubeconfig:
    cd app-stack && \
    terraform output kubectl_config
    export KUBECONFIG="${PWD}/kubeconfig_pai-data-foundation"    

help:
    @echo ">> help list:"
    @echo "  make prepare"
    @echo "  STACK=network-stack make init"
    @echo "  make fmt"
    @echo "  STACK=network-stack make validate"
    @echo "  STACK=network-stack make plan"
    @echo "  STACK=network-stack make deploy"
    @echo "  STACK=network-stack make ouput"
    @echo "  STACK=network-stack make docs"
    @echo "  STACK=network-stack make tflint"
    @echo "  STACK=network-stack make tfsec"
    @echo "  STACK=network-stack make checkov"
    @echo "  STACK=network-stack make tfcompliance"
    @echo "  STACK=network-stack make cost"
    @echo "  STACK=app-stack make kubeconfig"

Terraform Makefile - Simple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
TERRAFORM_VERSION=0.14.3
REGION=eu-west-1
PROFILE=aes-shared 

install:
    pre-commit install && \
    tenv install $(TERRAFORM_VERSION)

fmt:
    terraform fmt

init:
    terraform init

validate:
    terraform validate

plan:
    terraform plan

apply:
    terraform apply -auto-approve

docs:
    terraform-docs markdown ./

test: validate
        tflint .

        tfsec .

cost:
    infracost .

kms:
    aws kms list-keys --region $(REGION)

aws-vault:
    aws-vault exec $(PROFILE)