-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
layout: post | ||
title: "AWS Load Balancer" | ||
date: 2023-09-20 03:42:00 -0400 | ||
modified_date: 2024-05-28 15:22:00 -0400 | ||
categories: aws elb | ||
--- | ||
|
||
# AWS ELB | ||
|
||
As the name Elastic Load Balancer says it balances the load by routing traffic across multiple targets. | ||
Good place to configure and debug how the flow of traffic goes with in AWS. | ||
|
||
## Notes | ||
|
||
## Useful AWS commands | ||
|
||
```sh | ||
aws elbv2 describe-load-balancers --query 'LoadBalancers[].LoadBalancerName' | ||
aws elbv2 describe-load-balancers --query 'LoadBalancers[].[LoadBalancerName,LoadBalancerArn]' | ||
|
||
|
||
lb_arn=$(aws elbv2 describe-load-balancers --names "$1" --query 'LoadBalancers[].[LoadBalancerArn]' --output text) | ||
|
||
listener_arn=$(aws elbv2 describe-listeners --load-balancer-arn "$lb_arn" --query 'Listeners[].{ListenerArn:ListenerArn,Protocol:Protocol,Port:Port}' | jq -r ".[] | select(.Port=="$2").ListenerArn") | ||
|
||
aws elbv2 describe-rules --listener-arn "$listnerarn" --query 'Rules[].{Priority:Priority,Host:Conditions[0].Values[0]}' | jq | ||
|
||
``` | ||
|
||
## Useful scripts | ||
|
||
Python script which prints all the hostnames used in the load balancer rounting rule. | ||
Change the log level to DEBUG to back trace. | ||
|
||
```python | ||
import boto3 | ||
from pprint import pprint | ||
import json | ||
import logging | ||
import sys | ||
|
||
|
||
logger = logging.getLogger('print-aws') | ||
logger.setLevel(logging.INFO) | ||
sh = logging.StreamHandler(sys.stdout) | ||
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') | ||
sh.setFormatter(formatter) | ||
logger.addHandler(sh) | ||
|
||
elbv2 = boto3.client("elbv2") | ||
|
||
|
||
def printEndPointUrls(): | ||
response= elbv2.describe_load_balancers() | ||
loadbalancers = response["LoadBalancers"][2:] | ||
logger.debug(response) | ||
for lb in loadbalancers: | ||
lbArn=lb["LoadBalancerArn"] | ||
printLBListeners(lbArn) | ||
|
||
|
||
def printLBListeners(lbArn): | ||
logger.debug(lbArn) | ||
response = elbv2.describe_listeners(LoadBalancerArn=lbArn) | ||
listeners = response["Listeners"] | ||
logger.debug(listeners) | ||
for listener in listeners: | ||
if listener["Protocol"] == "HTTPS" and listener["Port"] == 443: | ||
if not listener["DefaultActions"]: | ||
logger.debug(listener["DefaultActions"][0]["RedirectConfig"]["Host"]) | ||
listenerArn = listener["ListenerArn"] | ||
printListenerRules(listenerArn) | ||
|
||
|
||
def printListenerRules(listenerArn): | ||
logger.debug("printing Listener Rules...") | ||
try: | ||
response = elbv2.describe_rules(ListenerArn=listenerArn) | ||
logger.debug(response) | ||
if response["Rules"]: | ||
for rule in response["Rules"]: | ||
logger.debug(rule) | ||
printRule(rule) | ||
except Exception as e: | ||
logger.error(e) | ||
return | ||
|
||
|
||
|
||
def printRule(rule): | ||
try: | ||
if rule["Conditions"]: | ||
for condition in rule["Conditions"]: | ||
if condition["Field"] == "host-header": | ||
print(*condition["Values"], sep = "\n") | ||
except Exception as e: | ||
logger.error("Error at printing rule \n %s", json.dumps(rule, indent=2)) | ||
logger.error(e) | ||
return | ||
|
||
|
||
if __name__ == "__main__": | ||
print(printEndPointUrls()) | ||
#print(printListenerRules("arn:aws:elasticloadbalancing:......")) | ||
|
||
|
||
``` | ||
|
||
## Resources |
This file contains 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