-
Notifications
You must be signed in to change notification settings - Fork 1
/
Run-AzTerraform.ps1
663 lines (582 loc) · 23.6 KB
/
Run-AzTerraform.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<#
.\Run-AzTerraform.ps1 `
-RunTerraformInit true `
-RunTerraformPlan true `
-RunTerraformApply false `
-RunTrivy true `
-RunCheckov true `
-RunTerraformCompliance true `
-TerraformCodeLocation "examples/module-development" `
-BackendStorageSubscriptionId $Env:BACKEND_STORAGE_SUBSCRIPTION_ID `
-BackendStorageAccountRgName $Env:BACKEND_STORAGE_RESOURCE_GROUP_NAME `
-BackendStorageAccountName $Env:BACKEND_STORAGE_ACCOUNT_NAME `
-BackendStorageAccountBlobContainerName $Env:BACKEND_STORAGE_ACCOUNT_BLOB_CONTAINER_NAME `
-BackendStorageAccountBlobStatefileName "lbd-uks-prd-test-build"
#>
<#
.\Run-AzTerraform.ps1 `
-RunTerraformInit true `
-RunTerraformPlan false `
-RunTerraformPlanDestroy true `
-RunTerraformDestroy true `
-TerraformCodeLocation "examples/module-development" `
-BackendStorageSubscriptionId $Env:BACKEND_STORAGE_SUBSCRIPTION_ID `
-BackendStorageAccountRgName $Env:BACKEND_STORAGE_RESOURCE_GROUP_NAME `
-BackendStorageAccountName $Env:BACKEND_STORAGE_ACCOUNT_NAME `
-BackendStorageAccountBlobContainerName $Env:BACKEND_STORAGE_ACCOUNT_BLOB_CONTAINER_NAME `
-BackendStorageAccountBlobStatefileName "lbd-uks-prd-test-build"
#>
param (
[string]$RunTerraformInit = "true",
[string]$RunTerraformPlan = "true",
[string]$RunTerraformPlanDestroy = "false",
[string]$RunTerraformApply = "false",
[string]$RunTerraformDestroy = "false",
[bool]$DebugMode = $false,
[string]$DeletePlanFiles = "true",
[string]$TerraformVersion = "latest",
[string]$RunTrivy = "true",
[string]$RunTerraformCompliance = "false",
[string]$TerraformCompliancePolicyFiles = "git:https://github.com/libre-devops/azure-naming-convention.git//?ref=main",
[string]$RunCheckov = "false",
[Parameter(Mandatory = $true)]
[string]$TerraformCodeLocation,
[Parameter(Mandatory = $true)]
[string]$BackendStorageSubscriptionId,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountRgName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountBlobContainerName,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountBlobStatefileName
)
try
{
$ErrorActionPreference = 'Stop'
$CurrentWorkingDirectory = (Get-Location).path
$TerraformCodePath = Join-Path -Path $CurrentWorkingDirectory -ChildPath $TerraformCodeLocation
# Enable debug mode if DebugMode is set to $true
if ($DebugMode)
{
$DebugPreference = "Continue"
$Env:TF_LOG = "DEBUG"
}
else
{
$DebugPreference = "SilentlyContinue"
}
function Convert-ToBoolean($value)
{
$valueLower = $value.ToLower()
if ($valueLower -eq "true")
{
return $true
}
elseif ($valueLower -eq "false")
{
return $false
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Invalid value - $value. Exiting."
exit 1
}
}
# Function to check if tenv is installed
function Test-TenvExists
{
try
{
$tenvPath = Get-Command tenv -ErrorAction Stop
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Tenv found at: $( $tenvPath.Source )" -ForegroundColor Green
if ($TerraformVersion -ne 'default')
{
Write-Host "[$( $MyInvocation.MyCommand.Name )] Info: Desired terraform version is $TerraformVersion, attempting to install now" -ForegroundColor Green
tenv tf install $TerraformVersion --verbose
tenv tf use $TerraformVersion --verbose
}
}
catch
{
Write-Warning "[$( $MyInvocation.MyCommand.Name )] Warning: tenv is not installed or not in PATH. Skipping version checking."
}
}
function Run-Trivy
{
[CmdletBinding()]
param (
[string]$PlanJsonFile = "tfplan.json",
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
try
{
$trivyPath = Get-Command trivy -ErrorAction Stop
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Trivy found at: $( $trivyPath.Source ), running now..." -ForegroundColor Green
trivy config $PlanJsonFile --exit-code 0 # soft fail
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Trivy is not installed or not in PATH. Exiting."
exit 1
}
}
function Run-Checkov
{
[CmdletBinding()]
param (
[string]$PlanJsonFile = "tfplan.json",
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
try
{
$checkovPath = Get-Command checkov -ErrorAction Stop
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Checkov found at: $( $checkovPath.Source ), running now..." -ForegroundColor Green
checkov -s -f $PlanJsonFile
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Checkov is not installed or not in PATH. Exiting."
exit 1
}
}
function Run-TerraformCompliance
{
[CmdletBinding()]
param (
[string]$PlanJsonFile = "tfplan.json",
[string]$PolicyFiles = $TerraformCompliancePolicyFiles,
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
try
{
$terraformCompliancePath = Get-Command terraform-compliance -ErrorAction Stop
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Terraform-compliance found at: $( $terraformCompliancePath.Source ), running now..." -ForegroundColor Green
terraform-compliance -p $PlanJsonFile -f $PolicyFiles --no-failure
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform-Compliance is not installed or not in PATH. Exiting."
exit 1
}
}
# Function to check if Terraform is installed
function Test-TerraformExists
{
try
{
$terraformPath = Get-Command terraform -ErrorAction Stop
Write-Host "[$( $MyInvocation.MyCommand.Name )] Success: Terraform found at: $( $terraformPath.Source )" -ForegroundColor Green
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform is not installed or not in PATH. Exiting."
exit 1
}
}
function Assert-AzStorageContainer
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$StorageAccountSubscription,
[Parameter(Mandatory = $true)]
[string]$StorageAccountName,
[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,
[Parameter(Mandatory = $true)]
[string]$ContainerName
)
begin {
try
{
$azureAplicationId = $Env:ARM_CLIENT_ID
$azureTenantId = $Env:ARM_TENANT_ID
$azurePassword = ConvertTo-SecureString $Env:ARM_CLIENT_SECRET -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId, $azurePassword)
Connect-AzAccount -ServicePrincipal -Credential $psCred -Tenant $azureTenantId | Out-Null
Write-Host "Info: Connected to AzAccount using Powershell" -ForegroundColor Yellow
# Set the subscription context
Set-AzContext -SubscriptionId $StorageAccountSubscription | Out-Null
# Get the Storage Account
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error in setting up the Azure context: $_"
return
}
}
process {
try
{
# Create a storage context using OAuth token
$ctx = New-AzStorageContext -StorageAccountName $storageAccount.StorageAccountName -UseConnectedAccount
# Check if the Blob Container Exists
$container = Get-AzStorageContainer -Name $ContainerName -Context $ctx -ErrorAction SilentlyContinue
# Create the Container if it Doesn't Exist
if ($null -eq $container)
{
New-AzStorageContainer -Name $ContainerName -Context $ctx
Write-Host "Success: Container '$ContainerName' created." -ForegroundColor Green
}
else
{
Write-Host "Info: Container '$ContainerName' already exists." -ForegroundColor Yellow
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error in processing the container creation: $_"
}
}
end {
Write-Host "Operation completed, removing PowerShell Context"
Disconnect-AzAccount | Out-Null
}
}
function Invoke-TerraformInit
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$BackendStorageSubscriptionId,
[Parameter(Mandatory = $true)]
[string]$BackendStorageAccountName,
[Parameter(Mandatory = $true)]
[string]$WorkingDirectory
)
Begin {
# Initial setup and variable declarations
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Initializing Terraform..."
$BackendStorageAccountBlobContainerName = $BackendStorageAccountBlobContainerName
$BackendStorageAccountRgName = $BackendStorageAccountRgName
Assert-AzStorageContainer `
-StorageAccountSubscription $BackendStorageSubscriptionId `
-StorageAccountName $BackendStorageAccountName `
-ResourceGroupName $BackendStorageAccountRgName `
-ContainerName $BackendStorageAccountBlobContainerName
}
Process {
try
{
$terraformCache = Join-Path -Path $WorkingDirectory -ChildPath ".terraform"
if (Test-Path -Path $terraformCache)
{
try
{
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Attempting to remove : $terraformCache"
Remove-Item -Force $terraformCache -Recurse -Confirm:$false
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Failed to remove .terraform folder: $_"
exit 1
}
}
# Change to the specified working directory
Set-Location -Path $WorkingDirectory
# Construct the backend config parameters
$backendConfigParams = @(
"-backend-config=subscription_id=$BackendStorageSubscriptionId",
"-backend-config=storage_account_name=$BackendStorageAccountName",
"-backend-config=resource_group_name=$BackendStorageAccountRgName",
"-backend-config=container_name=$BackendStorageAccountBlobContainerName"
"-backend-config=key=$BackendStorageAccountBlobStatefileName"
)
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Backend config params are: $backendConfigParams"
# Run terraform init with the constructed parameters
terraform init @backendConfigParams | Out-Host
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Last exit code is $LASTEXITCODE"
# Check if terraform init was successful
if ($LASTEXITCODE -ne 0)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform init failed with exit code $LASTEXITCODE"
exit 1
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform init failed with exception: $_"
exit 1
}
}
End {
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Terraform initialization completed."
}
}
function Invoke-TerraformPlan
{
[CmdletBinding()]
param (
[string]$WorkingDirectory = $WorkingDirectory,
[bool]$RunTerraformPlan = $true
)
Begin {
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Begin: Initializing Terraform Plan in $WorkingDirectory"
}
Process {
if ($RunTerraformPlan)
{
Write-Host "[$( $MyInvocation.MyCommand.Name )] Info: Running Terraform Plan in $WorkingDirectory" -ForegroundColor Green
try
{
Set-Location -Path $WorkingDirectory
terraform plan -out tfplan.plan | Out-Host
if (Test-Path tfplan.plan)
{
terraform show -json tfplan.plan | Tee-Object -FilePath tfplan.json | Out-Null
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform plan file not created"
exit 1
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error encountered during Terraform plan: $_"
exit 1
}
}
}
End {
Write-Debug "[$( $MyInvocation.MyCommand.Name )] End: Completed Terraform Plan execution"
}
}
# Function to execute Terraform plan for destroy
function Invoke-TerraformPlanDestroy
{
[CmdletBinding()]
param (
[string]$WorkingDirectory = $WorkingDirectory,
[bool]$RunTerraformPlanDestroy = $true
)
Begin {
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Begin: Preparing to execute Terraform Plan Destroy in $WorkingDirectory"
}
Process {
if ($RunTerraformPlanDestroy)
{
try
{
Write-Host "[$( $MyInvocation.MyCommand.Name )] Info: Running Terraform Plan Destroy in $WorkingDirectory" -ForegroundColor Yellow
Set-Location -Path $WorkingDirectory
terraform plan -destroy -out tfplan.plan | Out-Host
if (Test-Path tfplan.plan)
{
terraform show -json tfplan.plan | Tee-Object -FilePath tfplan.json | Out-Null
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform plan file not created"
exit 1
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error encountered during Terraform Plan Destroy: $_"
exit 1
}
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error encountered during Terraform Plan Destroy or internal script error occured: $_"
exit 1
}
}
End {
Write-Debug "[$( $MyInvocation.MyCommand.Name )] End: Completed execution of Terraform Plan Destroy"
}
}
# Function to execute Terraform apply
function Invoke-TerraformApply
{
if ($RunTerraformApply -eq $true)
{
try
{
Write-Host "[$( $MyInvocation.MyCommand.Name )] Info: Running Terraform Apply in $WorkingDirectory" -ForegroundColor Yellow
if (Test-Path tfplan.plan)
{
terraform apply -auto-approve tfplan.plan | Out-Host
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform plan file not present for terraform apply"
return $false
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform Apply failed"
return $false
}
}
}
# Function to execute Terraform destroy
function Invoke-TerraformDestroy
{
if ($RunTerraformDestroy -eq $true)
{
try
{
Write-Host "[$( $MyInvocation.MyCommand.Name )] Info: Running Terraform Destroy in $WorkingDirectory" -ForegroundColor Yellow
if (Test-Path tfplan.plan)
{
terraform apply -auto-approve tfplan.plan | Out-Host
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform plan file not present for terraform destroy"
return $false
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform Destroy failed"
return $false
}
}
}
# Convert string parameters to boolean
$ConvertedRunTerraformInit = Convert-ToBoolean $RunTerraformInit
$ConvertedRunTerraformPlan = Convert-ToBoolean $RunTerraformPlan
$ConvertedRunTerraformPlanDestroy = Convert-ToBoolean $RunTerraformPlanDestroy
$ConvertedRunTerraformApply = Convert-ToBoolean $RunTerraformApply
$ConvertedRunTerraformDestroy = Convert-ToBoolean $RunTerraformDestroy
$ConvertedDeletePlanFiles = Convert-ToBoolean $DeletePlanFiles
$ConvertedRunTrivy = Convert-ToBoolean $RunTrivy
$ConvertedRunTerraformCompliance = Convert-ToBoolean $RunTerraformCompliance
$ConvertedRunCheckov = Convert-ToBoolean $RunCheckov
# Diagnostic output
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedRunTerraformInit: $ConvertedRunTerraformInit"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedRunTerraformPlan: $ConvertedRunTerraformPlan"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedRunTerraformPlanDestroy: $ConvertedRunTerraformPlanDestroy"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedRunTerraformApply: $ConvertedRunTerraformApply"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedRunTerraformDestroy: $ConvertedRunTerraformDestroy"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: DebugMode: $DebugMode"
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: ConvertedDeletePlanFiles: $ConvertedDeletePlanFiles"
# Chicken and Egg checker
if (-not$ConvertedRunTerraformInit -and ($ConvertedRunTerraformPlan -or $ConvertedRunTerraformPlanDestroy -or $ConvertedRunTerraformApply -or $ConvertedRunTerraformDestroy))
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform init must be run before executing plan, plan destroy, apply, or destroy commands."
exit 1
}
if ($ConvertedRunTerraformPlan -eq $true -and $ConvertedRunTerraformPlanDestroy -eq $true)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Both Terraform Plan and Terraform Plan Destroy cannot be true at the same time"
exit 1
}
if ($ConvertedRunTerraformApply -eq $true -and $ConvertedRunTerraformDestroy -eq $true)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Both Terraform Apply and Terraform Destroy cannot be true at the same time"
exit 1
}
if ($ConvertedRunTerraformPlan -eq $false -and $ConvertedRunTerraformApply -eq $true)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: You must run terraform plan and terraform apply together to use this script"
exit 1
}
if ($ConvertedRunTerraformPlanDestroy -eq $false -and $ConvertedRunTerraformDestroy -eq $true)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: You must run terraform plan destroy and terraform destroy together to use this script"
exit 1
}
try
{
# Initial Terraform setup
Test-TenvExists
Test-TerraformExists
$WorkingDirectory = $TerraformCodePath
# Terraform Init
if ($ConvertedRunTerraformInit)
{
Invoke-TerraformInit `
-WorkingDirectory $WorkingDirectory `
-BackendStorageAccountName $BackendStorageAccountName `
-BackendStorageSubscriptionId $BackendStorageSubscriptionId
$InvokeTerraformInitSuccessful = ($LASTEXITCODE -eq 0)
}
else
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: Terraform initialization failed."
}
# Conditional execution based on parameters
if ($InvokeTerraformInitSuccessful -and $ConvertedRunTerraformPlan -and -not$ConvertedRunTerraformPlanDestroyonvRunTerraformPlanDestroy)
{
Invoke-TerraformPlan -WorkingDirectory $WorkingDirectory
$InvokeTerraformPlanSuccessful = ($LASTEXITCODE -eq 0)
if ($ConvertedRunTrivy -and $InvokeTerraformPlanSuccessful)
{
Run-Trivy -WorkingDirectory $WorkingDirectory
}
if ($ConvertedRunCheckov -and $InvokeTerraformPlanSuccessful)
{
Run-Checkov -WorkingDirectory $WorkingDirectory
}
if ($ConvertedRunTerraformCompliance -and $InvokeTerraformPlanSuccessful)
{
Run-TerraformCompliance -WorkingDirectory $WorkingDirectory
}
}
if ($InvokeTerraformInitSuccessful -and $ConvertedRunTerraformPlanDestroy -and -not$ConvertedRunTerraformPlan)
{
Invoke-TerraformPlanDestroy -WorkingDirectory $WorkingDirectory
$InvokeTerraformPlanDestroySuccessful = ($LASTEXITCODE -eq 0)
}
if ($InvokeTerraformInitSuccessful -and $ConvertedRunTerraformApply -and $InvokeTerraformPlanSuccessful)
{
Invoke-TerraformApply
$InvokeTerraformApplySuccessful = ($LASTEXITCODE -eq 0)
if (-not$InvokeTerraformApplySuccessful)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: An error occured during terraform apply command"
exit 1
}
}
if ($ConvertedRunTerraformDestroy -and $InvokeTerraformPlanDestroySuccessful)
{
Invoke-TerraformDestroy
$InvokeTerraformDestroySuccessful = ($LASTEXITCODE -eq 0)
if (-not$InvokeTerraformDestroySuccessful)
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: An error occured during terraform destroy command"
exit 1
}
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: in script execution: $_"
exit 1
}
}
catch
{
throw "[$( $MyInvocation.MyCommand.Name )] Error: An error has occured in the script: $_"
exit 1
}
finally
{
if ($DeletePlanFiles -eq $true)
{
$planFile = "tfplan.plan"
if (Test-Path $planFile)
{
Remove-Item -Path $planFile -Force -ErrorAction Stop
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Deleted $planFile"
}
$planJson = "tfplan.json"
if (Test-Path $planJson)
{
Remove-Item -Path $planJson -Force -ErrorAction Stop
Write-Debug "[$( $MyInvocation.MyCommand.Name )] Debug: Deleted $planJson"
}
}
Set-Location $CurrentWorkingDirectory
}