Created
December 7, 2017 19:51
-
-
Save MarcusFelling/18a2bb32f17306f726e59d1175167983 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Part 1 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]$ASGNameVariable # Passed in Octopus script step | |
) | |
# Get EC2 Instance | |
Try | |
{ | |
$response = Invoke-RestMethod -Uri "http://169.254.169.254/latest/meta-data/instance-id" -Method Get | |
If ($response) | |
{ | |
$instanceId = $response | |
} | |
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 ASG Name using $instanceId and set Octopus output variable to be used in subsequent deploy step ExitStandby.ps1 | |
Try | |
{ | |
$ASGInfo = Get-ASAutoScalingInstance -InstanceId $instanceId | |
$ASGName = $ASGInfo.AutoScalingGroupName | |
If ($ASGName) | |
{ | |
# Set ASGNameVariable Octopus output variable passed as argument in deploy step | |
# Referenced in subsequent deploy step ExitStandby.ps1: $ASGNameVariable = $OctopusParameters["Octopus.Action[EnterStandby.ps1].Output.$ASGNameVariable"] | |
Write-Host "Setting Octopus output variable $ASGNameVariable to value: $ASGName" | |
Set-OctopusVariable -name "$ASGNameVariable" -value "$ASGName" | |
Write-Host "Output variable set." | |
} | |
Else | |
{ | |
Write-Error -Message "Returned Auto Scaling Group name does not appear to be valid" | |
Exit 1 | |
} | |
} | |
Catch | |
{ | |
Write-Error -Message "Failed to retrieve Auto Scaling Group name from AWS." -Exception $_.Exception | |
Exit 1 | |
} | |
# Place instance in standby mode if InService, skip if already in standby mode. | |
Try | |
{ | |
$instanceState = (Get-ASAutoScalingInstance -InstanceId $instanceId).LifecycleState | |
If($instanceState -eq "InService") | |
{ | |
Write-Host "Placing instance: $instanceId into standby mode for ASG: $ASGName" | |
Enter-ASStandby -InstanceId $instanceId -AutoScalingGroupName $ASGName -ShouldDecrementDesiredCapacity $true -Force | |
Write-Host "Instance $instanceId is now in standby mode" | |
} | |
ElseIf($instanceState -eq "Standby") | |
{ | |
Write-Host "Instance already in standby" | |
} | |
Else | |
{ | |
Write-Error -Message "Error: Instance is not InService or Standby mode." -Exception $_.Exception | |
Exit 1 | |
} | |
} | |
Catch | |
{ | |
Write-Error -Message "Failed to place instance in standby mode." -Exception $_.Exception | |
Exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment