-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAWS_ASG_ExitStandby.ps1
More file actions
117 lines (102 loc) · 3.5 KB
/
AWS_ASG_ExitStandby.ps1
File metadata and controls
117 lines (102 loc) · 3.5 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
# Part 2 of 2
# Part 1 Places EC2 instance into autoscaling group's standby mode.
# Part 2 Exits standby mode and waits for instance to be InService.
param (
[Parameter(Mandatory=$true)][string]$ASGEnterStandbyDeployStep, # Deploy step name of AWS_ASG_EnterStandby.ps1
[Parameter(Mandatory=$true)][string]$ASGNameVariable, # Variable name that is set by AWS_ASG_EnterStandby.ps1 for ASG Name
[Parameter(Mandatory=$true)][string]$registrationCheckInterval,
[Parameter(Mandatory=$true)][string]$maxRegistrationCheckCount
)
# Get EC2 Instance
Try
{
$response = Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/instance-id" -Method Get
If ($response)
{
$instanceId = $response
Write-Host "Instance ID: $instanceId"
}
Else
{
Write-Error -Message "Returned Instance ID does not appear to be valid"
Exit 1
}
}
Catch
{
Write-Error -Message "Failed to load instance ID from AWS." -Exception $_.Exception
Exit 1
}
# Get Stack name and status
# If Stack is being Updated, instances are updated via AutoScaling group policy,
# and there is no need to place instances into StandBy
Try
{
$stackName = Get-EC2Tag -Filter @{ Name="key";Values="aws:cloudformation:stack-name"},@{ Name="resource-id";Values=$instanceID}
$stackInfo = Get-CFNStack -StackName $stackName.Value
if($stackInfo.StackStatus -eq "UPDATE_IN_PROGRESS"){
Write-Host "CloudFormation stack updating, this Octopus step will now be skipped."
Exit
}
}
Catch
{
Write-Error -Message "Failed to retrieve CloudFormation stack status from AWS." -Exception $_.Exception
Exit 1
}
# Get ASG Name variable from previous deploy step (AWS_ASG_EnterStandby.ps1)
Try
{
$ASGName = $OctopusParameters["Octopus.Action[$ASGEnterStandbyDeployStep].Output.$ASGNameVariable"]
Write-Host "Auto Scaling Group Name: $ASGName"
If (!$ASGName)
{
Write-Error -Message "Returned Auto Scaling Group Name does not appear to be valid"
Exit 1
}
}
Catch
{
Write-Error -Message "Failed to get ASGNameVariable output variable from Octopus" -Exception $_.Exception
Exit 1
}
# Exit standby mode
Try
{
Write-Host "Exiting standby mode for instance: $instanceId in ASG: $ASGName."
Exit-ASStandby -InstanceId $instanceId -AutoScalingGroupName $ASGName -Force
Write-Host "Instance exited standby mode, waiting for it to go into service."
$instanceState = (Get-ASAutoScalingInstance -InstanceId $instanceId).LifecycleState
Write-Host "Current State: $instanceState"
$checkCount = 0
Write-Host "Retry Parameters:"
Write-Host "Maximum Checks: $maxRegistrationCheckCount"
Write-Host "Check Interval: $registrationCheckInterval"
While ($instanceState -ne "InService" -and $checkCount -le $maxRegistrationCheckCount)
{
$checkCount += 1
# Wait a bit until we check the status
Write-Host "Waiting for $registrationCheckInterval seconds for instance to be InService"
Start-Sleep -Seconds $registrationCheckInterval
If ($checkCount -le $maxRegistrationCheckCount)
{
Write-Host "$checkCount/$maxRegistrationCheckCount Attempts"
}
$instanceState = (Get-ASAutoScalingInstance -InstanceId $instanceId).LifecycleState
Write-Host "Current instance state: $instanceState"
}
If ($instanceState -eq "InService")
{
Write-Host "Instance in service!"
}
Else
{
Write-Error -Message "Instance not in service: $instanceState"
Exit 1
}
}
Catch
{
Write-Error -Message "Failed to exit standby mode." -Exception $_.Exception
Exit 1
}