Skip to content

Instantly share code, notes, and snippets.

@rssnyder
Created July 16, 2026 22:05
Show Gist options
  • Select an option

  • Save rssnyder/de933f910fbc358c8c432cc58dd77ad0 to your computer and use it in GitHub Desktop.

Select an option

Save rssnyder/de933f910fbc358c8c432cc58dd77ad0 to your computer and use it in GitHub Desktop.
reset a listener rule and target groups
#!/usr/bin/env python3
"""
Set a listener rule to send 100% of traffic to one target group and
0% to another target group.
Fill in the three values below and run: python3 set_traffic_weight.py
"""
import boto3
# ---- INPUT: fill these in ----
LISTENER_RULE_ARN = "arn:aws:elasticloadbalancing:REGION:ACCOUNT:listener-rule/app/..."
ACTIVE_TARGET_GROUP_ARN = "arn:aws:elasticloadbalancing:REGION:ACCOUNT:targetgroup/active-tg/..." # gets 100%
INACTIVE_TARGET_GROUP_ARN = "arn:aws:elasticloadbalancing:REGION:ACCOUNT:targetgroup/inactive-tg/..." # gets 0%
# --------------------------------
client = boto3.client("elbv2")
client.modify_rule(
RuleArn=LISTENER_RULE_ARN,
Actions=[
{
"Type": "forward",
"ForwardConfig": {
"TargetGroups": [
{"TargetGroupArn": ACTIVE_TARGET_GROUP_ARN, "Weight": 100},
{"TargetGroupArn": INACTIVE_TARGET_GROUP_ARN, "Weight": 0},
]
},
}
],
)
print("Done. 100% -> active TG, 0% -> inactive TG.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment