-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathInvoke-Terraform.ps1
More file actions
182 lines (153 loc) · 7.43 KB
/
Invoke-Terraform.ps1
File metadata and controls
182 lines (153 loc) · 7.43 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
180
181
182
function Invoke-Terraform {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(Mandatory = $false)]
[string] $moduleFolderPath,
[Parameter(Mandatory = $false)]
[string] $tfvarsFileName = "",
[Parameter(Mandatory = $false)]
[switch] $autoApprove,
[Parameter(Mandatory = $false)]
[switch] $destroy,
[Parameter(Mandatory = $false)]
[string] $output = "",
[Parameter(Mandatory = $false)]
[string] $outputFilePath = "",
[Parameter(Mandatory = $false)]
[switch] $silent
)
if ($PSCmdlet.ShouldProcess("Apply Terraform", "modify")) {
# Check and Set Subscription ID
$removeSubscriptionId = $false
if($null -eq $env:ARM_SUBSCRIPTION_ID -or $env:ARM_SUBSCRIPTION_ID -eq "") {
Write-Verbose "Setting environment variable ARM_SUBSCRIPTION_ID"
$subscriptionId = $(az account show --query id -o tsv)
if($null -eq $subscriptionId -or $subscriptionId -eq "") {
Write-Error "Subscription ID not found. Please ensure you are logged in to Azure and have selected a subscription. Use 'az account show' to check."
return
}
$env:ARM_SUBSCRIPTION_ID = $subscriptionId
$removeSubscriptionId = $true
Write-Verbose "Environment variable ARM_SUBSCRIPTION_ID set to $subscriptionId"
}
terraform -chdir="$moduleFolderPath" init
$action = "apply"
if($destroy) {
$action = "destroy"
}
if(!$silent) {
Write-InformationColored "Terraform init has completed, now running the $action..." -ForegroundColor Green -NewLineBefore -InformationAction Continue
}
$planFileName = "tfplan"
# Start timer
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$StopWatch.Start()
$command = "terraform"
$arguments = @()
$arguments += "-chdir=$moduleFolderPath"
$arguments += "plan"
$arguments += "-out=$planFileName"
$arguments += "-input=false"
if($tfvarsFileName -ne "") {
$arguments += "-var-file=$tfvarsFileName"
}
if ($destroy) {
$arguments += "-destroy"
}
if(!$silent) {
Write-InformationColored "Running Plan Command for $action : $command $arguments" -ForegroundColor Green -NewLineBefore -InformationAction Continue
& $command $arguments
} else {
& $command $arguments | Write-Verbose
}
$exitCode = $LASTEXITCODE
# Stop and display timer
$StopWatch.Stop()
if(!$silent) {
Write-InformationColored "Time taken to complete Terraform plan:" -ForegroundColor Green -NewLineBefore -InformationAction Continue
}
$StopWatch.Elapsed | Format-Table
if($exitCode -ne 0) {
Write-InformationColored "Terraform plan for $action failed with exit code $exitCode. Please review the error and try again or raise an issue." -ForegroundColor Red -NewLineBefore -InformationAction Continue
throw "Terraform plan failed with exit code $exitCode. Please review the error and try again or raise an issue."
}
if(!$autoApprove) {
Write-InformationColored "Terraform plan has completed, please review the plan and confirm you wish to continue." -ForegroundColor Yellow -NewLineBefore -InformationAction Continue
$choices = [System.Management.Automation.Host.ChoiceDescription[]] @("&Yes", "&No")
$message = "Please confirm you wish to apply the plan."
$title = "Confirm Terraform plan"
$resultIndex = $host.ui.PromptForChoice($title, $message, $choices, 0)
if($resultIndex -eq 1) {
Write-InformationColored "You have chosen not to apply the plan. Exiting..." -ForegroundColor Red -NewLineBefore -InformationAction Continue
return
}
}
# Start timer
$StopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$StopWatch.Start()
$command = "terraform"
$arguments = @()
$arguments += "-chdir=$moduleFolderPath"
$arguments += "apply"
$arguments += "-auto-approve"
$arguments += "-input=false"
$arguments += "$planFileName"
if(!$silent) {
Write-InformationColored "Running Apply Command for $action : $command $arguments" -ForegroundColor Green -NewLineBefore -InformationAction Continue
& $command $arguments
} else {
& $command $arguments | Write-Verbose
}
$exitCode = $LASTEXITCODE
$currentAttempt = 0
$maxAttempts = 5
while($exitCode -ne 0 -and $currentAttempt -lt $maxAttempts) {
Write-InformationColored "Terraform $action failed with exit code $exitCode. This is likely a transient issue, so we are retrying..." -ForegroundColor Yellow -NewLineBefore -InformationAction Continue
$currentAttempt++
$command = "terraform"
$arguments = @()
$arguments += "-chdir=$moduleFolderPath"
$arguments += "apply"
$arguments += "-auto-approve"
$arguments += "-input=false"
if($tfvarsFileName -ne "") {
$arguments += "-var-file=$tfvarsFileName"
}
if ($destroy) {
$arguments += "-destroy"
}
Write-InformationColored "Running Apply Command for $action : $command $arguments" -ForegroundColor Green -NewLineBefore -InformationAction Continue
& $command $arguments
$exitCode = $LASTEXITCODE
}
if($removeSubscriptionId) {
Write-Verbose "Removing environment variable ARM_SUBSCRIPTION_ID that was set prior to this run"
Remove-Item $env:ARM_SUBSCRIPTION_ID = $null
}
# Stop and display timer
$StopWatch.Stop()
if(!$silent) {
Write-InformationColored "Time taken to complete Terraform apply:" -ForegroundColor Green -NewLineBefore -InformationAction Continue
}
$StopWatch.Elapsed | Format-Table
if($exitCode -ne 0) {
Write-InformationColored "Terraform $action failed with exit code $exitCode after $maxAttempts attempts. Please review the error and try again or raise an issue." -ForegroundColor Red -NewLineBefore -InformationAction Continue
throw "Terraform $action failed with exit code $exitCode after $maxAttempts attempts. Please review the error and try again or raise an issue."
} else {
if($output -ne "") {
if($outputFilePath -eq "") {
$outputFilePath = Join-Path $moduleFolderPath "output.json"
}
$command = "terraform"
$arguments = @()
$arguments += "-chdir=$moduleFolderPath"
$arguments += "output"
$arguments += "-json"
$arguments += "$output"
Write-Verbose "Outputting $output to $outputFilePath"
Write-Verbose "Running Output Command: $command $arguments"
& $command $arguments > $outputFilePath
}
}
}
}