-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path00_Confirm-InstanceProfile.ps1
45 lines (37 loc) · 1.51 KB
/
00_Confirm-InstanceProfile.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
<#
.SYNOPSIS
Waits until instance profile is attached to the EC2 instance.
.DESCRIPTION
Waits until the instance profile is attached to the EC2 instance. For additional information regarding EC2 instance profiles,
please refer to https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html
.EXAMPLE
Confirm-InstanceProfile
.NOTES
Copyright 2020-2024 The MathWorks, Inc.
#>
function Confirm-InstanceProfile {
Write-Output 'Starting Confirm-InstanceProfile...'
$StatusCode = $null
$Response = $null
while ($StatusCode -ne 200) {
try {
$Token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "120"} -Method PUT -Uri http://169.254.169.254/latest/api/token
$Response = Invoke-WebRequest -UseBasicParsing -Uri http://169.254.169.254/latest/meta-data/iam/info -Headers @{ "X-aws-ec2-metadata-token" = $Token }
$StatusCode = $Response.StatusCode
Start-Sleep -Milliseconds 100
}
catch {
$StatusCode = $_.Exception.Response.StatusCode.value__
}
}
Write-Output 'Found information about attached instance profile:' ($Response.Content | ConvertFrom-Json).InstanceProfileArn
Write-Output 'Done with Confirm-InstanceProfile.'
}
try {
Confirm-InstanceProfile
}
catch {
$ScriptPath = $MyInvocation.MyCommand.Path
Write-Output "ERROR - An error occurred while running 'Confirm-InstanceProfile' script: $ScriptPath. Error: $_"
throw
}