DescribeLoadBalancers 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeLoadBalancers 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DescribeLoadBalancers

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.NET
AWS SDK for .NET
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// <summary> /// Get the HTTP Endpoint of a load balancer by its name. /// </summary> /// <param name="loadBalancerName">The name of the load balancer.</param> /// <returns>The HTTP endpoint.</returns> public async Task<string> GetEndpointForLoadBalancerByName(string loadBalancerName) { if (_endpoint == null) { var endpointResponse = await _amazonElasticLoadBalancingV2.DescribeLoadBalancersAsync( new DescribeLoadBalancersRequest() { Names = new List<string>() { loadBalancerName } }); _endpoint = endpointResponse.LoadBalancers[0].DNSName; } return _endpoint; }
CLI
AWS CLI

描述負載平衡器

此範例說明指定的負載平衡器。

命令:

aws elbv2 describe-load-balancers --load-balancer-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188

輸出:

{ "LoadBalancers": [ { "Type": "application", "Scheme": "internet-facing", "IpAddressType": "ipv4", "VpcId": "vpc-3ac0fb5f", "AvailabilityZones": [ { "ZoneName": "us-west-2a", "SubnetId": "subnet-8360a9e7" }, { "ZoneName": "us-west-2b", "SubnetId": "subnet-b7d581c0" } ], "CreatedTime": "2016-03-25T21:26:12.920Z", "CanonicalHostedZoneId": "Z2P70J7EXAMPLE", "DNSName": "my-load-balancer-424835706.us-west-2.elb.amazonaws.com", "SecurityGroups": [ "sg-5943793c" ], "LoadBalancerName": "my-load-balancer", "State": { "Code": "active" }, "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188" } ] }

描述所有負載平衡器

此範例說明所有負載平衡器。

命令:

aws elbv2 describe-load-balancers
JavaScript
SDK for JavaScript (v3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import { ElasticLoadBalancingV2Client, DescribeLoadBalancersCommand, } from "@aws-sdk/client-elastic-load-balancing-v2"; export async function main() { const client = new ElasticLoadBalancingV2Client({}); const { LoadBalancers } = await client.send( new DescribeLoadBalancersCommand({}), ); const loadBalancersList = LoadBalancers.map( (lb) => `• ${lb.LoadBalancerName}: ${lb.DNSName}`, ).join("\n"); console.log( "Hello, Elastic Load Balancing! Let's list some of your load balancers:\n", loadBalancersList, ); } // Call function if run directly import { fileURLToPath } from "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { main(); }
PowerShell
for PowerShell 工具

範例 1:此範例會顯示指定區域的所有負載平衡器。

Get-ELB2LoadBalancer

輸出:

AvailabilityZones : {us-east-1c} CanonicalHostedZoneId : Z26RNL4JYFTOTI CreatedTime : 6/22/18 11:21:50 AM DNSName : test-elb1234567890-238d34ad8d94bc2e.elb.us-east-1.amazonaws.com IpAddressType : ipv4 LoadBalancerArn : arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/test-elb1234567890/238d34ad8d94bc2e LoadBalancerName : test-elb1234567890 Scheme : internet-facing SecurityGroups : {} State : Amazon.ElasticLoadBalancingV2.Model.LoadBalancerState Type : network VpcId : vpc-2cf00000
  • 如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 DescribeLoadBalancers

Python
SDK for Python (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class ElasticLoadBalancerWrapper: """Encapsulates Elastic Load Balancing (ELB) actions.""" def __init__(self, elb_client: boto3.client): """ Initializes the LoadBalancer class with the necessary parameters. """ self.elb_client = elb_client def get_endpoint(self, load_balancer_name) -> str: """ Gets the HTTP endpoint of the load balancer. :return: The endpoint. """ try: response = self.elb_client.describe_load_balancers( Names=[load_balancer_name] ) return response["LoadBalancers"][0]["DNSName"] except ClientError as err: log.error( f"Couldn't get the endpoint for load balancer {load_balancer_name}" ) error_code = err.response["Error"]["Code"] if error_code == "LoadBalancerNotFoundException": log.error( "Verify load balancer name and ensure it exists in the AWS console." ) log.error(f"Full error:\n\t{err}")
  • 如需 API 詳細資訊,請參閱 DescribeLoadBalancers AWS SDK for Python (Boto3) Word 參考中的 API