AWS CLI Application Load-Balancer Script for Re-Routing Traffic

Tessando used Ask the Experts™
I've recently set-up 3 Load-Balancers with 12 different Target Groups.

I'm looking to create a either an AWS CLI or Powershell script where I can send all traffic to one server (say, for the purposes of Maintenance and restarts).

Does anyone have, or would be willing to share, an AWS CLI script that allows me to essentially control the flow of traffic?

Thank you for your help.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Trying to understand what you really need.

You mentioned that you set up 3 load balancers. Are they equivalently providing the same service? Are you looking for methodology to "load balance and failover" the load balancers?

Since you mentioned servers in your question, I assume that you are looking for what is called blue/green deployment. How to refresh your cluster in stages, so you have a mix of current servers (blue) and new servers (green).

The trick is to correctly use Auto-scaling groups, Launch configurations, AMI and code deployment tools.

1. Have an up to date AMI of your servers.
2. Change your application to run on ASG.
3. Have remote deployment and configuration toosl.  AWS CodeDeploy and AWS Systems Manager do the job well.
4. When change comes, start a new instance off your current AMI, apply changes and create a new AMI.
5. Update your launch configuration to use the green AMI
6. Increase you ASG target value. ASG will spin up green instances to meet your new target value. You now have a mixed environment.
7. Verify that all is well, and kill your blue instances

Here is an article with some code examples. They use python to deploy new code and reconfigure servers.

Or, if you have servers manually set up, no auto scaling, no AMI in place, then first ask yourself what are you doing in the cloud in the first place. But then, treat ELB and EC2 like you would treat an on prem lb with snowflake servers. You will come up with a playbook that you want to implement, and that can be translated to AWS CLI or PowerShell. For example, a command to add an instance to an ELB is someting like

aws elb register-instances-with-load-balancer   --load-balancer-name <value>  --instances <value>  --region <region>

Open in new window

TessandoIT Administrator


Thank you, Shalomc. I really appreciate your answer... it's very thorough and an approach that, honestly, I didn't think of.

Right now, it's my understanding that the ALB splits traffic between two servers.

What I'm looking for is a script that would temporarily remove one server from the load-balancer so that I could do maintenance without effecting traffic. I could then run another script that would be in inverse of that to put both servers back into service, essentially normalizing the config.

If it's helpful, I wanted to do something like inverting the DNS Failover Policy. So, for example, if I want to invert my Failover Policy it would look like this:

aws route53 update-health-check --health-check-id xxxxxx-xxxx-xxx5-xxx8-d829615afced --inverted

Open in new window

And then I would normalize it like this:

aws route53 update-health-check --health-check-id xxxxxx-xxxx-xxx5-xxx8-d829615afced --no-inverted

Open in new window

Hopefully this makes sense.

Thank you again for your help, I appreciate it.
Look at the last 2 commands I mentioned. First one  disconnects an instance, second one reconnects it.
If you use ALB, then use the elbv2 register-targets and deregister-targets commands.
TessandoIT Administrator


Thank you for the clear explanation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial