/// <summary> /// checks to see if this instance is in the /// terminate wait state /// </summary> /// <returns></returns> private bool IsInTerminateWaitState() { bool returnValue = false; using (AmazonAutoScalingClient scalingClient = new AmazonAutoScalingClient()) { DescribeAutoScalingInstancesRequest describeAutoScalingInstancesRequest = new DescribeAutoScalingInstancesRequest { InstanceIds = new List <string>(new string[] { _InstanceId }) }; try { DescribeAutoScalingInstancesResponse describeAutoScalingInstancesResponse = scalingClient.DescribeAutoScalingInstancesAsync(describeAutoScalingInstancesRequest).Result; if (describeAutoScalingInstancesResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (AutoScalingInstanceDetails instDetail in describeAutoScalingInstancesResponse.AutoScalingInstances) { if (instDetail.InstanceId.Equals(_InstanceId, StringComparison.OrdinalIgnoreCase)) { if (instDetail.LifecycleState.CaseInsensitiveEquals("Terminating:Wait")) { returnValue = true; } break; } } } } catch (Exception) { //Yes AWS throws Exceptions and System.AggregrateExceptions //so for now this is the right way to handle it returnValue = false; } } return(returnValue); }
/// <summary> /// checks to see if this is an autoscaled instance /// </summary> /// <param name="instanceId"></param> /// <returns></returns> private bool IsAutoscaledInstance() { bool returnValue = false; if (!_InstanceId.IsNullOrWhiteSpace()) { using (AmazonAutoScalingClient scalingClient = new AmazonAutoScalingClient()) { DescribeAutoScalingInstancesRequest describeAutoScalingInstancesRequest = new DescribeAutoScalingInstancesRequest { InstanceIds = new List <string>(new string[] { _InstanceId }) }; try { DescribeAutoScalingInstancesResponse describeAutoScalingInstancesResponse = scalingClient.DescribeAutoScalingInstancesAsync(describeAutoScalingInstancesRequest).Result; if (describeAutoScalingInstancesResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (AutoScalingInstanceDetails instDetail in describeAutoScalingInstancesResponse.AutoScalingInstances) { if (instDetail.InstanceId.CaseInsensitiveEquals(_InstanceId)) { _AutoScalingGroupName = instDetail.AutoScalingGroupName; returnValue = true; break; } } } } catch (Exception) { //Yes AWS throws Exceptions and System.AggregrateExceptions //so for now this is the right way to handle it returnValue = false; } if (returnValue) { DescribeLifecycleHooksRequest describeLifecycleHooksRequest = new DescribeLifecycleHooksRequest { AutoScalingGroupName = _AutoScalingGroupName }; try { DescribeLifecycleHooksResponse describeLifecycleHooksResponse = scalingClient.DescribeLifecycleHooksAsync(describeLifecycleHooksRequest).Result; if (describeLifecycleHooksResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (LifecycleHook lifecycleHook in describeLifecycleHooksResponse.LifecycleHooks) { if (lifecycleHook.LifecycleTransition.CaseInsensitiveEquals("autoscaling:EC2_INSTANCE_TERMINATING")) { _TerminatingLifeCycleHookName = lifecycleHook.LifecycleHookName; } } } } catch (Exception) { //Yes AWS throws Exceptions and System.AggregrateExceptions //so for now this is the right way to handle it returnValue = false; } } } } return(returnValue); }