简体   繁体   中英

How to create listener rule in AWS CDK?

Hi I am in working AWS CDK. I am trying to create ECS with Application Load balance. I create ECS Cluster, Task Definition, Load balancer and listner.

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.

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. So I have not created any Target groups but one created automatically When I do cdk synth. Next I want to have listner rule to my 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.

target_groups (Optional[List[IApplicationTargetGroup]]) – Target groups to forward requests to. Only one of fixedResponse, redirectResponse or targetGroups can be specified.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM