Elastic Load Balancing-第 2 版範例使用 AWS CLI - AWS Command Line Interface

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

Elastic Load Balancing-第 2 版範例使用 AWS CLI

下列程式碼範例說明如何使用 Elastic Load Balancing-第 2 版,來執行動作及實作常見案例。 AWS Command Line Interface

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用add-listener-certificates

AWS CLI

新增憑證至安全接聽程式

此範例會將指定的憑證新增至指定的安全接聽程式。

命令:

aws elbv2 add-listener-certificates --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 --certificates CertificateArn=arn:aws:acm:us-west-2:123456789012:certificate/5cc54884-f4a3-4072-80be-05b9ba72f705

輸出:

{ "Certificates": [ { "CertificateArn": "arn:aws:acm:us-west-2:123456789012:certificate/5cc54884-f4a3-4072-80be-05b9ba72f705", "IsDefault": false } ] }

下列程式碼範例會示範如何使用add-tags

AWS CLI

若要將標籤新增至負載平衡器

下列add-tags範例會將project和標department籤新增至指定的負載平衡器。

aws elbv2 add-tags \ --resource-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --tags "Key=project,Value=lima" "Key=department,Value=digital-media"
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考AddTags中的。

下列程式碼範例會示範如何使用create-listener

AWS CLI

範例 1:建立 HTTP 監聽程式

下列create-listener範例會為指定的「應用程式負載平衡器」建立 HTTP 接聽程式,將要求轉送至指定的目標群組。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --protocol HTTP \ --port 80 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

如需詳細資訊,請參閱應用程式負載平衡器使用者指南中的教學課程:使用 AWS CLI 建立應用程式負載平衡器。

範例 2:若要建立 HTTPS 接聽程式

下列create-listener範例會為指定的「應用程式負載平衡器」建立 HTTPS 接聽程式,將要求轉送至指定的目標群組。您必須為 HTTPS 接聽程式指定 SSL 憑證。您可以使用 Certificate Manager (ACM) 建立及管理憑 AWS 證。或者,您可以使用 SSL/TLS 工具建立憑證、取得憑證授權單位 (CA) 簽署的憑證,然後將憑證上傳至 AWS Identity and Access Management (IAM)。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --protocol HTTPS \ --port 443 \ --certificates CertificateArn=arn:aws:acm:us-west-2:123456789012:certificate/3dcb0a41-bd72-4774-9ad9-756919c40557 \ --ssl-policy ELBSecurityPolicy-2016-08 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

如需詳細資訊,請參閱應用程式負載平衡器使用者指南中的新增 HTTPS 接聽程式

範例 3:建立 TCP 接聽程式

下列create-listener範例會為指定的 Network Load Balancer 建立 TCP 接聽程式,將要求轉送至指定的目標群組。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/net/my-network-load-balancer/5d1b75f4f1cee11e \ --protocol TCP \ --port 80 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-tcp-targets/b6bba954d1361c78

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的教學課程:使用 AWS CLI 建立網路負載平衡器。

範例 4:建立 TLS 接聽程式

下列create-listener範例會為指定的 Network Load Balancer 建立 TLS 接聽程式,將要求轉送至指定的目標群組。您必須指定 TLS 接聽程式的 SSL 憑證。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --protocol TLS \ --port 443 \ --certificates CertificateArn=arn:aws:acm:us-west-2:123456789012:certificate/3dcb0a41-bd72-4774-9ad9-756919c40557 \ --ssl-policy ELBSecurityPolicy-2016-08 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的網路負載平衡器的 TLS 接聽程式

範例 5:建立 UDP 接聽程式

下列create-listener範例會為指定的 Network Load Balancer 建立 UDP 接聽程式,將要求轉送至指定的目標群組。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/net/my-network-load-balancer/5d1b75f4f1cee11e \ --protocol UDP \ --port 53 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-tcp-targets/b6bba954d1361c78

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的教學課程:使用 AWS CLI 建立網路負載平衡器。

範例 6:為指定的閘道和轉送建立監聽器

下列create-listener範例會為指定的閘道 Load Balancer 建立接聽程式,將要求轉送至指定的目標群組。

aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:850631746142:loadbalancer/gwy/my-gateway-load-balancer/e0f9b3d5c7f7d3d6 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:850631746142:targetgroup/my-glb-targets/007ca469fae3bb1615

輸出:

{ "Listeners": [ { "ListenerArn": "arn:aws:elasticloadbalancing:us-east-1:850631746142:listener/gwy/my-agw-lb-example2/e0f9b3d5c7f7d3d6/afc127db15f925de", "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:850631746142:loadbalancer/gwy/my-agw-lb-example2/e0f9b3d5c7f7d3d6", "DefaultActions": [ { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:850631746142:targetgroup/test-tg-agw-2/007ca469fae3bb1615", "ForwardConfig": { "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:850631746142:targetgroup/test-tg-agw-2/007ca469fae3bb1615" } ] } } ] } ] }

如需詳細資訊,請參閱閘道負載平衡器使用者指南中的使用 AWS CLI 開始使用閘道負載平衡器。

  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考CreateListener中的。

下列程式碼範例會示範如何使用create-load-balancer

AWS CLI

範例 1:建立面向網際網路的負載平衡器

下列create-load-balancer範例會建立網際網路對向的 Application Load Balancer,並為指定的子網路啟用可用區域。

aws elbv2 create-load-balancer \ --name my-load-balancer \ --subnets subnet-b7d581c0 subnet-8360a9e7

輸出:

{ "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": "2017-08-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": "provisioning" }, "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188" } ] }

如需詳細資訊,請參閱應用程式負載平衡器使用者指南中的教學課程:使用 AWS CLI 建立應用程式負載平衡器。

範例 2:建立內部負載平衡器

下列create-load-balancer範例會建立內部 Application Load Balancer,並為指定的子網路啟用可用區域。

aws elbv2 create-load-balancer \ --name my-internal-load-balancer \ --scheme internal \ --subnets subnet-b7d581c0 subnet-8360a9e7

輸出:

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

如需詳細資訊,請參閱應用程式負載平衡器使用者指南中的教學課程:使用 AWS CLI 建立應用程式負載平衡器。

範例 3:建立 Network Load Balancer

下列create-load-balancer範例會建立網際網路對向網路的 Network Load Balancer,並為指定的子網路啟用可用區域。它會使用子網路對應,將指定的彈性 IP 位址與可用區域的負載平衡器節點所使用的網路介面產生關聯。

aws elbv2 create-load-balancer \ --name my-network-load-balancer \ --type network \ --subnet-mappings SubnetId=subnet-b7d581c0,AllocationId=eipalloc-64d5890a

輸出:

{ "LoadBalancers": [ { "Type": "network", "Scheme": "internet-facing", "IpAddressType": "ipv4", "VpcId": "vpc-3ac0fb5f", "AvailabilityZones": [ { "LoadBalancerAddresses": [ { "IpAddress": "35.161.207.171", "AllocationId": "eipalloc-64d5890a" } ], "ZoneName": "us-west-2b", "SubnetId": "subnet-5264e837" } ], "CreatedTime": "2017-10-15T22:41:25.657Z", "CanonicalHostedZoneId": "Z2P70J7EXAMPLE", "DNSName": "my-network-load-balancer-5d1b75f4f1cee11e.elb.us-west-2.amazonaws.com", "LoadBalancerName": "my-network-load-balancer", "State": { "Code": "provisioning" }, "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/net/my-network-load-balancer/5d1b75f4f1cee11e" } ] }

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的教學課程:使用 AWS CLI 建立網路負載平衡器。

範例 4:建立閘道 Load Balancer

下列create-load-balancer範例會建立閘道 Load Balancer,並為指定的子網路啟用可用區域。

aws elbv2 create-load-balancer \ --name my-gateway-load-balancer \ --type gateway \ --subnets subnet-dc83f691 subnet-a62583f9

輸出:

{ "LoadBalancers": [ { "Type": "gateway", "VpcId": "vpc-838475fe", "AvailabilityZones": [ { "ZoneName": "us-east-1b", "SubnetId": "subnet-a62583f9" }, { "ZoneName": "us-east-1a", "SubnetId": "subnet-dc83f691" } ], "CreatedTime": "2021-07-14T19:33:43.324000+00:00", "LoadBalancerName": "my-gateway-load-balancer", "State": { "Code": "provisioning" }, "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:850631746142:loadbalancer/gwy/my-gateway-load-balancer/dfbb5a7d32cdee79" } ] }

如需詳細資訊,請參閱閘道負載平衡器使用者指南中的使用 AWS CLI 開始使用閘道負載平衡器。

下列程式碼範例會示範如何使用create-rule

AWS CLI

範例 1:使用路徑條件和前進動作建立規則

下列create-rule範例會建立一個規則,在 URL 包含指定的模式時,將要求轉送至指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 5 \ --conditions file://conditions-pattern.json --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

conditions-pattern.json 的內容:

[ { "Field": "path-pattern", "PathPatternConfig": { "Values": ["/images/*"] } } ]

範例 2:使用主機條件和固定回應建立規則

下列create-rule範例會建立一個規則,在主機標頭中的主機名稱與指定的主機名稱相符時,提供固定回應。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 10 \ --conditions file://conditions-host.json \ --actions file://actions-fixed-response.json

conditions-host.json 的內容

[ { "Field": "host-header", "HostHeaderConfig": { "Values": ["*.example.com"] } } ]

actions-fixed-response.json 的內容

[ { "Type": "fixed-response", "FixedResponseConfig": { "MessageBody": "Hello world", "StatusCode": "200", "ContentType": "text/plain" } } ]

範例 3:使用來源 IP 位址條件、驗證動作和轉寄動作來建立規則

下列create-rule範例會建立一個規則,在來源 IP 位址符合指定的 IP 位址時驗證使用者;如果驗證成功,則會將要求轉送至指定的目標群組。

aws elbv2 create-rule \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --priority 20 \ --conditions file://conditions-source-ip.json \ --actions file://actions-authenticate.json

conditions-source-ip.json 的內容

[ { "Field": "source-ip", "SourceIpConfig": { "Values": ["192.0.2.0/24", "198.51.100.10/32"] } } ]

actions-authenticate.json 的內容

[ { "Type": "authenticate-oidc", "AuthenticateOidcConfig": { "Issuer": "https://idp-issuer.com", "AuthorizationEndpoint": "https://authorization-endpoint.com", "TokenEndpoint": "https://token-endpoint.com", "UserInfoEndpoint": "https://user-info-endpoint.com", "ClientId": "abcdefghijklmnopqrstuvwxyz123456789", "ClientSecret": "123456789012345678901234567890", "SessionCookieName": "my-cookie", "SessionTimeout": 3600, "Scope": "email", "AuthenticationRequestExtraParams": { "display": "page", "prompt": "login" }, "OnUnauthenticatedRequest": "deny" }, "Order": 1 }, { "Type": "forward", "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:880185128111:targetgroup/cli-test/642a97ecb0e0f26b", "Order": 2 } ]
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考CreateRule中的。

下列程式碼範例會示範如何使用create-target-group

AWS CLI

範例 1:建立 Application Load Balancer 的目標群組

下列create-target-group範例會為「Application Load Balancer」建立目標群組,您可在其中依執行個體 ID (目標類型為instance) 註冊目標。此目標群組使用 HTTP 通訊協定、連接埠 80 和 HTTP 目標群組的預設健全狀況檢查設定值。

aws elbv2 create-target-group \ --name my-targets \ --protocol HTTP \ --port 80 \ --target-type instance \ --vpc-id vpc-3ac0fb5f

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "TargetGroupName": "my-targets", "Protocol": "HTTP", "Port": 80, "VpcId": "vpc-3ac0fb5f", "HealthCheckProtocol": "HTTP", "HealthCheckPort": "traffic-port", "HealthCheckEnabled": true, "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 5, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "HealthCheckPath": "/", "Matcher": { "HttpCode": "200" }, "TargetType": "instance", "ProtocolVersion": "HTTP1", "IpAddressType": "ipv4" } ] }

如需詳細資訊,請參閱《應程式負載平衡器使用者指南》中的建立目標群組

範例 2:建立目標群組,將流量從 Application Load Balancer 路由至 Lambda 函數

下列create-target-group範例會為 Application Load Balancer 建立目標群組,其中目標為 Lambda 函數 (目標類型為lambda)。依預設,會停用此目標群組的 Health 況檢查。

aws elbv2 create-target-group \ --name my-lambda-target \ --target-type lambda

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-lambda-target/a3003e085dbb8ddc", "TargetGroupName": "my-lambda-target", "HealthCheckEnabled": false, "HealthCheckIntervalSeconds": 35, "HealthCheckTimeoutSeconds": 30, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "HealthCheckPath": "/", "Matcher": { "HttpCode": "200" }, "TargetType": "lambda", "IpAddressType": "ipv4" } ] }

如需詳細資訊,請參閱 Application Load Balancer 使用者指南Lambda 函數做為目標

範例 3:建立 Network Load Balancer 的目標群組

下列create-target-group範例會為 Network Load Balancer 建立目標群組,您可在其中依 IP 位址註冊目標 (目標類型為ip)。此目標群組使用 TCP 通訊協定、連接埠 80 和 TCP 目標群組的預設健全狀況檢查設定。

aws elbv2 create-target-group \ --name my-ip-targets \ --protocol TCP \ --port 80 \ --target-type ip \ --vpc-id vpc-3ac0fb5f

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-ip-targets/b6bba954d1361c78", "TargetGroupName": "my-ip-targets", "Protocol": "TCP", "Port": 80, "VpcId": "vpc-3ac0fb5f", "HealthCheckEnabled": true, "HealthCheckProtocol": "TCP", "HealthCheckPort": "traffic-port", "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 10, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "TargetType": "ip", "IpAddressType": "ipv4" } ] }

如需詳細資訊,請參閱《網路負載平衡器使用者指南》中的建立目標群組

範例 4:建立目標群組,將流量從 Network Load Balancer 路由到 Application Load Balancer

下列create-target-group範例會為您將應用程式負載平衡器註冊為目標 (目標類型為alb) 的 Network Load Balancer 建立目標群組。

AWS Elbv2 create-target-group -名稱- my-alb-target 協議 TCP-端口 80-目標類型阿爾布-VPC 標識 VPC-3ac0fb5f

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-alb-target/a3003e085dbb8ddc", "TargetGroupName": "my-alb-target", "Protocol": "TCP", "Port": 80, "VpcId": "vpc-838475fe", "HealthCheckProtocol": "HTTP", "HealthCheckPort": "traffic-port", "HealthCheckEnabled": true, "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 6, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "HealthCheckPath": "/", "Matcher": { "HttpCode": "200-399" }, "TargetType": "alb", "IpAddressType": "ipv4" } ] }

如需詳細資訊,請參閱《網路負載平衡器使用者指南》中的以應用程式負載平衡器作為目標建立目標群組。

範例 5:建立閘道 Load Balancer 的目標群組

下列create-target-group範例會為閘道 Load Balancer 建立目標群組,目標群組通訊協定為執行個體GENEVE

aws elbv2 create-target-group \ --name my-glb-targetgroup \ --protocol GENEVE \ --port 6081 \ --target-type instance \ --vpc-id vpc-838475fe

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-glb-targetgroup/00c3d57eacd6f40b6f", "TargetGroupName": "my-glb-targetgroup", "Protocol": "GENEVE", "Port": 6081, "VpcId": "vpc-838475fe", "HealthCheckProtocol": "TCP", "HealthCheckPort": "80", "HealthCheckEnabled": true, "HealthCheckIntervalSeconds": 10, "HealthCheckTimeoutSeconds": 5, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "TargetType": "instance" } ] }

如需詳細資訊,請參閱閘道 Load Balancer 使用指南中的建立目標群組 < https://docs.aws.amazon.com/elasticloadbalancing/latest/gateway/create-target-group.html>`__。

下列程式碼範例會示範如何使用delete-listener

AWS CLI

刪除監聽器

下列delete-listener範例會刪除指定的接聽程式。

aws elbv2 delete-listener \ --listener-arn arn:aws:elasticloadbalancing:ua-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考DeleteListener中的。

下列程式碼範例會示範如何使用delete-load-balancer

AWS CLI

若要刪除負載平衡器

下列delete-load-balancer範例會刪除指定的負載平衡器。

aws elbv2 delete-load-balancer \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188

下列程式碼範例會示範如何使用delete-rule

AWS CLI

刪除規則

下列delete-rule範例會刪除指定的規則。

aws elbv2 delete-rule \ --rule-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/1291d13826f405c3
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考DeleteRule中的。

下列程式碼範例會示範如何使用delete-target-group

AWS CLI

若要刪除目標群組

下列delete-target-group範例會刪除指定的目標群組。

aws elbv2 delete-target-group \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

此命令不會產生輸出。

如需詳細資訊,請參閱《應用程式負載平衡器指南》中的刪除負載平衡器

下列程式碼範例會示範如何使用deregister-targets

AWS CLI

範例 1:從目標群組取消註冊目標

下列deregister-targets範例會從指定的目標群組中移除指定的執行個體。

aws elbv2 deregister-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \ --targets Id=i-1234567890abcdef0

範例 2:取消註冊使用連接埠覆寫註冊的目標

下列deregister-targets範例會從使用連接埠覆寫註冊的目標群組中移除執行個體。

aws elbv2 deregister-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-internal-targets/3bb63f11dfb0faf9 \ --targets Id=i-1234567890abcdef0,Port=80 Id=i-1234567890abcdef0,Port=766

下列程式碼範例會示範如何使用describe-account-limits

AWS CLI

描述 Elastic Load Balancing 限制

下列describe-account-limits範例會顯示您在目前區域中 AWS 帳戶的 Elastic Load Balancing 限制。

aws elbv2 describe-account-limits

輸出:

{ "Limits": [ { "Name": "target-groups", "Max": "3000" }, { "Name": "targets-per-application-load-balancer", "Max": "1000" }, { "Name": "listeners-per-application-load-balancer", "Max": "50" }, { "Name": "rules-per-application-load-balancer", "Max": "100" }, { "Name": "network-load-balancers", "Max": "50" }, { "Name": "targets-per-network-load-balancer", "Max": "3000" }, { "Name": "targets-per-availability-zone-per-network-load-balancer", "Max": "500" }, { "Name": "listeners-per-network-load-balancer", "Max": "50" }, { "Name": "condition-values-per-alb-rule", "Max": "5" }, { "Name": "condition-wildcards-per-alb-rule", "Max": "5" }, { "Name": "target-groups-per-application-load-balancer", "Max": "100" }, { "Name": "target-groups-per-action-on-application-load-balancer", "Max": "5" }, { "Name": "target-groups-per-action-on-network-load-balancer", "Max": "1" }, { "Name": "certificates-per-application-load-balancer", "Max": "25" }, { "Name": "certificates-per-network-load-balancer", "Max": "25" }, { "Name": "targets-per-target-group", "Max": "1000" }, { "Name": "target-id-registrations-per-application-load-balancer", "Max": "1000" }, { "Name": "network-load-balancer-enis-per-vpc", "Max": "1200" }, { "Name": "application-load-balancers", "Max": "50" }, { "Name": "gateway-load-balancers", "Max": "100" }, { "Name": "gateway-load-balancers-per-vpc", "Max": "100" }, { "Name": "geneve-target-groups", "Max": "100" }, { "Name": "targets-per-availability-zone-per-gateway-load-balancer", "Max": "300" } ] }

如需詳細資訊,請參閱AWS 一般參考資料中的配額

下列程式碼範例會示範如何使用describe-listener-certificates

AWS CLI

說明安全接聽程式的憑證

此範例說明指定安全接聽程式的憑證。

命令:

aws elbv2 describe-listener-certificates --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2

輸出:

{ "Certificates": [ { "CertificateArn": "arn:aws:acm:us-west-2:123456789012:certificate/5cc54884-f4a3-4072-80be-05b9ba72f705", "IsDefault": false }, { "CertificateArn": "arn:aws:acm:us-west-2:123456789012:certificate/3dcb0a41-bd72-4774-9ad9-756919c40557", "IsDefault": false }, { "CertificateArn": "arn:aws:acm:us-west-2:123456789012:certificate/fe59da96-6f58-4a22-8eed-6d0d50477e1d", "IsDefault": true } ] }

下列程式碼範例會示範如何使用describe-listeners

AWS CLI

描述監聽器

此範例說明指定的偵聽程式。

命令:

aws elbv2 describe-listeners --listener-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2

輸出:

{ "Listeners": [ { "Port": 80, "Protocol": "HTTP", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2" } ] }

描述負載平衡器的監聽器

此範例說明指定負載平衡器的接聽程式。

命令:

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

輸出:

{ "Listeners": [ { "Port": 443, "Protocol": "HTTPS", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ], "SslPolicy": "ELBSecurityPolicy-2015-05", "Certificates": [ { "CertificateArn": "arn:aws:iam::123456789012:server-certificate/my-server-cert" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/0467ef3c8400ae65" }, { "Port": 80, "Protocol": "HTTP", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2" } ] }

下列程式碼範例會示範如何使用describe-load-balancer-attributes

AWS CLI

描述負載平衡器屬性

下列describe-load-balancer-attributes範例會顯示指定負載平衡器的屬性。

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

下列範例輸出顯示 Application Load Balancer 的屬性。

{ "Attributes": [ { "Value": "false", "Key": "access_logs.s3.enabled" }, { "Value": "", "Key": "access_logs.s3.bucket" }, { "Value": "", "Key": "access_logs.s3.prefix" }, { "Value": "60", "Key": "idle_timeout.timeout_seconds" }, { "Value": "false", "Key": "deletion_protection.enabled" }, { "Value": "true", "Key": "routing.http2.enabled" } ] }

下列範例輸出包含 Network Load Balancer 的屬性。

{ "Attributes": [ { "Value": "false", "Key": "access_logs.s3.enabled" }, { "Value": "", "Key": "access_logs.s3.bucket" }, { "Value": "", "Key": "access_logs.s3.prefix" }, { "Value": "false", "Key": "deletion_protection.enabled" }, { "Value": "false", "Key": "load_balancing.cross_zone.enabled" } ] }

下列程式碼範例會示範如何使用describe-load-balancers

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

下列程式碼範例會示範如何使用describe-rules

AWS CLI

範例 1:描述規則

下列describe-rules範例會顯示指定規則的詳細資訊。

aws elbv2 describe-rules \ --rule-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee

範例 2:描述監聽器的規則

下列describe-rules範例會顯示指定之接聽程式之規則的詳細資訊。輸出包括預設規則以及您新增的任何其他規則。

aws elbv2 describe-rules \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考DescribeRules中的。

下列程式碼範例會示範如何使用describe-ssl-policies

AWS CLI

範例 1:依負載平衡器類型列出用於 SSL 交涉的原則

下列describe-ssl-policies範例顯示可用於與應用 Application Load Balancer 進行 SSL 交涉的原則名稱。此範例使用--query參數僅顯示策略的名稱。

aws elbv2 describe-ssl-policies \ --load-balancer-type application \ --query SslPolicies[*].Name

輸出:

[ "ELBSecurityPolicy-2016-08", "ELBSecurityPolicy-TLS13-1-2-2021-06", "ELBSecurityPolicy-TLS13-1-2-Res-2021-06", "ELBSecurityPolicy-TLS13-1-2-Ext1-2021-06", "ELBSecurityPolicy-TLS13-1-2-Ext2-2021-06", "ELBSecurityPolicy-TLS13-1-1-2021-06", "ELBSecurityPolicy-TLS13-1-0-2021-06", "ELBSecurityPolicy-TLS13-1-3-2021-06", "ELBSecurityPolicy-TLS-1-2-2017-01", "ELBSecurityPolicy-TLS-1-1-2017-01", "ELBSecurityPolicy-TLS-1-2-Ext-2018-06", "ELBSecurityPolicy-FS-2018-06", "ELBSecurityPolicy-2015-05", "ELBSecurityPolicy-TLS-1-0-2015-04", "ELBSecurityPolicy-FS-1-2-Res-2019-08", "ELBSecurityPolicy-FS-1-1-2019-08", "ELBSecurityPolicy-FS-1-2-2019-08", "ELBSecurityPolicy-FS-1-2-Res-2020-10" ]

範例 2:列出支援特定通訊協定的原則

下列describe-ssl-policies範例顯示支援 TLS 1.3 通訊協定的原則名稱。此範例使用--query參數僅顯示策略的名稱。

aws elbv2 describe-ssl-policies \ --load-balancer-type application \ --query SslPolicies[?contains(SslProtocols,'TLSv1.3')].Name

輸出:

[ "ELBSecurityPolicy-TLS13-1-2-2021-06", "ELBSecurityPolicy-TLS13-1-2-Res-2021-06", "ELBSecurityPolicy-TLS13-1-2-Ext1-2021-06", "ELBSecurityPolicy-TLS13-1-2-Ext2-2021-06", "ELBSecurityPolicy-TLS13-1-1-2021-06", "ELBSecurityPolicy-TLS13-1-0-2021-06", "ELBSecurityPolicy-TLS13-1-3-2021-06" ]

範例 3:顯示策略的密碼

下列describe-ssl-policies範例會顯示指定策略的密碼名稱。此範例使用--query參數僅顯示密碼名稱。清單中的第一個密碼具有優先順序 1,其餘的密碼依優先順序排列。

aws elbv2 describe-ssl-policies \ --names ELBSecurityPolicy-TLS13-1-2-2021-06 \ --query SslPolicies[*].Ciphers[*].Name

輸出:

[ "TLS_AES_128_GCM_SHA256", "TLS_AES_256_GCM_SHA384", "TLS_CHACHA20_POLY1305_SHA256", "ECDHE-ECDSA-AES128-GCM-SHA256", "ECDHE-RSA-AES128-GCM-SHA256", "ECDHE-ECDSA-AES128-SHA256", "ECDHE-RSA-AES128-SHA256", "ECDHE-ECDSA-AES256-GCM-SHA384", "ECDHE-RSA-AES256-GCM-SHA384", "ECDHE-ECDSA-AES256-SHA384", "ECDHE-RSA-AES256-SHA384" ]

如需詳細資訊,請參閱應用程式負載平衡器使用者指南中的安全性原則

下列程式碼範例會示範如何使用describe-tags

AWS CLI

描述指派給負載平衡器的標籤

此範例說明指派給指定負載平衡器的標籤。

命令:

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

輸出:

{ "TagDescriptions": [ { "ResourceArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "Tags": [ { "Value": "lima", "Key": "project" }, { "Value": "digital-media", "Key": "department" } ] } ] }
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考DescribeTags中的。

下列程式碼範例會示範如何使用describe-target-group-attributes

AWS CLI

描述目標群組屬性

下列describe-target-group-attributes範例會顯示指定目標群組的屬性。

aws elbv2 describe-target-group-attributes \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

如果通訊協定為 HTTP 或 HTTPS,且目標類型為或,則輸出會包含屬性ipinstance

{ "Attributes": [ { "Value": "false", "Key": "stickiness.enabled" }, { "Value": "300", "Key": "deregistration_delay.timeout_seconds" }, { "Value": "lb_cookie", "Key": "stickiness.type" }, { "Value": "86400", "Key": "stickiness.lb_cookie.duration_seconds" }, { "Value": "0", "Key": "slow_start.duration_seconds" } ] }

如果通訊協定為 HTTP 或 HTTPS,且目標類型為,則下列輸出會包含屬性lambda

{ "Attributes": [ { "Value": "false", "Key": "lambda.multi_value_headers.enabled" } ] }

如果通訊協定是 TCP、TLS、UDP 或 TCP_UDP,則下列輸出會包含屬性。

{ "Attributes": [ { "Value": "false", "Key": "proxy_protocol_v2.enabled" }, { "Value": "300", "Key": "deregistration_delay.timeout_seconds" } ] }

下列程式碼範例會示範如何使用describe-target-groups

AWS CLI

範例 1:描述目標群組

下列describe-target-groups範例顯示指定目標群組的詳細資訊。

aws elbv2 describe-target-groups \ --target-group-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "TargetGroupName": "my-targets", "Protocol": "HTTP", "Port": 80, "VpcId": "vpc-3ac0fb5f", "HealthCheckProtocol": "HTTP", "HealthCheckPort": "traffic-port", "HealthCheckEnabled": true, "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 5, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "HealthCheckPath": "/", "Matcher": { "HttpCode": "200" }, "LoadBalancerArns": [ "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188" ], "TargetType": "instance", "ProtocolVersion": "HTTP1", "IpAddressType": "ipv4" } ] }

範例 2:描述負載平衡器的所有目標群組

下列describe-target-groups範例顯示指定負載平衡器之所有目標群組的詳細資訊。此範例使用--query參數僅顯示目標群組名稱。

aws elbv2 describe-target-groups \ --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --query TargetGroups[*].TargetGroupName

輸出:

[ "my-instance-targets", "my-ip-targets", "my-lambda-target" ]

如需詳細資訊,請參閱《應用程式負載平衡器指南》中的目標群組

下列程式碼範例會示範如何使用describe-target-health

AWS CLI

範例 1:描述目標群組目標的健全狀況

下列describe-target-health範例顯示指定目標群組之目標的健全狀況詳細資訊。這些目標是健康的。

aws elbv2 describe-target-health \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

輸出:

{ "TargetHealthDescriptions": [ { "HealthCheckPort": "80", "Target": { "Id": "i-ceddcd4d", "Port": 80 }, "TargetHealth": { "State": "healthy" } }, { "HealthCheckPort": "80", "Target": { "Id": "i-0f76fade", "Port": 80 }, "TargetHealth": { "State": "healthy" } } ] }

範例 2:描述目標的健全狀況

下列describe-target-health範例顯示指定目標的健全狀況詳細資訊。這個目標是健康的。

aws elbv2 describe-target-health \ --targets Id=i-0f76fade,Port=80 \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

輸出:

{ "TargetHealthDescriptions": [ { "HealthCheckPort": "80", "Target": { "Id": "i-0f76fade", "Port": 80 }, "TargetHealth": { "State": "healthy" } } ] }

下列範例輸出適用於未在監聽器動作中指定其目標群組的目標。此目標無法從負載平衡器接收流量。

{ "TargetHealthDescriptions": [ { "HealthCheckPort": "80", "Target": { "Id": "i-0f76fade", "Port": 80 }, "TargetHealth": { "State": "unused", "Reason": "Target.NotInUse", "Description": "Target group is not configured to receive traffic from the load balancer" } } ] }

下列範例輸出適用於剛在監聽器動作中指定其目標群組的目標。目標仍在註冊中。

{ "TargetHealthDescriptions": [ { "HealthCheckPort": "80", "Target": { "Id": "i-0f76fade", "Port": 80 }, "TargetHealth": { "State": "initial", "Reason": "Elb.RegistrationInProgress", "Description": "Target registration is in progress" } } ] }

下列範例輸出適用於狀態不良的目標。

{ "TargetHealthDescriptions": [ { "HealthCheckPort": "80", "Target": { "Id": "i-0f76fade", "Port": 80 }, "TargetHealth": { "State": "unhealthy", "Reason": "Target.Timeout", "Description": "Connection to target timed out" } } ] }

下列範例輸出適用於 Lambda 函數的目標,且運作狀態檢查已停用。

{ "TargetHealthDescriptions": [ { "Target": { "Id": "arn:aws:lambda:us-west-2:123456789012:function:my-function", "AvailabilityZone": "all", }, "TargetHealth": { "State": "unavailable", "Reason": "Target.HealthCheckDisabled", "Description": "Health checks are not enabled for this target" } } ] }

下列程式碼範例會示範如何使用modify-listener

AWS CLI

範例 1:若要將預設動作變更為轉寄動作

下列modify-listener範例會變更指定接聽程式的預設動作 (轉寄動作)。

aws elbv2 modify-listener \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-new-targets/2453ed029918f21f

輸出:

{ "Listeners": [ { "Protocol": "HTTP", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-new-targets/2453ed029918f21f", "Type": "forward" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "Port": 80, "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2" } ] }

範例 2:將預設動作變更為重新導向動作

下列modify-listener範例會將指定接聽程式的預設動作變更為重新導向動作。

aws elbv2 modify-listener \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 \ --default-actions Type=redirect,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-new-targets/2453ed029918f21f

輸出:

{ "Listeners": [ { "Protocol": "HTTP", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-new-targets/2453ed029918f21f", "Type": "redirect" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "Port": 80, "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2" } ] }

範例 3:變更伺服器憑證

此範例會變更指定 HTTPS 接聽程式的伺服器憑證。

aws elbv2 modify-listener \ --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/0467ef3c8400ae65 \ --certificates CertificateArn=arn:aws:iam::123456789012:server-certificate/my-new-server-cert

輸出:

{ "Listeners": [ { "Protocol": "HTTPS", "DefaultActions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ], "SslPolicy": "ELBSecurityPolicy-2015-05", "Certificates": [ { "CertificateArn": "arn:aws:iam::123456789012:server-certificate/my-new-server-cert" } ], "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188", "Port": 443, "ListenerArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/0467ef3c8400ae65" } ] }
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考ModifyListener中的。

下列程式碼範例會示範如何使用modify-load-balancer-attributes

AWS CLI

啟用刪除保護

此範例會針對指定的負載平衡器啟用刪除保護。

命令:

aws elbv2 modify-load-balancer-attributes --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --attributes Key=deletion_protection.enabled,Value=true

輸出:

{ "Attributes": [ { "Value": "true", "Key": "deletion_protection.enabled" }, { "Value": "false", "Key": "access_logs.s3.enabled" }, { "Value": "60", "Key": "idle_timeout.timeout_seconds" }, { "Value": "", "Key": "access_logs.s3.prefix" }, { "Value": "", "Key": "access_logs.s3.bucket" } ] }

若要變更閒置逾時

此範例會變更指定負載平衡器的閒置逾時值。

命令:

aws elbv2 modify-load-balancer-attributes --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --attributes Key=idle_timeout.timeout_seconds,Value=30

輸出:

{ "Attributes": [ { "Value": "30", "Key": "idle_timeout.timeout_seconds" }, { "Value": "false", "Key": "access_logs.s3.enabled" }, { "Value": "", "Key": "access_logs.s3.prefix" }, { "Value": "true", "Key": "deletion_protection.enabled" }, { "Value": "", "Key": "access_logs.s3.bucket" } ] }

啟用存取記錄

此範例會啟用指定負載平衡器的存取記錄。請注意,S3 儲存貯體必須與負載平衡器位於相同的區域,並且必須附加政策以授予 Elastic Load Balancing 服務的存取權。

命令:

aws elbv2 modify-load-balancer-attributes --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --attributes Key=access_logs.s3.enabled,Value=true Key=access_logs.s3.bucket,Value=my-loadbalancer-logs Key=access_logs.s3.prefix,Value=myapp

輸出:

{ "Attributes": [ { "Value": "true", "Key": "access_logs.s3.enabled" }, { "Value": "my-load-balancer-logs", "Key": "access_logs.s3.bucket" }, { "Value": "myapp", "Key": "access_logs.s3.prefix" }, { "Value": "60", "Key": "idle_timeout.timeout_seconds" }, { "Value": "false", "Key": "deletion_protection.enabled" } ] }

下列程式碼範例會示範如何使用modify-rule

AWS CLI

若要修改規則

下列modify-rule範例會更新指定規則的動作和條件。

aws elbv2 modify-rule \ --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \ --conditions Field=path-pattern,Values='/images/*' --rule-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee

輸出:

{ "Rules": [ { "Priority": "10", "Conditions": [ { "Field": "path-pattern", "Values": [ "/images/*" ] } ], "RuleArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/9683b2d02a6cabee", "IsDefault": false, "Actions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ] } ] }
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考ModifyRule中的。

下列程式碼範例會示範如何使用modify-target-group-attributes

AWS CLI

修改取消註冊延遲逾時

此範例會將取消註冊延遲逾時設定為指定目標群組的指定值。

命令:

aws elbv2 modify-target-group-attributes --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 --attributes Key=deregistration_delay.timeout_seconds,Value=600

輸出:

{ "Attributes": [ { "Value": "false", "Key": "stickiness.enabled" }, { "Value": "600", "Key": "deregistration_delay.timeout_seconds" }, { "Value": "lb_cookie", "Key": "stickiness.type" }, { "Value": "86400", "Key": "stickiness.lb_cookie.duration_seconds" } ] }

下列程式碼範例會示範如何使用modify-target-group

AWS CLI

修改目標群組的健全狀況檢查組態

下列modify-target-group範例會變更用來評估指定目標群組之目標健全狀況檢查之健全狀況檢查的組態。請注意,由於 CLI 剖析逗號的方式,您必須使用單引號而不是雙引號括住--matcher選項的範圍。

aws elbv2 modify-target-group \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-https-targets/2453ed029918f21f \ --health-check-protocol HTTPS \ --health-check-port 443 \ --matcher HttpCode='200,299'

輸出:

{ "TargetGroups": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-https-targets/2453ed029918f21f", "TargetGroupName": "my-https-targets", "Protocol": "HTTPS", "Port": 443, "VpcId": "vpc-3ac0fb5f", "HealthCheckProtocol": "HTTPS", "HealthCheckPort": "443", "HealthCheckEnabled": true, "HealthCheckIntervalSeconds": 30, "HealthCheckTimeoutSeconds": 5, "HealthyThresholdCount": 5, "UnhealthyThresholdCount": 2, "Matcher": { "HttpCode": "200,299" }, "LoadBalancerArns": [ "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188" ], "TargetType": "instance", "ProtocolVersion": "HTTP1", "IpAddressType": "ipv4" } ] }

如需詳細資訊,請參閱《應用程式負載平衡器指南》中的目標群組

下列程式碼範例會示範如何使用register-targets

AWS CLI

範例 1:依執行個體 ID 向目標群組註冊目標

下列register-targets範例會將指定的執行個體註冊至目標群組。目標群組的目標類型必須為instance

aws elbv2 register-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \ --targets Id=i-1234567890abcdef0 Id=i-0abcdef1234567890

範例 2:使用連接埠覆寫將目標註冊至目標群組

下列register-targets範例會使用多個連接埠,將指定的執行個體註冊至目標群組。這可讓您在與目標群組中的目標相同的執行個體上註冊容器。

aws elbv2 register-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-internal-targets/3bb63f11dfb0faf9 \ --targets Id=i-0598c7d356eba48d7,Port=80 Id=i-0598c7d356eba48d7,Port=766

範例 3:依 IP 位址向目標群組註冊目標

下列register-targets範例會將指定的 IP 位址註冊至目標群組。目標群組的目標類型必須為ip

aws elbv2 register-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-tcp-ip-targets/8518e899d173178f \ --targets Id=10.0.1.15 Id=10.0.1.23

範例 4:若要將 Lambda 函數註冊為目標

下列register-targets範例會將指定的 IP 位址註冊至目標群組。目標群組的目標類型必須為lambda。您必須授與 Elastic Load Balancing 權限才能叫用 Lambda 函數。

aws elbv2 register-targets \ --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-tcp-ip-targets/8518e899d173178f \ --targets Id=arn:aws:lambda:us-west-2:123456789012:function:my-function
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考RegisterTargets中的。

下列程式碼範例會示範如何使用remove-listener-certificates

AWS CLI

從安全接聽程式移除憑證

此範例會從指定的安全接聽程式移除指定的憑證。

命令:

aws elbv2 remove-listener-certificates --listener-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2 --certificates CertificateArn=arn:aws:acm:us-west-2:123456789012:certificate/5cc54884-f4a3-4072-80be-05b9ba72f705

下列程式碼範例會示範如何使用remove-tags

AWS CLI

若要從負載平衡器移除標籤

下列remove-tags範例會從指定的負載平衡器移除projectdepartment標籤。

aws elbv2 remove-tags \ --resource-arns arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 \ --tag-keys project department
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考RemoveTags中的。

下列程式碼範例會示範如何使用set-ip-address-type

AWS CLI

設定負載平衡器的位址類型

此範例會將指定負載平衡器的位址類型設定為dualstack。負載平衡器子網路必須具有關聯的 IPv6 CIDR 區塊。

命令:

aws elbv2 set-ip-address-type --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --ip-address-type dualstack

輸出:

{ "IpAddressType": "dualstack" }
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考SetIpAddressType中的。

下列程式碼範例會示範如何使用set-rule-priorities

AWS CLI

設定規則優先順序

此範例會設定指定規則的優先順序。

命令:

aws elbv2 set-rule-priorities --rule-priorities RuleArn=arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/1291d13826f405c3,Priority=5

輸出:

{ "Rules": [ { "Priority": "5", "Conditions": [ { "Field": "path-pattern", "Values": [ "/img/*" ] } ], "RuleArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:listener-rule/app/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2/1291d13826f405c3", "IsDefault": false, "Actions": [ { "TargetGroupArn": "arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067", "Type": "forward" } ] } ] }

下列程式碼範例會示範如何使用set-security-groups

AWS CLI

將安全群組與負載平衡器建立關聯

此範例會將指定的安全性群組與指定的負載平衡器產生關聯。

命令:

aws elbv2 set-security-groups --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --security-groups sg-5943793c

輸出:

{ "SecurityGroupIds": [ "sg-5943793c" ] }

下列程式碼範例會示範如何使用set-subnets

AWS CLI

啟用負載平衡器的可用區域

此範例會為指定負載平衡器啟用指定子網路的可用區域。

命令:

aws elbv2 set-subnets --load-balancer-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188 --subnets subnet-8360a9e7 subnet-b7d581c0

輸出:

{ "AvailabilityZones": [ { "SubnetId": "subnet-8360a9e7", "ZoneName": "us-west-2a" }, { "SubnetId": "subnet-b7d581c0", "ZoneName": "us-west-2b" } ] }
  • 如需 API 詳細資訊,請參閱AWS CLI 命令參考SetSubnets中的。