Elastic Load Balancing-版本 2 示例使用 AWS CLI - AWS Command Line Interface

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Elastic Load Balancing-版本 2 示例使用 AWS CLI

以下代码示例向您展示了如何在 Elastic Load Balancing-版本 2 中 AWS Command Line Interface 使用来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示了如何使用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"

以下代码示例显示了如何使用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

有关更多信息,请参阅教程:使用应用程序负载均衡器用户指南 AWS CLI中的创建应用程序负载均衡器

示例 2:创建HTTPS监听器

以下create-listener示例为指定的 Application Load Balancer 创建了一个HTTPS侦听器,该监听器将请求转发到指定的目标组。必须为HTTPS监听器指定SSL证书。您可以使用 Certificate Manager (ACM) 创建和管理 AWS 证书。或者,您可以使用SSL/TLS工具创建证书,获取由证书颁发机构 (CA) 签名的证书,然后将证书上传到 Ident AWS ity 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

有关更多信息,请参阅教程:使用网络负载均衡器用户指南 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 Bal ancer 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

有关更多信息,请参阅教程:使用网络负载均衡器用户指南 AWS CLI中的创建网络负载均衡器

示例 6:为指定的网关和转发创建侦听器

以下 create-listener 示例为指定的网关负载均衡器创建一个侦听器,用于将请求转发到指定的目标组。

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中的网关负载均衡器入门

以下代码示例显示了如何使用create-load-balancer

AWS CLI

示例 1:创建面向 Internet 的负载均衡器

以下 create-load-balancer 示例创建一个面向 Internet 的应用程序负载均衡器,并为指定的子网启用可用区。

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 示例创建一个内部应用程序负载均衡器,并为指定的子网启用可用区。

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:创建网络负载均衡器

以下 create-load-balancer 示例创建一个面向 Internet 的网络负载均衡器,并为指定的子网启用可用区。它使用子网映射,将指定的弹性 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" } ] }

有关更多信息,请参阅教程:使用网络负载均衡器用户指南 AWS CLI中的创建网络负载均衡器

示例 4:创建网关负载均衡器

以下 create-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 } ]

以下代码示例显示了如何使用create-target-group

AWS CLI

示例 1:为 Application Load Balancer 创建目标组

以下 create-target-group 示例为应用程序负载均衡器创建目标组,以便您按实例 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 示例为应用程序负载均衡器创建目标组,在其中目标为 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" } ] }

有关更多信息,请参阅应用程序负载均衡器用户指南中的 Lambda 函数作为目标

示例 3:为 Network Load Balancer 创建目标组

以下 create-target-group 示例为网络负载均衡器创建目标组,以便您按 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:创建目标组以将流量从网络负载均衡器路由到 Application Load Balancer

以下create-target-group示例为网络负载均衡器创建目标组,您将在其中将应用程序负载均衡器注册为目标(目标类型为alb)。

aws elbv2 create-target-group--name--portocol--port 80--target-type alb my-alb-target--vpc-id vpc-3ac0 TCP fb5f

输出:

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

有关更多信息,请参阅网络负载均衡器用户指南中的创建以 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

以下代码示例显示了如何使用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

以下代码示例显示了如何使用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

此命令不生成任何输出。

有关更多信息,请参阅 App lication 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

以下代码示例显示了如何使用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" } ] } ] }

以下代码示例显示了如何使用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、、或 TCP _ TLSUDP,则以下输出将包含属性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" } ] }

以下代码示例显示了如何使用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" } ] } ] }

以下代码示例显示了如何使用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

以下代码示例显示了如何使用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

以下代码示例显示了如何使用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" }

以下代码示例显示了如何使用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" } ] }