文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配使用 CreateRule
與 CLI
下列程式碼範例示範如何使用 CreateRule
。
- 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
\ --priority5
\ --conditionsfile://conditions-pattern.json
--actionsType=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
\ --priority10
\ --conditionsfile://conditions-host.json
\ --actionsfile://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
\ --priority20
\ --conditionsfile://conditions-source-ip.json
\ --actionsfile://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
。
-