-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskfile.yml
More file actions
179 lines (158 loc) · 4.16 KB
/
Taskfile.yml
File metadata and controls
179 lines (158 loc) · 4.16 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
version: '3'
vars:
TERRAFORM_VERSION: 1.5.0
AWS_REGION: '{{.AWS_REGION | default "us-west-2"}}'
ENVIRONMENT: '{{.ENVIRONMENT | default "dev"}}'
env:
AWS_REGION: '{{.AWS_REGION}}'
TF_WORKSPACE: '{{.ENVIRONMENT}}'
tasks:
default:
cmds:
- task --list
silent: true
check-tools:
desc: Check if required tools are installed
cmds:
- |
missing_tools=()
for tool in terraform tflint checkov; do
if ! command -v $tool >/dev/null 2>&1; then
missing_tools+=($tool)
fi
done
if [ ${#missing_tools[@]} -ne 0 ]; then
echo "Missing required tools: ${missing_tools[*]}"
echo "Please run 'task setup' to install missing tools"
exit 1
fi
silent: true
init:
desc: Initialize Terraform working directory
deps: [check-tools]
cmds:
- terraform init
- tflint --init
- terraform workspace select {{.ENVIRONMENT}} 2>/dev/null || terraform workspace new {{.ENVIRONMENT}}
validate:
desc: Validate Terraform configurations
deps: [check-tools, init]
cmds:
- terraform fmt -check -diff -recursive
- terraform validate
- tflint
- checkov -d .
plan:
desc: Create Terraform plan
deps: [validate]
cmds:
- terraform plan -out=tfplan
env:
TF_VAR_environment: '{{.ENVIRONMENT}}'
apply:
desc: Apply Terraform plan
cmds:
- terraform apply tfplan
preconditions:
- test -f tfplan
destroy:
desc: Destroy infrastructure
interactive: true
cmds:
- echo "Are you sure you want to destroy {{.ENVIRONMENT}} environment? (y/n)"
- read -r response
- |
if [ "$response" = "y" ]; then
terraform destroy -auto-approve
else
echo "Destroy cancelled"
exit 1
fi
env:
TF_VAR_environment: '{{.ENVIRONMENT}}'
clean:
desc: Clean up generated files
cmds:
- rm -rf .terraform tfplan
- find . -type f -name ".terraform.lock.hcl" -delete
- find . -type f -name "terraform.tfstate*" -delete
lint:
desc: Run all linters
deps: [init]
cmds:
- task: lint:tf
- task: lint:tflint
- task: lint:checkov
lint:tf:
desc: Run terraform fmt
cmds:
- terraform fmt -recursive
lint:tflint:
desc: Run tflint
deps: [init]
cmds:
- tflint --recursive
lint:checkov:
desc: Run checkov
cmds:
- checkov -d .
docs:
desc: Generate Terraform documentation
cmds:
- terraform-docs markdown . > TERRAFORM.md
test:
desc: Run tests
cmds:
- terratest/run_tests.sh
sources:
- "**/*.tf"
- "test/**/*"
generates:
- test/reports/**/*
setup:
desc: Install required tools
cmds:
- |
if ! command -v tflint >/dev/null 2>&1; then
curl -L "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" -o tflint.zip
unzip tflint.zip
sudo mv tflint /usr/local/bin/
rm tflint.zip
fi
- |
if ! command -v terraform-docs >/dev/null 2>&1; then
curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/latest/download/terraform-docs-linux-amd64.tar.gz
tar -xzf terraform-docs.tar.gz
sudo mv terraform-docs /usr/local/bin/
rm terraform-docs.tar.gz
fi
- |
if ! command -v checkov >/dev/null 2>&1; then
pip3 install checkov
fi
pre-commit:
desc: Run pre-commit checks
cmds:
- task: validate
- task: lint
- task: docs
deploy:
desc: Deploy to specific environment
cmds:
- task: init
- task: plan
- task: apply
env:
TF_VAR_environment: '{{.ENVIRONMENT}}'
cost-estimate:
desc: Estimate infrastructure costs
cmds:
- infracost breakdown --path .
security-scan:
desc: Run security scans
cmds:
- task: lint:checkov
- terraform plan -out=tfplan
- terraform show -json tfplan | jq '.' > tfplan.json
- checkov -f tfplan.json
- rm tfplan.json