Loading...
Loading...
Manage and troubleshoot Alibaba Cloud ALB (Application Load Balancer). List/inspect/create/update instances, listeners, server groups, rules, certificates, ACLs, security policies, and health check status.
npx skill4agent add cinience/alicloud-skills alicloud-network-albmkdir -p output/alicloud-network-alb
for f in skills/network/slb/alicloud-network-alb/scripts/*.py; do
python3 -m py_compile "$f"
done
echo "py_compile_ok" > output/alicloud-network-alb/validate.txtoutput/alicloud-network-alb/validate.txtpip install alibabacloud_alb20200616 alibabacloud_tea_openapi alibabacloud_credentialsALICLOUD_ACCESS_KEY_IDALICLOUD_ACCESS_KEY_SECRETALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRETALICLOUD_SECURITY_TOKEN~/.alibabacloud/credentials--output <file>scripts/list_instances.pypython3 scripts/list_instances.py --region cn-hangzhou
python3 scripts/list_instances.py --region cn-hangzhou --vpc-id vpc-xxx
python3 scripts/list_instances.py --region cn-hangzhou --address-type Internet --status Active
python3 scripts/list_instances.py --region cn-hangzhou --lb-ids alb-aaa alb-bbb --jsonscripts/get_instance_status.py# Tree overview: zones → listeners → rules
python3 scripts/get_instance_status.py --region cn-hangzhou --lb-id alb-xxx
# Full API response as JSON
python3 scripts/get_instance_status.py --region cn-hangzhou --lb-id alb-xxx --view detailscripts/create_load_balancer.py# Internet-facing ALB in two zones
python3 scripts/create_load_balancer.py --region cn-hangzhou --name my-alb \
--vpc-id vpc-xxx --address-type Internet \
--zone cn-hangzhou-h:vsw-aaa --zone cn-hangzhou-i:vsw-bbb
# Internal ALB with deletion protection
python3 scripts/create_load_balancer.py --region cn-hangzhou --name my-alb \
--vpc-id vpc-xxx --address-type Intranet --deletion-protection \
--zone cn-hangzhou-h:vsw-aaa --zone cn-hangzhou-i:vsw-bbbscripts/delete_load_balancer.pypython3 scripts/delete_load_balancer.py --region cn-hangzhou --lb-id alb-xxx
python3 scripts/delete_load_balancer.py --region cn-hangzhou --lb-id alb-xxx --yes # skip confirmscripts/deletion_protection.pypython3 scripts/deletion_protection.py --region cn-hangzhou --resource-id alb-xxx --enable
python3 scripts/deletion_protection.py --region cn-hangzhou --resource-id alb-xxx --disablescripts/list_listeners.pypython3 scripts/list_listeners.py --region cn-hangzhou --lb-id alb-xxx
python3 scripts/list_listeners.py --region cn-hangzhou --lb-id alb-xxx --jsonscripts/get_listener_attribute.pypython3 scripts/get_listener_attribute.py --region cn-hangzhou --listener-id lsn-xxxscripts/create_listener.py# HTTP listener forwarding to server group
python3 scripts/create_listener.py --region cn-hangzhou --lb-id alb-xxx \
--protocol HTTP --port 80 --action-type ForwardGroup \
--forward-server-groups sgp-xxx
# HTTPS listener with certificate
python3 scripts/create_listener.py --region cn-hangzhou --lb-id alb-xxx \
--protocol HTTPS --port 443 --action-type ForwardGroup \
--forward-server-groups sgp-xxx --certificate-ids cert-xxx
# HTTP to HTTPS redirect
python3 scripts/create_listener.py --region cn-hangzhou --lb-id alb-xxx \
--protocol HTTP --port 80 --action-type Redirect \
--redirect-protocol HTTPS --redirect-port 443
# Dry run
python3 scripts/create_listener.py --region cn-hangzhou --lb-id alb-xxx \
--protocol HTTP --port 80 --action-type ForwardGroup \
--forward-server-groups sgp-xxx --dry-runscripts/update_listener.py# Update description
python3 scripts/update_listener.py --region cn-hangzhou --listener-id lsn-xxx \
--description "Production HTTP listener"
# Change default forwarding target
python3 scripts/update_listener.py --region cn-hangzhou --listener-id lsn-xxx \
--forward-server-groups sgp-new
# Update timeouts and security policy
python3 scripts/update_listener.py --region cn-hangzhou --listener-id lsn-xxx \
--idle-timeout 60 --request-timeout 120 --security-policy-id tls_cipher_policy_1_2
# Enable HTTP/2 and gzip
python3 scripts/update_listener.py --region cn-hangzhou --listener-id lsn-xxx \
--http2-enabled true --gzip-enabled truescripts/start_listener.pyscripts/stop_listener.pypython3 scripts/start_listener.py --region cn-hangzhou --listener-id lsn-xxx
python3 scripts/stop_listener.py --region cn-hangzhou --listener-id lsn-xxxscripts/delete_listener.pypython3 scripts/delete_listener.py --region cn-hangzhou --listener-id lsn-xxx
python3 scripts/delete_listener.py --region cn-hangzhou --listener-id lsn-xxx --yes # skip confirmscripts/list_server_groups.pypython3 scripts/list_server_groups.py --region cn-hangzhou
python3 scripts/list_server_groups.py --region cn-hangzhou --vpc-id vpc-xxx
python3 scripts/list_server_groups.py --region cn-hangzhou --sg-ids sgp-aaa sgp-bbbscripts/list_server_group_servers.pypython3 scripts/list_server_group_servers.py --region cn-hangzhou --sg-id sgp-xxxscripts/create_server_group.py# Basic HTTP server group
python3 scripts/create_server_group.py --region cn-hangzhou --name my-sg \
--vpc-id vpc-xxx --protocol HTTP
# With health check customization
python3 scripts/create_server_group.py --region cn-hangzhou --name my-sg \
--vpc-id vpc-xxx --protocol HTTP \
--health-check-path /health --health-check-interval 10
# With sticky sessions
python3 scripts/create_server_group.py --region cn-hangzhou --name my-sg \
--vpc-id vpc-xxx --protocol HTTP \
--sticky-session-enabled --sticky-session-type Server --sticky-session-cookie SERVERID
# Dry run
python3 scripts/create_server_group.py --region cn-hangzhou --name my-sg \
--vpc-id vpc-xxx --dry-runscripts/delete_server_group.pypython3 scripts/delete_server_group.py --region cn-hangzhou --sg-id sgp-xxx
python3 scripts/delete_server_group.py --region cn-hangzhou --sg-id sgp-xxx --yes # skip confirmscripts/add_servers.py# Add ECS server (type:id:port[:weight[:description]])
python3 scripts/add_servers.py --region cn-hangzhou --sg-id sgp-xxx \
--server ecs:i-xxx:8080
# Add multiple servers with weight
python3 scripts/add_servers.py --region cn-hangzhou --sg-id sgp-xxx \
--server ecs:i-xxx:8080:100:web-1 \
--server ecs:i-yyy:8080:50:web-2
# Add IP-based server (for Ip-type server group)
python3 scripts/add_servers.py --region cn-hangzhou --sg-id sgp-xxx \
--server ip:10.0.1.100:8080scripts/remove_servers.py# Remove server (type:id:port)
python3 scripts/remove_servers.py --region cn-hangzhou --sg-id sgp-xxx \
--server ecs:i-xxx:8080
# Remove multiple servers
python3 scripts/remove_servers.py --region cn-hangzhou --sg-id sgp-xxx \
--server ecs:i-xxx:8080 --server ecs:i-yyy:8080scripts/list_rules.py# By load balancer
python3 scripts/list_rules.py --region cn-hangzhou --lb-id alb-xxx
# By listener
python3 scripts/list_rules.py --region cn-hangzhou --listener-id lsn-xxxscripts/create_rule.py# Block DELETE method with 405 response
python3 scripts/create_rule.py --region cn-hangzhou --listener-id lsn-xxx \
--name "block-delete" --priority 10 \
--condition-method DELETE \
--action-fixed-response "405 Method Not Allowed"
# Host-based routing to server group
python3 scripts/create_rule.py --region cn-hangzhou --listener-id lsn-xxx \
--name "api-route" --priority 20 \
--condition-host "api.example.com" \
--action-forward-to sgp-xxx
# Path-based routing
python3 scripts/create_rule.py --region cn-hangzhou --listener-id lsn-xxx \
--name "api-v1-route" --priority 30 \
--condition-host "api.example.com" --condition-path "/v1/*" \
--action-forward-to sgp-xxx
# HTTP to HTTPS redirect
python3 scripts/create_rule.py --region cn-hangzhou --listener-id lsn-xxx \
--name "force-https" --priority 5 \
--action-redirect "https 443"scripts/update_rule.py# Update rule name and priority
python3 scripts/update_rule.py --region cn-hangzhou --rule-id rule-xxx \
--name "new-name" --priority 50
# Change forwarding target
python3 scripts/update_rule.py --region cn-hangzhou --rule-id rule-xxx \
--action-forward-to sgp-new
# Update conditions and actions together
python3 scripts/update_rule.py --region cn-hangzhou --rule-id rule-xxx \
--condition-host "new.example.com" \
--action-forward-to sgp-newscripts/delete_rule.pypython3 scripts/delete_rule.py --region cn-hangzhou --rule-id rule-xxx
python3 scripts/delete_rule.py --region cn-hangzhou --rule-id rule-xxx --yes # skip confirmscripts/check_health_status.py# All listeners
python3 scripts/check_health_status.py --region cn-hangzhou --lb-id alb-xxx
# Specific listener
python3 scripts/check_health_status.py --region cn-hangzhou --lb-id alb-xxx --listener-id lsn-xxx
# JSON output (includes rule-level health status)
python3 scripts/check_health_status.py --region cn-hangzhou --lb-id alb-xxx --jsonscripts/list_listener_certificates.pypython3 scripts/list_listener_certificates.py --region cn-hangzhou --listener-id lsn-xxxscripts/list_security_policies.py# Custom policies only
python3 scripts/list_security_policies.py --region cn-hangzhou
# Include system predefined policies
python3 scripts/list_security_policies.py --region cn-hangzhou --systemscripts/list_acls.pypython3 scripts/list_acls.py --region cn-hangzhou
python3 scripts/list_acls.py --region cn-hangzhou --acl-ids acl-aaa acl-bbbscripts/list_acl_entries.pypython3 scripts/list_acl_entries.py --region cn-hangzhou --acl-id acl-xxxjob_idwait_for_job.pyscripts/wait_for_job.py# Wait for a job (default 120s timeout)
python3 scripts/wait_for_job.py --region cn-hangzhou --job-id 606f647c-xxxx-xxxx
# Custom timeout and interval
python3 scripts/wait_for_job.py --region cn-hangzhou --job-id xxx --timeout 300 --interval 3
# JSON output
python3 scripts/wait_for_job.py --region cn-hangzhou --job-id xxx --json
# Write result to file
python3 scripts/wait_for_job.py --region cn-hangzhou --job-id xxx --json --output result.jsonProcessingSucceededFailedreferences/resource-dependencies.mdPrerequisites: VPC, VSwitches, backend instances (ECS/ENI/ECI), and SSL certificates (for HTTPS) must already exist.
ServerGroupIdresp = client.create_server_group(alb_models.CreateServerGroupRequest(
server_group_name="my-sg",
vpc_id="vpc-xxx",
protocol="HTTP",
scheduler="Wrr",
health_check_config=alb_models.CreateServerGroupRequestHealthCheckConfig(
health_check_enabled=True,
health_check_path="/health",
health_check_codes=["http_2xx", "http_3xx"],
),
))
server_group_id = resp.body.server_group_idAclIdresp = client.create_acl(alb_models.CreateAclRequest(acl_name="my-acl"))
acl_id = resp.body.acl_id
# Add IP entries
client.add_entries_to_acl(alb_models.AddEntriesToAclRequest(
acl_id=acl_id,
acl_entries=[
alb_models.AddEntriesToAclRequestAclEntries(entry="10.0.0.0/8", description="internal"),
alb_models.AddEntriesToAclRequestAclEntries(entry="203.0.113.1/32", description="office"),
],
))client.add_servers_to_server_group(alb_models.AddServersToServerGroupRequest(
server_group_id=server_group_id, # ← Step 1a
servers=[alb_models.AddServersToServerGroupRequestServers(
server_type="Ecs",
server_id="i-xxx",
port=8080,
weight=100,
)],
))LoadBalancerIdresp = client.create_load_balancer(alb_models.CreateLoadBalancerRequest(
load_balancer_name="my-alb",
address_type="Internet", # Internet | Intranet
load_balancer_edition="Standard", # Basic | Standard | StandardWithWaf
vpc_id="vpc-xxx",
load_balancer_billing_config=alb_models.CreateLoadBalancerRequestLoadBalancerBillingConfig(
pay_type="PostPay",
),
zone_mappings=[
alb_models.CreateLoadBalancerRequestZoneMappings(zone_id="cn-hangzhou-h", v_switch_id="vsw-aaa"),
alb_models.CreateLoadBalancerRequestZoneMappings(zone_id="cn-hangzhou-i", v_switch_id="vsw-bbb"),
],
))
load_balancer_id = resp.body.load_balancer_id
# ⚠️ Async operation — poll GetLoadBalancerAttribute until LoadBalancerStatus == "Active"client.enable_load_balancer_access_log(alb_models.EnableLoadBalancerAccessLogRequest(
load_balancer_id=load_balancer_id, # ← Step 3
log_project="my-sls-project",
log_store="alb-access-log",
))ListenerId# HTTPS Listener (for HTTP, omit the certificates parameter)
resp = client.create_listener(alb_models.CreateListenerRequest(
load_balancer_id=load_balancer_id, # ← Step 3
listener_protocol="HTTPS",
listener_port=443,
default_actions=[alb_models.CreateListenerRequestDefaultActions(
type="ForwardGroup",
forward_group_config=alb_models.CreateListenerRequestDefaultActionsForwardGroupConfig(
server_group_tuples=[alb_models.CreateListenerRequestDefaultActionsForwardGroupConfigServerGroupTuples(
server_group_id=server_group_id, # ← Step 1a
)],
),
)],
certificates=[alb_models.CreateListenerRequestCertificates(certificate_id="cert-xxx")],
))
listener_id = resp.body.listener_id
# ⚠️ Async operation — poll GetListenerAttribute until ListenerStatus == "Running"client.create_rule(alb_models.CreateRuleRequest(
listener_id=listener_id, # ← Step 5
rule_name="api-route",
priority=10,
rule_conditions=[alb_models.CreateRuleRequestRuleConditions(
type="Host",
host_config=alb_models.CreateRuleRequestRuleConditionsHostConfig(values=["api.example.com"]),
)],
rule_actions=[alb_models.CreateRuleRequestRuleActions(
type="ForwardGroup",
order=1,
forward_group_config=alb_models.CreateRuleRequestRuleActionsForwardGroupConfig(
server_group_tuples=[alb_models.CreateRuleRequestRuleActionsForwardGroupConfigServerGroupTuples(
server_group_id=server_group_id, # ← Step 1a (or another ServerGroup)
)],
),
)],
))client.associate_acls_with_listener(alb_models.AssociateAclsWithListenerRequest(
listener_id=listener_id, # ← Step 5
acl_type="White", # White (whitelist) | Black (blacklist)
acl_ids=[acl_id], # ← Step 1b
))references/resource-dependencies.md# 1. Detach Listener sub-resources
client.dissociate_acls_from_listener(alb_models.DissociateAclsFromListenerRequest(
listener_id="lsn-xxx", acl_ids=["acl-xxx"],
))
client.delete_rule(alb_models.DeleteRuleRequest(rule_id="rule-xxx"))
# 2. Delete Listener
client.delete_listener(alb_models.DeleteListenerRequest(listener_id="lsn-xxx"))
# 3. Delete ALB (disable deletion protection first)
client.disable_deletion_protection(alb_models.DisableDeletionProtectionRequest(resource_id="alb-xxx"))
client.delete_load_balancer(alb_models.DeleteLoadBalancerRequest(load_balancer_id="alb-xxx"))
# 4. Delete independent resources
client.remove_servers_from_server_group(alb_models.RemoveServersFromServerGroupRequest(
server_group_id="sgp-xxx",
servers=[alb_models.RemoveServersFromServerGroupRequestServers(
server_type="Ecs", server_id="i-xxx", port=8080,
)],
))
client.delete_server_group(alb_models.DeleteServerGroupRequest(server_group_id="sgp-xxx"))
client.delete_acl(alb_models.DeleteAclRequest(acl_id="acl-xxx"))# Start / Stop Listener
client.start_listener(alb_models.StartListenerRequest(listener_id="lsn-xxx"))
client.stop_listener(alb_models.StopListenerRequest(listener_id="lsn-xxx"))
# Disable Access Log
client.disable_load_balancer_access_log(alb_models.DisableLoadBalancerAccessLogRequest(
load_balancer_id="alb-xxx",
))client.update_listener_attribute(alb_models.UpdateListenerAttributeRequest(
listener_id="lsn-xxx",
idle_timeout=60, # seconds
request_timeout=120, # seconds
security_policy_id="tls_cipher_policy_1_2", # HTTPS only
http_2enabled=True, # HTTPS only
))client.update_server_group_attribute(alb_models.UpdateServerGroupAttributeRequest(
server_group_id="sgp-xxx",
scheduler="Wrr", # Wrr | Wlc | Sch | Uch
health_check_config=alb_models.UpdateServerGroupAttributeRequestHealthCheckConfig(
health_check_enabled=True,
health_check_path="/health",
health_check_interval=5,
healthy_threshold=3,
unhealthy_threshold=3,
health_check_codes=["http_2xx", "http_3xx"],
),
sticky_session_config=alb_models.UpdateServerGroupAttributeRequestStickySessionConfig(
sticky_session_enabled=True,
sticky_session_type="Server", # Server | Insert
cookie="SERVERID",
),
))client.update_server_group_servers_attribute(alb_models.UpdateServerGroupServersAttributeRequest(
server_group_id="sgp-xxx",
servers=[alb_models.UpdateServerGroupServersAttributeRequestServers(
server_type="Ecs",
server_id="i-xxx",
port=8080,
weight=50, # adjust weight for traffic shifting
)],
))client.update_rule_attribute(alb_models.UpdateRuleAttributeRequest(
rule_id="rule-xxx",
rule_actions=[alb_models.UpdateRuleAttributeRequestRuleActions(
type="ForwardGroup",
order=1,
forward_group_config=alb_models.UpdateRuleAttributeRequestRuleActionsForwardGroupConfig(
server_group_tuples=[
alb_models.UpdateRuleAttributeRequestRuleActionsForwardGroupConfigServerGroupTuples(
server_group_id="sgp-blue", weight=80,
),
alb_models.UpdateRuleAttributeRequestRuleActionsForwardGroupConfigServerGroupTuples(
server_group_id="sgp-green", weight=20,
),
],
),
)],
))# Create HTTP:80 listener that redirects all traffic to HTTPS:443
client.create_listener(alb_models.CreateListenerRequest(
load_balancer_id=load_balancer_id,
listener_protocol="HTTP",
listener_port=80,
default_actions=[alb_models.CreateListenerRequestDefaultActions(
type="Redirect",
redirect_config=alb_models.CreateListenerRequestDefaultActionsRedirectConfig(
protocol="HTTPS",
port="443",
http_redirect_code="301",
),
)],
))# QUIC listener (requires an existing HTTPS listener on the same ALB)
client.create_listener(alb_models.CreateListenerRequest(
load_balancer_id=load_balancer_id,
listener_protocol="QUIC",
listener_port=443,
default_actions=[alb_models.CreateListenerRequestDefaultActions(
type="ForwardGroup",
forward_group_config=alb_models.CreateListenerRequestDefaultActionsForwardGroupConfig(
server_group_tuples=[alb_models.CreateListenerRequestDefaultActionsForwardGroupConfigServerGroupTuples(
server_group_id=server_group_id,
)],
),
)],
certificates=[alb_models.CreateListenerRequestCertificates(certificate_id="cert-xxx")],
))
# Note: QUIC Client Hello must be ≥ 1024 bytes; see troubleshooting doc for details# Create a reusable health check template
resp = client.create_health_check_template(alb_models.CreateHealthCheckTemplateRequest(
health_check_template_name="standard-http-check",
health_check_protocol="HTTP",
health_check_path="/health",
health_check_method="HEAD",
health_check_codes=["http_2xx", "http_3xx"],
health_check_interval=5,
health_check_timeout=3,
healthy_threshold=3,
unhealthy_threshold=3,
))
template_id = resp.body.health_check_template_id
# Apply template to a server group
client.apply_health_check_template_to_server_group(
alb_models.ApplyHealthCheckTemplateToServerGroupRequest(
server_group_id="sgp-xxx",
health_check_template_id=template_id,
)
)alicloud-observability-sls-log-queryGetLoadBalancerAttributeAccessLogConfig.LogProjectAccessLogConfig.LogStorereferences/log-analysis.mdreferences/troubleshooting.mdreferences/api_quick_map.mdoutput/alicloud-network-alb/