This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
159 lines (134 loc) · 6.11 KB
/
Makefile
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
.ONESHELL:
.PHONY: all help install check precommit tfswitch tfinit tfplan tfapply tfproviders tfreconfigure tfcook lint secure clean
.DEFAULT_GOAL = help
# Shell and Environment
SHELL := /bin/zsh
include .env
export
# Settings
APPNAME = wardenwombat
p = infrastructure/terraform
DEBUG_MODE = false
# Binary and Variables
BLUE=\033[0;34m
NC=\033[0m
SRC=.
ERRORCHECK="Not all packages installed, try running `make install`. Exiting..."
CHECKTF := $(shell terraform --version 2>/dev/null)
CHECKTFSWITCH := $(shell tfswitch --version 2>/dev/null)
CHECKTFLINT := $(shell tflint --version 2>/dev/null)
CHECKTFSEC := $(shell tfsec --version 2>/dev/null)
CHECKTFORM := $(shell terraformer --version 2>/dev/null)
TERRAFORMBINARY := $(shell which terraform)
header:
@echo -e "$(BLUE)--------------------------------------------------"
@echo -e "🚀 Automated makefile helper for: $(APPNAME)"
@echo -e "--------------------------------------------------$(NC)"
@echo ' '
check: header
@echo 'Requirements Checks:'
# Check if Terraform is installed and ready to use
ifdef CHECKTF
@echo " ✅ terraform is installed ($(shell which terraform))"
else
@echo " ❌ terraform is not installed"
$(error "$(ERRORCHECK)")
endif
# Check if Terraform Switch is installed and ready to use
ifdef CHECKTFSWITCH
@echo " ✅ tfswitch is installed ($(shell which tfswitch))"
else
@echo " ❌ tfswitch is not installed"
$(error "$(ERRORCHECK)")
endif
# Check if Terraform Lint is installed and ready to use
ifdef CHECKTFLINT
@echo " ✅ tflint is installed ($(shell which tflint))"
else
@echo " ❌ tflint is not installed"
$(error "$(ERRORCHECK)")
endif
# Check if tfsec is installed and ready to use
ifdef CHECKTFSEC
@echo " ✅ tfsec is installed ($(shell which tfsec))"
else
@echo " ❌ tfsec is not installed"
$(error "$(ERRORCHECK)")
endif
@echo " "
help: check
@echo ' '
@echo 'Usage: '
@echo ' make install attempt to install required dependencies '
@echo ' make check check you have all required dependencies '
@echo ' make precommit run precommit lint and test '
@echo ' make tfswitch set correct version of terraform (tfswitch) '
@echo ' make tfinit initialise terraform requirements '
@echo ' make tfplan run terraform plans '
@echo ' make tfapply apply terraform on project folder '
@echo ' make tfproviders print providers for project folder '
@echo ' make tfreconfigure reconfigure terraform state '
@echo ' make lint lint and validate terraform project '
@echo ' make secure test project security '
@echo ' make clean remove all local caches '
@echo ' '
precommit: check
@echo -e "${BLUE}🛫 Running pre-commit scripts against source and test files...${NC}\n"
@pre-commit run --all-files
install:
@brew install pre-commit gawk terraform-docs tflint tfsec coreutils
@brew install warrensbox/tap/tfswitch
@brew install --HEAD terraformer
tfstage:
@if [ "$(DEBUG_MODE)" = "true" ]; then \
export TF_LOG=TRACE; \
else \
unset TF_LOG; \
fi; \
tfswitch: tfstage
@tfswitch --chdir=$(p) --latest
tfinit: tfstage
@$(TERRAFORMBINARY) -chdir=$(p) init -input=false -upgrade -backend-config=s3.tfvars
tfplan: tfstage
@$(TERRAFORMBINARY) -chdir=$(p) plan -input=false
tfapply: tfstage
@$(TERRAFORMBINARY) -chdir=$(p) apply -input=false -auto-approve
tfproviders: tfstage
@$(TERRAFORMBINARY) -chdir=$(p) providers
tfreconfigure: tfstage
@$(TERRAFORMBINARY) -chdir=$(p) init -migrate-state -backend-config=s3.tfvars
tfstateshow:
@$(TERRAFORMBINARY) -chdir=$(p) state show .terraform/terraform.tfstate
secure:
@tfsec $(p)
lint:
@$(TERRAFORMBINARY) -chdir=$(p) fmt -check=true -diff=true -recursive
@tflint --chdir=$(p) --color
tfcook: check
@tfswitch --chdir=infrastructure/.terraformer 1.6.4
@$(TERRAFORMBINARY) -chdir=infrastructure/.terraformer init -upgrade
@$(TERRAFORMBINARY) -chdir=infrastructure/.terraformer plan
# Copy any new packages over before we start the work with terraformer
# $(shell mkdir ~/.terraform.d/plugins/darwin_arm64)
# $(shell mkdir ~/.terraform.d/plugins/darwin_amd64)
# $(shell cp -n .terraformer/.terraform/providers/**/terraform-provider/darwin_arm64/* ~/.terraform.d/plugins/darwin_arm64/)
# $(shell cp -n .terraformer/.terraform/providers/**/terraform-provider/darwin_amd64/* ~/.terraform.d/plugins/darwin_amd64/)
# TODO: exec chmod+x /Users/vincentkoc/.terraform.d/plugins/darwin_amd64/terraform-provider-aws_v4.50.0_x5
@echo " "
@echo "$(BLUE)Note: You need to use AWS credentials in the CLI using 'export' commands."
@echo "You can test the CLI can access AWS using 'aws sts get-caller-identity'. $(NC)"
@echo "We are currently connected as: "
@echo "`aws sts get-caller-identity`"
@echo " "
@terraformer import aws --profile=default "" --regions=ap-southeast-2 --excludes=acm,alb,appsync,auto_scaling,batch,budgets,cloud9,cloudhsm,codebuild,codecommit,codedeploy,codepipelines,cognito,config,customer_gateway,datapipeline,devicefarm,docdb,eip,elastic_beanstalk,elasticache,elb,eni,es,firehose,iot,kinesis,kms,media_package,media_store,medialive,msk,nacl,rds,redshift,resourcegroups,route53,secretsmanager,securityhub,servicecatalog,sfn,sns,ssm,swf,transit_gateway,vpc_peering,vpn_connection,vpn_gateway,workspace,xray,identitystore,glue,efs,emr,ec2,ecr,ecs,eks,ecrpublic,ebs,waf,waf_regional,wafv2_cloudfront,wafv2_regional -r "*" -o "infrastructure/.terraformer/generated"
clean:
@rm -rf infrastructure/**/.terraform
@rm -rf infrastructure/**/.terraform.lock.hcl
awscfcreate:
@aws cloudformation create-stack --stack-name $(CFSTACKNAME) --template-body file://$(PWD)/$(CFTEMPLATE) --capabilities CAPABILITY_IAM
awscfdeploy:
@aws cloudformation deploy --stack-name $(CFSTACKNAME) --template-file $(CFTEMPLATE) --capabilities CAPABILITY_IAM
awscfupdate:
@aws cloudformation update-stack --stack-name $(CFSTACKNAME) --template-body file://$(CFTEMPLATE) --capabilities CAPABILITY_IAM
awscfdelete:
@aws cloudformation delete-stack --stack-name $(CFSTACKNAME)