【问题标题】:How can I attach multiple certificates to a load balancer in CDK?如何将多个证书附加到 CDK 中的负载均衡器?
【发布时间】:2020-10-16 08:52:57
【问题描述】:

我正在 ECS 上设置 grafana,并希望它可以通过不同子域上的两个不同 URL 访问:grafana.subdomain.mycompany.com 和 grafana.mycompany.com(我们需要两个 URL,因为子域版本需要grafana auth 代理,而非子域版本用于编辑仪表板)。

我的问题是 ApplicationLoadBalancedFargateService 只允许您提供一个证书。

到目前为止,我最好的解决方案是这样做。

        const grafanaService = new ecs_patterns.ApplicationLoadBalancedFargateService(this, 'Grafana', {
            cluster: this.props.ecsCluster,
            taskDefinition: taskdef,
            domainZone: this.props.hostedZone,
            domainName: this.grafanaDns,
            publicLoadBalancer: true
        })

        const sslListener = grafanaService.loadBalancer.addListener('SSL', {
            port: 443,
            certificates: [this.props.certificateHarvestSubdomain, this.props.certificateRootSubdomain],
            protocol: ApplicationProtocol.HTTPS
        })

        sslListener.addTargets('grafanaTarget', {
            targets: [grafanaService.service],
            port: 80,
            protocol: ApplicationProtocol.HTTP,
            healthCheck: {
                path: '/login',
                interval: cdk.Duration.minutes(1)
            }
        })

        new ARecord(this, 'Alias', {
            zone: this.props.hostedZone,
            target: route53.RecordTarget.fromAlias(new LoadBalancerTarget(grafanaService.loadBalancer)),
            recordName: 'grafana'
        })

这可以解决问题,但它给我留下了一个问题:我无法进行 HTTP 到 HTTPS 的重定向,因为 ApplicationLoadBalancedFargateService 已经在端口 80 上创建了一个侦听器,而我找不到访问现有侦听器的方法。如果我尝试创建另一个侦听器,则无法说明侦听器已存在。

【问题讨论】:

    标签: aws-cdk


    【解决方案1】:

    问题是,正如您所提到的,将在端口 80 上创建一个默认侦听器。 但是,您可以覆盖 listenerPort 以匹配 443。 尽管您需要指定domainName、domainZone 和certificate,因为如果缺少其中一个,则默认使用 HTTP 作为协议。 来自 HTTP -> HTTPS 的重定向不适用于此默认设置。 但是,您想一次添加多个证书。

    因此,您需要在 ALB-FGS 构造函数中传递 protocol 参数来覆盖 HTTP 默认值,以使 HTTP -> HTTPS 发生。 此时 ALB-FGS 的 redirectHTTP 参数正在使用该协议。

    要提供多个证书,您可以在(默认)443 监听器上显式添加证书。 从服务中获取侦听器对象并添加多个证书应该可以解决问题。

    在下面找到适应的代码。

    免责声明:我运行了 cdk synth 并没有实际部署它。 如果您在部署过程中发现错误,请告诉我。

       const grafanaService = new ecspatterns.ApplicationLoadBalancedFargateService(
          this,
          "Grafana",
          {
            cluster: this.props.ecsCluster,
            taskDefinition: taskdef,
            domainZone: this.props.hostedZone,
            domainName: this.grafanaDns,
            publicLoadBalancer: true,
            // here are the changes, mentioned in the description above for the FGS
            // if you only had one certificate, 
            // you would add it here directly and get rid of the `protocol` argument
            listenerPort: 443,
            protocol: ApplicationProtocol.HTTPS,
            redirectHTTP: true,
          }
        );
    
        grafanaService.listener.addCertificates("GrafanaHttpsCertificates", [
          this.props.certificateHarvestSubdomain,
          this.props.certificateRootSubdomain,
        ]);
    
    

    【讨论】:

    • 我得到侦听器端口“443”已在使用中(服务:AmazonElasticLoadBalancing;状态代码:400;错误代码:ValidationError;请求 ID:8fc42188-1778-4c98-ab22-54e9664cd20c;代理:null ) 我尝试删除 listenerPort 并离开协议,但得到了同样的错误。
    猜你喜欢
    • 2016-04-05
    • 2014-06-08
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多