-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.ps1
340 lines (305 loc) · 18.7 KB
/
deploy.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
# #
# ▄▄▄█████▓▓█████ ▄▄▄ ███▄ ▄███▓ ██▓ ▄████▄ ▓█████ #
# ▓ ██▒ ▓▒▓█ ▀▒████▄ ▓██▒▀█▀ ██▒ ▓██▒▒██▀ ▀█ ▓█ ▀ #
# ▒ ▓██░ ▒░▒███ ▒██ ▀█▄ ▓██ ▓██░ ▒██▒▒▓█ ▄ ▒███ #
# ░ ▓██▓ ░ ▒▓█ ▄░██▄▄▄▄██ ▒██ ▒██ ░██░▒▓▓▄ ▄██▒▒▓█ ▄ #
# ▒██▒ ░ ░▒████▒▓█ ▓██▒▒██▒ ░██▒ ░██░▒ ▓███▀ ░░▒████▒ #
# ▒ ░░ ░░ ▒░ ░▒▒ ▓▒█░░ ▒░ ░ ░ ░▓ ░ ░▒ ▒ ░░░ ▒░ ░ #
# ░ ░ ░ ░ ▒ ▒▒ ░░ ░ ░ ▒ ░ ░ ▒ ░ ░ ░ #
# ░ ░ ░ ▒ ░ ░ ▒ ░░ ░ #
# ░ OFX INFRASTRUCTURE & CLOUD ENGINEERING ░ ░ #
# #
<#
.DESCRIPTION
<Deploys Transit Gateway and Transit Gateway Attachments in Child Accounts>
.INPUTS
<Modules: PS-AWS-SSO-AUTH
# Files
./files/attachment-source.yaml
./files/transit-gateway.yaml>
.OUTPUTS
<./files/attachment.yaml>
.NOTES
Author: Fraser Elliot Carter Smith
#>
#fixed variables
$variables = . "./variables.ps1"
$masteraccount = ($accounts | ? Master -eq $true).Account
$masteraccountID = ($accounts | ? Master -eq $true).AccountId
$masterrole = ($accounts | ? Master -eq $true).Role
$transitgatewaysource = "./files/transit-gateway-source.yaml"
$resourcesharesource = "./files/resource-share-source.yaml"
$attachmentsource = "./files/attachment-source.yaml"
$transitgateway = "./files/transit-gateway.yaml"
$resourceshare = "./files/resource-share.yaml"
#Script Begin
Write-Host "---------------------------"
Write-Host " Script Processing Start. " -f white -b magenta
Write-Host "---------------------------"
Write-Host ""
# Deploy transit gateway in master account
Write-Host "Processing Transit Gateway" -f black -b white
Write-Host "Connecting to account: $masteraccount" -f white
Switch-RoleAlias $masteraccount $masterrole
$stackname = $projectname
$infile = $transitgatewaysource
$outfile = $transitgateway
Write-Host "Writing $outfile file" -f green
Write-Host "Processing $stackname" -f Magenta
(Get-Content $infile) | Foreach-Object {
$_ -replace("regexbgpasn","$bgpASN")
} | Set-Content $outfile -force
$error.clear() ; $stack = 0 ; $stack_status = 0 ; $changeset_status = 0
# Try get existing stack
try { $stack_status = ((Get-CFNStack -Stackname $stackname -region $region).StackStatus).Value }
catch {Write-Host "Stack does not exist..." -f yellow ; $stack = 3 } # set stack value to 3 to deploy
if($redeploy -eq $true){$stack = 1} # if forcing update (delete + new)
if($delete -eq $true){$stack = 1} # if forcing teardown (delete only)
# Check Stack Status
if($stack -eq 0){
$updatevalues = @("CREATE_COMPLETE","CREATE_IN_PROGRESS","UPDATE_COMPLETE","UPDATE_IN_PROGRESS","ROLLBACK_COMPLETE","UPDATE_ROLLBACK_COMPLETE")
$deletevalues = @("CREATE_FAILED","DELETE_FAILED")
$newvalues = @("DELETE_COMPLETE")
foreach($value in $updatevalues){if($value -eq $stack_status){
# Create a change set and compare to see if there are changes being provided.
try { New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)}
catch {
#Changeset May Already Exist, Try Remove and then Try Again
try {
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)
} catch {}
}
try { $changeset_status = (Get-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname).status }
catch {Write-Host "No Change Set Exists -- this should never happen!" -f red ; continue } # break as logic error
if($changeset_status -eq "FAILED" ){
Write-Host "No Changes" -f white ; Write-Host "" ; continue
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
} # break as no changes
if($changeset_status -eq "CREATE_COMPLETE" ){
Write-Host "Changes Detected, Updating Stack" -f yellow ; $stack = 2
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
}
} } #skip iteration
foreach($value in $deletevalues){if($value -eq $stack_status){ $stack = 1 ; $redeploy = $true }
foreach($value in $newvalues){if($value -eq $stack_status){ $stack = 3 }}
}
}
# Delete Stack
if($stack -eq 1){
Write-Host "Removing Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
# Force Update (New Stack after Force Delete)
if($redeploy -eq $true){$stack = 3}
}
# Update Stack
if($stack -eq 2){
Write-Host "Updating Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNChangeSet -Stackname $stackname -ChangeSetName $stackname -Region $region -Force | Out-Null #cleanup change set
# Update-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
} # try wait for stack deployment if needed, catch will hide error if stack does not exist.
# Create Stack (Generally a New Stack)
if($stack -eq 3){
$error.clear()
# Attempts to validate the CF template.
Write-host "Validating CF Template: " -nonewline ;
Test-CFNTemplate -templateBody (Get-Content $outfile -raw) -Region $region
if($error.count -gt 0){Write-Host "Error Validation Failure!" -f red ; Write-Host "" ; continue }
if($error.count -eq 0){Write-Host "Template is Valid" -f green ; Write-Host "" }
Write-Host "Creating Stack: " -f White -b Magenta -NoNewLine ; Write-Host " $stackname"-f black -b white
# New-CFNStack -StackName $stackname -TemplateBody (Get-Content $outfile -raw) -Region $region
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
}
Write-Host ""
#get variables to create yaml files for RAM and Transit Gateway attachments Cloud Formations
$transitgatewayID = (Get-CFNExport -Region $region | ? Name -eq $stackname).Value
$transitgatewayARN = (Get-EC2TransitGateway -region $region | ? TransitGatewayId -eq $transitgatewayID).TransitGatewayArn
#Share Transit Gateway via RAM
Write-Host "Processing Resource Share" -f black -b white
$principalslist = "" #Create principals list
foreach($a in $accounts){
$accountID = $a.AccountId
$skip = $a.Master ; if($skip -eq $true){continue} #skips adding master account ID to principals list
$principalslist += "- `"$accountID`" `n"+" "}
$stackname = "$projectname-share"
$resourcesharename = $stackname #referenced later to accept the resource share in other accounts
$infile = $resourcesharesource
$outfile = $resourceshare
Write-Host "Writing $outfile file" -f green
Write-Host "Processing $stackname" -f Magenta
(Get-Content $infile) | Foreach-Object {
$_ -replace("regexprincipals","$principalslist") `
-replace("regexname",$stackname) `
-replace("regexresourcearns",$transitgatewayARN)
} | Set-Content $outfile -force
$error.clear() ; $stack = 0 ; $stack_status = 0 ; $changeset_status = 0
# Try get existing stack
try { $stack_status = ((Get-CFNStack -Stackname $stackname -region $region).StackStatus).Value }
catch {Write-Host "Stack does not exist..." -f yellow ; $stack = 3 } # set stack value to 3 to deploy
if($redeploy -eq $true){$stack = 1} # if forcing update (delete + new)
if($delete -eq $true){$stack = 1} # if forcing teardown (delete only)
# Check Stack Status
if($stack -eq 0){
$updatevalues = @("CREATE_COMPLETE","CREATE_IN_PROGRESS","UPDATE_COMPLETE","UPDATE_IN_PROGRESS","ROLLBACK_COMPLETE","UPDATE_ROLLBACK_COMPLETE")
$deletevalues = @("CREATE_FAILED","DELETE_FAILED")
$newvalues = @("DELETE_COMPLETE")
foreach($value in $updatevalues){if($value -eq $stack_status){
# Create a change set and compare to see if there are changes being provided.
try { New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)}
catch {
#Changeset May Already Exist, Try Remove and then Try Again
try {
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)
} catch {}
}
try { $changeset_status = (Get-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname).status }
catch {Write-Host "No Change Set Exists -- this should never happen!" -f red ; continue } # break as logic error
if($changeset_status -eq "FAILED" ){
Write-Host "No Changes" -f white ; Write-Host "" ; continue
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
} # break as no changes
if($changeset_status -eq "CREATE_COMPLETE" ){
Write-Host "Changes Detected, Updating Stack" -f yellow ; $stack = 2
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
}
} } #skip iteration
foreach($value in $deletevalues){if($value -eq $stack_status){ $stack = 1 ; $redeploy = $true }
foreach($value in $newvalues){if($value -eq $stack_status){ $stack = 3 }}
}
}
# Delete Stack
if($stack -eq 1){
Write-Host "Removing Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
# Force Update (New Stack after Force Delete)
if($redeploy -eq $true){$stack = 3}
}
# Update Stack
if($stack -eq 2){
Write-Host "Updating Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNChangeSet -Stackname $stackname -ChangeSetName $stackname -Region $region -Force | Out-Null #cleanup change set
# Update-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
} # try wait for stack deployment if needed, catch will hide error if stack does not exist.
# Create Stack (Generally a New Stack)
if($stack -eq 3){
$error.clear()
# Attempts to validate the CF template.
Write-host "Validating CF Template: " -nonewline ;
Test-CFNTemplate -templateBody (Get-Content $outfile -raw) -Region $region
if($error.count -gt 0){Write-Host "Error Validation Failure!" -f red ; Write-Host "" ; continue }
if($error.count -eq 0){Write-Host "Template is Valid" -f green ; Write-Host "" }
Write-Host "Creating Stack: " -f White -b Magenta -NoNewLine ; Write-Host " $stackname"-f black -b white
# New-CFNStack -StackName $stackname -TemplateBody (Get-Content $outfile -raw) -Region $region
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
}
Write-Host ""
# Connect to each account and configure transit gateway attachment
Write-Host "Processing Attachments" -f black -b white
foreach($a in $accounts){
$skip = $a.Master ; if($skip -eq $true){continue} # Skip processing master account
$account = $a.account
$role = $a.Role
$subnets = $a.subnets
$vpc = $a.vpc
$stackname = "$projectname-attachment-$vpc"
$attachment = "./files/$stackname.yaml"
# Connect to Account
Write-Host "Connecting to account: $Account" -f white
Switch-RoleAlias $Account $role
# Build Subnet Array
$subnetlist = $null
foreach($sub in $subnets){
$subnetlist += "- $sub `n"+" "
}
# Find and replace vpc, Subnets, project uuid and output to attachment.yaml
$infile = $attachmentsource
$outfile = $attachment
Write-Host "Writing $outfile file" -f green
Write-Host "Processing $stackname" -f Magenta
(Get-Content $infile) | Foreach-Object {
$_ -replace("regextransitgatewayID","$transitgatewayID") `
-replace("regextagname",$Account)`
-replace("regexvpc",$vpc) `
-replace("regexsubnets",$subnetlist) `
-replace("regexuuid",$uuid)
} | Set-Content $outfile -force
# Accept resource share ARN
try {Get-RAMResourceShareInvitation -region $region | ? ResourceShareName -like $resourcesharename | Confirm-RAMResourceShareInvitation -region $region ; Write-Host "Accepting Share" -f green }
catch { Write-Host "Error or already accepted!" -f yellow}
$error.clear() ; $stack = 0 ; $stack_status = 0 ; $changeset_status = 0
# Try get existing stack
try { $stack_status = ((Get-CFNStack -Stackname $stackname -region $region).StackStatus).Value }
catch {Write-Host "Stack does not exist..." -f yellow ; $stack = 3 } # set stack value to 3 to deploy
if($redeploy -eq $true){$stack = 1} # if forcing update (delete + new)
if($delete -eq $true){$stack = 1} # if forcing teardown (delete only)
# Check Stack Status
if($stack -eq 0){
$updatevalues = @("CREATE_COMPLETE","CREATE_IN_PROGRESS","UPDATE_COMPLETE","UPDATE_IN_PROGRESS","ROLLBACK_COMPLETE","UPDATE_ROLLBACK_COMPLETE")
$deletevalues = @("CREATE_FAILED","DELETE_FAILED")
$newvalues = @("DELETE_COMPLETE")
foreach($value in $updatevalues){if($value -eq $stack_status){
# Create a change set and compare to see if there are changes being provided.
try { New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)}
catch {
#Changeset May Already Exist, Try Remove and then Try Again
try {
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
New-CFNChangeSet -StackName $stackname -Region $region -ChangeSetName $stackname -TemplateBody (Get-Content $outfile -raw)
} catch {}
}
try { $changeset_status = (Get-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname).status }
catch {Write-Host "No Change Set Exists -- this should never happen!" -f red ; continue } # break as logic error
if($changeset_status -eq "FAILED" ){
Write-Host "No Changes" -f white ; Write-Host "" ; continue
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
} # break as no changes
if($changeset_status -eq "CREATE_COMPLETE" ){
Write-Host "Changes Detected, Updating Stack" -f yellow ; $stack = 2
Remove-CFNChangeSet -Region $region -ChangeSetName $stackname -stackname $stackname -Force | Out-Null
}
} } #skip iteration
foreach($value in $deletevalues){if($value -eq $stack_status){ $stack = 1 ; $redeploy = $true }
foreach($value in $newvalues){if($value -eq $stack_status){ $stack = 3 }}
}
}
# Delete Stack
if($stack -eq 1){
Write-Host "Removing Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
# Force Update (New Stack after Force Delete)
if($redeploy -eq $true){$stack = 3}
}
# Update Stack
if($stack -eq 2){
Write-Host "Updating Stack: " -f black -b red -NoNewLine ; Write-Host " $stackname"-f black -b white
# Remove-CFNChangeSet -Stackname $stackname -ChangeSetName $stackname -Region $region -Force | Out-Null #cleanup change set
# Update-CFNStack -Stackname $stackname -region $region -force
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
} # try wait for stack deployment if needed, catch will hide error if stack does not exist.
# Create Stack (Generally a New Stack)
if($stack -eq 3){
$error.clear()
# Attempts to validate the CF template.
Write-host "Validating CF Template: " -nonewline ;
Test-CFNTemplate -templateBody (Get-Content $outfile -raw) -Region $region
if($error.count -gt 0){Write-Host "Error Validation Failure!" -f red ; Write-Host "" ; continue }
if($error.count -eq 0){Write-Host "Template is Valid" -f green ; Write-Host "" }
Write-Host "Creating Stack: " -f White -b Magenta -NoNewLine ; Write-Host " $stackname"-f black -b white
# New-CFNStack -StackName $stackname -TemplateBody (Get-Content $outfile -raw) -Region $region
# try{ Wait-CFNStack -Stackname $stackname -region $region -timeout 240 } catch { Write-Host " $stackname failed" -f black -b red }
}
Write-Host ""
}
#$rollbackhash | Export-CSV $rollbackfile -force
Write-Host ""
Write-Host "---------------------------"
Write-Host "Script Processing Complete." -f white -b magenta
Write-Host "---------------------------"
Write-Host ""
Write-Host ""