generated from cyber-scot/terraform-module-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Run-Terraform.ps1
330 lines (285 loc) Β· 11.1 KB
/
Run-Terraform.ps1
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<#
.SYNOPSIS
Automates the execution of Terraform operations (init, plan, apply, destroy) with configurable parameters and includes Terraform version management using 'tfenv'.
.DESCRIPTION
This PowerShell script offers a parameter-driven approach to manage Terraform commands. It allows users to control the execution of Terraform init, plan, apply, and destroy operations. Additionally, it handles Terraform version management using 'tfenv', ensuring the desired version of Terraform is installed and used. The script is suitable for both automated environments and manual execution, providing options for debugging and plan file cleanup post-execution.
.PARAMETER RunTerraformInit
Executes 'terraform init' when set to 'true'.
.PARAMETER RunTerraformPlan
Executes 'terraform plan' when set to 'true'.
.PARAMETER RunTerraformPlanDestroy
Executes 'terraform plan' with the destroy option when set to 'true'.
.PARAMETER RunTerraformApply
Executes 'terraform apply' when set to 'true'.
.PARAMETER RunTerraformDestroy
Executes 'terraform destroy' when set to 'true'.
.PARAMETER WorkingDirectory
Specifies the directory where Terraform commands will be executed.
.PARAMETER DebugMode
Enables additional diagnostic output if set to 'true'.
.PARAMETER DeletePlanFiles
Determines whether to delete Terraform plan files after execution, set to 'true' to enable deletion.
.PARAMETER TerraformVersion
Specifies the version of Terraform to use, accepts 'latest' or a specific version number.
.EXAMPLE
.\Run-Terraform.ps1 -RunTerraformInit "true" -RunTerraformPlan "true" -RunTerraformApply "false" -RunTerraformDestroy "false" -DebugMode "false" -DeletePlanFiles "true" -TerraformVersion "latest"
Runs Terraform init and plan with the latest version of Terraform, without debug mode, and deletes plan files after execution.
.NOTES
Ensure Terraform or 'tfenv' is installed and accessible in the system path. The script is intended for use in a PowerShell environment. It's designed for flexibility and includes error handling to ensure smooth execution.
#>
param (
[string]$RunTerraformInit = "true",
[string]$RunTerraformPlan = "true",
[string]$RunTerraformPlanDestroy = "false",
[string]$RunTerraformApply = "false",
[string]$RunTerraformDestroy = "false",
[string]$WorkingDirectory = (Get-Location).Path,
[string]$DebugMode = "false",
[string]$DeletePlanFiles = "true",
[string]$TerraformVersion = "latest",
[Parameter(Mandatory = $true)]
[string]$TerraformStateName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageSubscriptionId,
[Parameter(Mandatory = $true)]
[string]$BackendStorageResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountBlobContainerName
)
# Function to check if Tfenv is installed
function Check-TfenvExists {
try {
$tfenvPath = Get-Command tfenv -ErrorAction Stop
Write-Host "Success: Tfenv found at: $($tfenvPath.Source)" -ForegroundColor Green
return $true
}
catch {
Write-Warning "Warning: Tfenv is not installed or not in PATH. Skipping version checking."
return $false
}
}
# Function to check if Terraform is installed
function Check-TerraformExists {
try {
$terraformPath = Get-Command terraform -ErrorAction Stop
Write-Host "Success: Terraform found at: $($terraformPath.Source)" -ForegroundColor Green
}
catch {
Write-Error "Error: Terraform is not installed or not in PATH. Exiting."
exit 1
}
}
# Function to ensure the desired version of Terraform is installed
function Ensure-TerraformVersion {
# Check if the specified version is already installed
$tfVersion = $TerraformVersion.ToLower()
if ($tfVersion -eq 'latest') {
Write-Host "Success: Terraform version is set to 'latest', running install and use" -ForegroundColor Green
tfenv install $tfVersion
tfenv use $tfVersion
}
else {
try {
Write-Information "Info: Installing Terraform version $Version using tfenv..."
tfenv install $Version
tfenv use $Version
Write-Host "Success: Installed and set Terraform version $Version" -ForegroundColor Green
}
catch {
Write-Error "Error: Failed to install Terraform version $Version"
exit 1
}
}
}
# Function to convert string to boolean
function Convert-ToBoolean($value) {
$valueLower = $value.ToLower()
if ($valueLower -eq "true") {
return $true
}
elseif ($valueLower -eq "false") {
return $false
}
else {
Write-Error "Error: Invalid value - $value. Exiting."
exit 1
}
}
$tfenvExists = Check-TfenvExists
if ($tfenvExists) {
Ensure-TerraformVersion -Version $TerraformVersion
}
Check-TerraformExists
# Convert string parameters to boolean
$RunTerraformInit = Convert-ToBoolean $RunTerraformInit
$RunTerraformPlan = Convert-ToBoolean $RunTerraformPlan
$RunTerraformPlanDestroy = Convert-ToBoolean $RunTerraformPlanDestroy
$RunTerraformApply = Convert-ToBoolean $RunTerraformApply
$RunTerraformDestroy = Convert-ToBoolean $RunTerraformDestroy
$DebugMode = Convert-ToBoolean $DebugMode
$DeletePlanFiles = Convert-ToBoolean $DeletePlanFiles
# Enable debug mode if DebugMode is set to $true
if ($DebugMode) {
$DebugPreference = "Continue"
}
# Diagnostic output
Write-Debug "RunTerraformInit: $RunTerraformInit"
Write-Debug "RunTerraformPlan: $RunTerraformPlan"
Write-Debug "RunTerraformPlanDestroy: $RunTerraformPlanDestroy"
Write-Debug "RunTerraformApply: $RunTerraformApply"
Write-Debug "RunTerraformDestroy: $RunTerraformDestroy"
Write-Debug "DebugMode: $DebugMode"
Write-Debug "DeletePlanFiles: $DeletePlanFiles"
if ($RunTerraformPlan -eq $true -and $RunTerraformPlanDestroy -eq $true) {
Write-Error "Error: Both Terraform Plan and Terraform Plan Destroy cannot be true at the same time"
exit 1
}
if ($RunTerraformApply -eq $true -and $RunTerraformDestroy -eq $true) {
Write-Error "Error: Both Terraform Apply and Terraform Destroy cannot be true at the same time"
exit 1
}
if ($RunTerraformPlan -eq $false -and $RunTerraformApply -eq $true) {
Write-Error "Error: You must run terraform plan and terraform apply together to use this script"
exit 1
}
if ($RunTerraformPlanDestroy -eq $false -and $RunTerraformDestroy -eq $true) {
Write-Error "Error: You must run terraform plan destroy and terraform destroy together to use this script"
exit 1
}
# Change to the specified working directory
try {
$CurrentWorkingDirectory = (Get-Location).path
Set-Location -Path $WorkingDirectory
}
catch {
Write-Error "Error: Unable to change to directory: $WorkingDirectory" -ForegroundColor Red
exit 1
}
function Run-TerraformInit {
if ($RunTerraformInit -eq $true) {
try {
Write-Host "Info: Running Terraform init in $WorkingDirectory" -ForegroundColor Green
# Construct the backend config parameters
$backendConfigParams = @(
"-backend-config=subscription_id=$BackendStorageSubscriptionId",
"-backend-config=resource_group_name=$BackendStorageResourceGroupName",
"-backend-config=storage_account_name=$BackendStorageAccountName",
"-backend-config=container_name=$BackendStorageAccountBlobContainerName",
"-backend-config=key=$TerraformStateName"
)
# Run terraform init with the constructed parameters
terraform init @backendConfigParams | Out-Host
return $true
}
catch {
Write-Error "Error: Terraform init failed" -ForegroundColor Red
return $false
}
}
}
# Function to execute Terraform plan
function Run-TerraformPlan {
if ($RunTerraformPlan -eq $true) {
Write-Host "Info: Running Terraform Plan in $WorkingDirectory" -ForegroundColor Green
terraform plan -out tfplan.plan | Out-Host
if (Test-Path tfplan.plan) {
terraform show -json tfplan.plan | Tee-Object -FilePath tfplan.json | Out-Null
return $true
}
else {
Write-Host "Error: Terraform plan file not created"
return $false
}
}
return $false
}
# Function to execute Terraform plan for destroy
function Run-TerraformPlanDestroy {
if ($RunTerraformPlanDestroy -eq $true) {
try {
Write-Host "Info: Running Terraform Plan Destroy in $WorkingDirectory" -ForegroundColor Yellow
terraform plan -destroy -out tfplan.plan
if (Test-Path tfplan.plan) {
terraform show -json tfplan.plan | Tee-Object -FilePath tfplan.json | Out-Null
return $true
}
else {
Write-Error "Error: Terraform plan file not created"
return $false
}
}
catch {
Write-Error "Error: Terraform Plan Destroy failed"
return $false
}
}
return $false
}
# Function to execute Terraform apply
function Run-TerraformApply {
if ($RunTerraformApply -eq $true) {
try {
Write-Host "Info: Running Terraform Apply in $WorkingDirectory" -ForegroundColor Yellow
if (Test-Path tfplan.plan) {
terraform apply -auto-approve tfplan.plan | Out-Host
return $true
}
else {
Write-Error "Error: Terraform plan file not present for terraform apply"
return $false
}
}
catch {
Write-Error "Error: Terraform Apply failed"
return $false
}
}
return $false
}
# Function to execute Terraform destroy
function Run-TerraformDestroy {
if ($RunTerraformDestroy -eq $true) {
try {
Write-Host "Info: Running Terraform Destroy in $WorkingDirectory" -ForegroundColor Yellow
if (Test-Path tfplan.plan) {
terraform apply -auto-approve tfplan.plan | Out-Host
return $true
}
else {
Write-Error "Error: Terraform plan file not present for terraform destroy"
return $false
}
}
catch {
Write-Error "Error: Terraform Destroy failed"
return $false
}
}
return $false
}
# Execution flow
if (Run-TerraformInit) {
$planSuccess = Run-TerraformPlan
$planDestroySuccess = Run-TerraformPlanDestroy
if ($planSuccess -and $RunTerraformApply -eq $true) {
Run-TerraformApply
}
if ($planDestroySuccess -and $RunTerraformDestroy -eq $true) {
Run-TerraformDestroy
}
}
if ($DeletePlanFiles -eq $true) {
$planFile = "tfplan.plan"
if (Test-Path $planFile) {
Remove-Item -Path $planFile -Force -ErrorAction Stop
Write-Debug "Deleted $planFile"
}
$planJson = "tfplan.json"
if (Test-Path $planJson) {
Remove-Item -Path $planJson -Force -ErrorAction Stop
Write-Debug "Deleted $planJson"
}
}
Set-Location $CurrentWorkingDirectory