Managing EC2 instances in AWS Elastic Load Balancers


AWS-elastic-load-balancer-icon
If you use AWS Elastic Load Balancers in front of your EC2 instances, you know that sometimes EC2 ELBs can take forever to recognize that the instance has come back up. This can happen long after the load balancer starts getting HTTP 200 responses to the health check you have specified for the ELB. I usually see this behavior after I have shut an instance down for a couple of hours for maintenance or for instances that require ELBs but which aren’t run all the time.

My solution to this is (surprise!) a PowerShell script, below, using Remove-ELBInstanceFromLoadBalancer followed by Register-ELBInstanceWithLoadBalancer. Both calls are using a “Name” tag associated with the EC2 instance so that it isn’t necessary to feed the PowerShell script the actual EC2 instance ID.

One odd thing I’ve noticed about using Register-ELBInstanceWithLoadBalancer is that it seems to return in $AWSHistory all the instances registered to that ELB, not just the one that was just enabled. Still, this code works well for me…and I hope for you, too.

<# De-register an EC2 instance from an AWS ELB using a tag (in this case, a tag labelled "Name", then re-register that instance.
Copyright 2015 Air11 Technology LLC
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>

$hashTable = @{"ec2-instance-1" = "ec2-elb-1";  # key = instance name; item = ELB
               "ec2-instance-2" = "ec2-elb-1";
               "ec2-instance-3" = "ec2-elb-1";
               "ec2-instance-4" = "ec2-elb-2";
               "ec2-instance-5" = "ec2-elb-2";
               "ec2-instance-6" = "ec2-elb-2";
               }


foreach ($hashKey in $hashTable.Keys) {

$elbInstanceObj = New-Object Amazon.ElasticLoadBalancing.Model.Instance
$instance = Get-EC2Instance -Filter @{name='tag:Name'; values= "${hashKey}" } | Select -ExpandProperty instances #Get instance object
$elbInstanceObj.InstanceId = $instance.InstanceId
Remove-ELBInstanceFromLoadBalancer -LoadBalancerName "$($hashTable.Item($hashKey))" -Instances $elbInstanceObj -Force # Deregister instance
Register-ELBInstanceWithLoadBalancer -LoadBalancerName "$($hashTable.Item($hashKey))" -Instances $elbInstanceObj # Reregister instance to reset health check

}

Posted

in

, , ,

by

Comments

8 responses to “Managing EC2 instances in AWS Elastic Load Balancers”

  1. Gary Woodfine Avatar

     Just wanted to say thank you for posting this little nugget online.

    It helped me immensely, as I have been developing a script to take instances out of an ELB.

    This shortened the journey time greatly!  I have also found your blog in general to be a great resource for powershell!

    Powershell, is one of my least favourite languages to work with, but your blog is gradually changing that!

    Thanks and greatly appreciate your efforts!

    1. Alex Neihaus Avatar
      Alex Neihaus

      Thank you very much, Gary.

      I am pleased you found my suggestions useful.

      It’s comments like this one that encourage me to keep posting.

      Cheers!

      Alex

  2. Jeff Avatar
    Jeff

    PowerShell in AWS… Shocked I tell you! I’m Shocked!

    Unfortunately, my legs are chained to my chair and my eyes are glued to PowerShell_ISE pretty much all day to get anything done with Windows Server in AWS.

    Anyhow – I’m late to the party here – but when specifically do you run this? You described the exact scenario that I’ll be faced with now that I’m implementing ELBs for an HA environment. Hopefully most of the time, our commercial production servers will be brought down gracefully for maintenance – which means every 6 weeks based on a quasi-agile schedule. We’re trying to compress the schedule, but just patching Windows 2012 R2 Web/App/DB Servers takes quite a while with 1 or 2 reboots. Then throw in our software and we’re down for a good bit. (I hate to say, but 3 – 4 hours).

    Can you please elaborate on when you run this script – right after your instance(s) is back on line I suppose…?

    1. Alex Neihaus Avatar
      Alex Neihaus

      I run this script during my maintenance window BEFORE I apply maintenance. An alternative might be to apply the maintenance first, wait for the AMI to be created and then, in an abundance of caution, create a new instance from it. I usually don’t do this because my window isn’t that long. The only really important thing is to make sure that you allow instance to reboot during AMI creation. The script does this explicitly even though it’s the default. Really happy this PowerShell script will be of some use to you!

    2. Alex Neihaus Avatar
      Alex Neihaus

      One other idea: check out Sapien’s PowerShell Studio. It’s expensive but a real VS-like environment for PowerShell development. It makes the PowerShell ISE look like Notepad.

      1. Jeff Avatar
        Jeff

        Ooooh, that sounds nice. Right now I’m a one man show building a proper B2B production environments for multitenant and private cloud customers – with SQL Server HA, EDI, BI – the whole shebang.

        I appreciate you taking the time to publish your work. I have a goal to gather up some of the AWS Windows Server tools that I’m building, plus a nice PowerShell cheat-sheet chock full of snippets that do tons of useful functions. There are so few examples of how to make and manage Windows environments in AWS.

        1. Alex Neihaus Avatar
          Alex Neihaus

          Jeff, thanks for the feedback. While I agree that there aren’t enough Windows Server examples, I must say that the core support for Windows Server in AWS is excellent. I have yet to find an AWS API that is not available in the PowerShell cmdlets (though the naming can be a little strained. And for this, there’s even the Get-AWSCmdlet cmdlet!).

          I look forward to seeing your repository of PowerShell tools for AWS — maybe we should start a project on GitHub to collect them?

  3. […] How to register and deregister EC2 instances by name from an ELB January 9, 2015 […]

Leave a Reply

Your email address will not be published. Required fields are marked *