您好,我正在使用 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)
)
下面是向ECS添加ALB。
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)
这是 throw
Listener rule needs at least one action
有人可以帮我识别这个错误吗?
最佳答案
创建ApplicationListenerRule时,您必须指定一个操作,该操作是target_groups、fixed_response或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]
)
请注意,这种情况有一个 CDK 模式,即 aws-ecs-patterns,它为常见架构模式提供了更高级别的构造。
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-ecs-patterns-readme.html
关于python - 如何在AWS CDK中创建监听器规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59188959/