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

本文件 AWS CLI 僅適用於 的第 1 版。如需與 第 2 版相關的文件 AWS CLI,請參閱 第 2 版使用者指南

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

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

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

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

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

主題

動作

下列程式碼範例示範如何使用 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範例會將 projectdepartment標籤新增至指定的負載平衡器。

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詳細資訊,請參閱 命令參考 AddTags中的 。 AWS CLI

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

AWS CLI

範例 1:建立HTTP接聽程式

下列create-listener範例會為指定的 Application Load Balancer 建立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

如需詳細資訊,請參閱 使用者指南中的教學課程:使用 建立 Application Load Balancer AWS CLI

範例 2:建立HTTPS接聽程式

下列create-listener範例會為指定的 Application Load Balancer 建立HTTPS接聽程式,將請求轉送至指定的目標群組。您必須為HTTPS接聽程式指定SSL憑證。您可以使用 AWS Certificate Manager () 建立和管理憑證ACM。或者,您可以使用 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

如需詳細資訊,請參閱 Application Load Balancer 使用者指南中的新增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

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

範例 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

如需詳細資訊,請參閱 TLS Network Load Balancer 使用者指南中的 Network Load Balancer 接聽程式

範例 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

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

範例 6:為指定的閘道和轉送建立接聽程式

下列create-listener範例會為指定的 Gateway 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" } ] } } ] } ] }

如需詳細資訊,請參閱 Gateway Load Balancer 使用者指南中的使用 開始使用 AWS CLI Gateway Load Balancer

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

下列程式碼範例示範如何使用 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" } ] }

如需詳細資訊,請參閱 使用者指南中的教學課程:使用 建立 Application Load Balancer 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" } ] }

如需詳細資訊,請參閱 使用者指南中的教學課程:使用 建立 Application Load Balancer AWS CLI

範例 3:建立 Network Load Balancer

下列create-load-balancer範例會建立面向網際網路的網路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" } ] }

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

範例 4:建立 Gateway Load Balancer

下列create-load-balancer範例會建立 Gateway 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" } ] }

如需詳細資訊,請參閱 Gateway Load Balancer 使用者指南 AWS CLI中的使用 的 Gateway Load Balancer 入門。

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

AWS CLI

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

如果 URL包含指定的模式,則下列create-rule範例會建立規則,將請求轉送至指定的目標群組。

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詳細資訊,請參閱 命令參考 CreateRule中的 。 AWS CLI

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

AWS CLI

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

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

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" } ] }

如需詳細資訊,請參閱 Application Load Balancer 使用者指南中的建立目標群組

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

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

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目標群組的TCP通訊協定、連接埠 80 和預設運作狀態檢查設定。

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" } ] }

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的建立目標群組

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

下列create-target-group範例會建立 Network Load Balancer 的目標群組,您可以將 Application Load Balancer 註冊為目標 (目標類型為 alb)。

aws elbv2 create-target-group --name my-alb-target --protocol TCP --port 80 --target-type alb --vpc-id 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" } ] }

如需詳細資訊,請參閱 Network Load Balancer 使用者指南中的使用 Application Load Balancer 建立目標群組做為目標

範例 5:為 Gateway Load Balancer 建立目標群組

下列create-target-group範例會為 Gateway 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" } ] }

如需詳細資訊,請參閱 Gateway 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詳細資訊,請參閱 命令參考 DeleteListener中的 。 AWS CLI

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DeleteRule中的 。 AWS CLI

下列程式碼範例示範如何使用 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

此命令不會產生輸出。

如需詳細資訊,請參閱 Application Load Balancer 指南 中的刪除Application Load Balancer

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DescribeRules中的 。 AWS CLI

下列程式碼範例示範如何使用 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範例顯示支援 1.3 TLS 通訊協定的政策名稱。此範例使用 --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" ]

如需詳細資訊,請參閱 Application Load Balancer 使用者指南中的安全政策

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 DescribeTags中的 。 AWS CLI

下列程式碼範例示範如何使用 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且目標類型為 instance或 ,則輸出會包含 屬性ip

{ "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、、 UDP或 TCP_TLS,則下列輸出包含 屬性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詳細資訊,請參閱 命令參考 ModifyListener中的 。 AWS CLI

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 ModifyRule中的 。 AWS CLI

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 RegisterTargets中的 。 AWS CLI

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 RemoveTags中的 。 AWS CLI

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

AWS CLI

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

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

命令:

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詳細資訊,請參閱 命令參考 SetIpAddressType中的 。 AWS CLI

下列程式碼範例示範如何使用 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詳細資訊,請參閱 命令參考 SetSubnets中的 。 AWS CLI