簡體   English   中英

如何在 AWS CDK 中創建偵聽器規則?

[英]How to create listener rule in AWS CDK?

嗨,我在工作 AWS CDK。 我正在嘗試使用應用程序負載平衡創建 ECS。 我創建了 ECS 集群、任務定義、負載均衡器和監聽器。

下面是我的負載均衡器。

lb = elbv2.ApplicationLoadBalancer(
            self, "MWSLoadBalancer",
            vpc = vpc,
            internet_facing= True,
            security_group= mws_vpc_sg_alb
        )

下面是我的聽眾

listener = lb.add_listener(
            "MWSLoadBalanceListener",
            port = 80,
            open = True,
        )

下面是健康檢查

health_check = elbv2.HealthCheck(
            interval=core.Duration.seconds(60),
            path="/",
            timeout=core.Duration.seconds(5)
        )

下面是將 ALB 添加到 ECS。

target = listener.add_targets(
            "MWSLoadBalancerTargetGroup",
            port=80,
            targets=[service],
            health_check=health_check,
        )

根據https://docs.aws.amazon.com/cdk/api/latest/docs/aws-elasticloadbalancingv2-readme.html#targets-and-target-groups如果我們添加我們的平衡目標(例如 AutoScalingGroups、ECS 服務或單個實例)直接發送到您的偵聽器,將自動為您創建相應的 TargetGroup。 所以我沒有創建任何目標組,而是在我做 cdk 合成時自動創建的。 接下來,我想為我的 ALB 設置監聽器規則。 監聽規則的雲形成模板如下。

MWSLoadBalancerHttpListenerRule:
    Type: "AWS::ElasticLoadBalancingV2::ListenerRule"
    DependsOn: MWSLoadBalancer
    Properties:
      Actions:
        - Type: forward
          TargetGroupArn: !Ref MWSTargetGroup
      ListenerArn: !Ref MWSLoadBalanceListener
      Conditions:
        - Field: path-pattern
          Values:
            - "/api/*"
      Priority: 3

我嘗試創建如下的監聽器規則。

 elbv2.ApplicationListenerRule(self, id = "listner rule", path_pattern="/api/*", priority = 1, listener = listener)

這是扔

偵聽器規則至少需要一項操作

有人可以幫我找出這個錯誤嗎?

創建 ApplicationListenerRule 時,您必須指定一個操作,它是 target_groups、fixed_response 或 redirect_response 之一。

target_groups (Optional[List[IApplicationTargetGroup]]) – 將請求轉發到的目標組。 只能指定 fixedResponse、redirectResponse 或 targetGroups 之一。

elbv2.ApplicationListenerRule(
    self, 
    id="listener rule", 
    path_pattern="/api/*", 
    priority=1, 
    listener=listener, 
    target_groups=[target]
)

需要注意的是,這種情況下有一個 CDK 模式 aws-ecs-patterns,它為常見的架構模式提供了更高級別的構造。

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-patterns-readme.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM