简体   繁体   English

如何在 AWS CDK 中创建侦听器规则?

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

Hi I am in working AWS CDK.嗨,我在工作 AWS CDK。 I am trying to create ECS with Application Load balance.我正在尝试使用应用程序负载平衡创建 ECS。 I create ECS Cluster, Task Definition, Load balancer and listner.我创建了 ECS 集群、任务定义、负载均衡器和监听器。

Below is my load balancer.下面是我的负载均衡器。

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

Below is my listener下面是我的听众

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

Below is health check下面是健康检查

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

Below is adding ALB to ECS.下面是将 ALB 添加到 ECS。

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

As per https://docs.aws.amazon.com/cdk/api/latest/docs/aws-elasticloadbalancingv2-readme.html#targets-and-target-groups If we add our balancing targets (such as AutoScalingGroups, ECS services or individual instances) to your listener directly, the appropriate TargetGroup will be automatically created for you.根据https://docs.aws.amazon.com/cdk/api/latest/docs/aws-elasticloadbalancingv2-readme.html#targets-and-target-groups如果我们添加我们的平衡目标(例如 AutoScalingGroups、ECS 服务或单个实例)直接发送到您的侦听器,将自动为您创建相应的 TargetGroup。 So I have not created any Target groups but one created automatically When I do cdk synth.所以我没有创建任何目标组,而是在我做 cdk 合成时自动创建的。 Next I want to have listner rule to my ALB.接下来,我想为我的 ALB 设置监听器规则。 Cloud formation template of listner rule is as below.监听规则的云形成模板如下。

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

I tried to create listner rule as below.我尝试创建如下的监听器规则。

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

This is throwing这是扔

Listener rule needs at least one action侦听器规则至少需要一项操作

Can someone help me to identify this error?有人可以帮我找出这个错误吗?

When creating an ApplicationListenerRule you have to specify an action, which is one of target_groups, fixed_response or redirect_response.创建 ApplicationListenerRule 时,您必须指定一个操作,它是 target_groups、fixed_response 或 redirect_response 之一。

target_groups (Optional[List[IApplicationTargetGroup]]) – Target groups to forward requests to. target_groups (Optional[List[IApplicationTargetGroup]]) – 将请求转发到的目标组。 Only one of fixedResponse, redirectResponse or targetGroups can be specified.只能指定 fixedResponse、redirectResponse 或 targetGroups 之一。

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

Just to note that there is a CDK pattern for this case, aws-ecs-patterns, which provides higher level constructs for common architectural patterns.需要注意的是,这种情况下有一个 CDK 模式 aws-ecs-patterns,它为常见的架构模式提供了更高级别的构造。

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-patterns-readme.html 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