如何在CDK(python)中正确编码AWS ALB重定向

如何在CDK(python)中正确编码AWS ALB重定向,python,aws-cdk,aws-application-load-balancer,Python,Aws Cdk,Aws Application Load Balancer,学习AWS CDK(来自terraform)。我目前正在努力,如何使用编写一段代码将允许我创建重定向,如下面的屏幕截图所示: 到目前为止,我拥有的代码: class LoadBalancer(core.Construct): def __init__(self, scope: core.Construct, construct_id: str, props): super().__init__(scope, construct_id) self.props = props

学习AWS CDK(来自terraform)。我目前正在努力,如何使用编写一段代码将允许我创建重定向,如下面的屏幕截图所示:

到目前为止,我拥有的代码:

class LoadBalancer(core.Construct):

def __init__(self, scope: core.Construct, construct_id: str, props):
    super().__init__(scope, construct_id)
    self.props = props

def load_balancer(self, security_group, fargate_service, arn_certificates_list):
    load_balancer = alb.ApplicationLoadBalancer(
        self,
        "TestFargateALB",
        vpc=self.props["vpc"],
        deletion_protection=True if self.props["environment"].lower() == "prod" else False,
        http2_enabled=True,
        internet_facing=True,
        security_group=security_group
    )

    load_balancer.set_attribute(key="routing.http.drop_invalid_header_fields.enabled", value="true")

    http_load_balancer_listener = load_balancer.add_listener(
        'http_listener', port=80, open=False
    )

    https_load_balancer_listener = load_balancer.add_listener(
        "https_listener", port=443, open=False, certificate_arns=arn_certificates_list
    )

    https_target_group = https_load_balancer_listener.add_targets(
        'ecs_target_group',
        targets=[fargate_service],
        port=80,
        protocol=alb.ApplicationProtocol.HTTP,
        deregistration_delay=core.Duration.seconds(amount=10)
    )

    https_target_group.configure_health_check(
        healthy_http_codes="200,301,302",
        healthy_threshold_count=3,
        interval=core.Duration.seconds(10),
        timeout=core.Duration.seconds(5),
        unhealthy_threshold_count=5,
        path="/"
    )

    http_load_balancer_listener.add_action(
        'DefaultAction',
        action=alb.ListenerAction(action_json=alb.CfnListener.ActionProperty(
            type="redirect",
            redirect_config=alb.CfnListener.RedirectConfigProperty(
                status_code="HTTP_301",
                host="#{host}",
                protocol="HTTPS",
                port="443",
                path="#{path}",
                query="#{query}"
            )
        ))
    )

    alb.ApplicationListenerRule(self, "HttpsRule",
                                listener=https_load_balancer_listener,
                                priority=1,
                                path_pattern="/",
                                target_groups=[https_target_group]
                                )

    return load_balancer
部署期间CDK抛出的错误:

5:04:16 PM | CREATE|u FAILED | AWS::ElasticLoadBalancingV2::Listener | testaltestfargate…tplistener3A9BAD2E路径参数必须是有效路径,应以“/”开头,并且最多可以包含以下占位符之一:“#{Path}”、“#{host}”、“#{port}”。(服务:ElasticLoadBalancingV2,状态代码:400,请求ID:9cc3b04c-0787-4455-a7ee-2a1b3e9a30b3,扩展请求ID:null)


有人能帮我正确地写这篇文章吗?

我们只需要在loadbalancer上调用
addRedirect
方法

方法的默认参数已存在

{
   sourcePort: 80,
   targetPort: 443,
   sourceProtocol: elbv2.ApplicationProtocol.HTTP,
   targetProtocol: elbv2.ApplicationProtocol.HTTPS,
}
无论如何,这些都是该方法的默认选项。所以,我们只需要一行
lb.addRedirect()

Python方法将是类似的文档:

这将添加侦听器规则,将80重定向到443,状态为HTTP 301


我们可以简单地在负载平衡器本身上使用
add\u redirect\u response
而不是添加带有重定向规则的操作,它将类似于负载平衡器。add\u redirect\u response,如Thank you,I'm blind:),简单的重定向方法。现在我有其他问题(ACM证书)将做另一个主题
const vpc = ec2.Vpc.fromLookup(this, "myVpc", {
  isDefault: false,
  vpcId: "vpc-0cb67dd096388f8ca",
});

const lb = new elbv2.ApplicationLoadBalancer(this, "LB", {
  vpc,
  internetFacing: true,
});

lb.addRedirect();
lb.add_redirect(
    source_protocol=elbv2.ApplicationProtocol.HTTPS,
    source_port=80,
    target_protocol=elbv2.ApplicationProtocol.HTTP,
    target_port=443
)