如何在 Cloudformation 模板中引用现有的 ELB DNSName

how to reference existing ELB DNSName in Cloudformation template

我们有 Cloudformation 模板,我们通过它为我们的产品部署基础设施资源。以下是通过 CF 模板创建的 AWS 组件: 1. 网络组件。像 VPC、子网、安全组等。 2. IAM 角色和策略。 3.电子病历 4.EKS 5.麦斯隆 6.RDS 7. 橡皮筋

同样在我们的 Cloudformation 模板中,我们几乎没有像 "Custom::KubeManifest" 这样的自定义资源。我们通过它在 AWS EKS 集群中部署对象。我们的 kubernetes 对象之一是 "Service" 对象。它为内部服务创建一个服务端点,以便来自 public 网络的请求可以到达我们的 kubernetes 集群。

我们想检查是否可以在 Cloudformation 模板中引用现有的 ELB DNS 名称,以便我们可以在输出中显示 ELB DnsName。

例如,当我们调用 "Custom::KubeManifest" 资源时,如下模板:

  ServiceDeployment:
    Type: "Custom::KubeManifest"
    Version: '1.0'
    Properties:
      ServiceToken: !Ref KubeManifestLambdaArn
      KubeConfigPath: !Sub "s3://${KubeConfigS3Bucket}/${KubeConfigS3Key}"
      KubeConfigKmsContext: !Ref KmsContext
      Manifest:
        apiVersion: v1
        kind: Service
        metadata:
          name: test
          labels:
            app: client
            tier: master
        spec:
          selector:
            app: client
            tier: master
          ports:
          - name: client-api
            port: 9877
            protocol: TCP
          - name: client-snapshots
            port: 9878
            protocol: TCP
          - name: client-support
            port: 9881
            protocol: TCP
  UiDeployment:
    Type: "Custom::KubeManifest"
    Version: '1.0'
    Properties:
      ServiceToken: !Ref KubeManifestLambdaArn
      KubeConfigPath: !Sub "s3://${KubeConfigS3Bucket}/${KubeConfigS3Key}"
      KubeConfigKmsContext: !Ref KmsContext
      Manifest:
        apiVersion: v1
        kind: Service
        metadata:
          name: client-ui
          annotations:
            service.beta.kubernetes.io/aws-load-balancer-internal: 0.0.0.0/0
            service.beta.kubernetes.io/aws-load-balancer-proxy-protocol: '*'
            service.beta.kubernetes.io/aws-load-balancer-type: nlb
            service.beta.kubernetes.io/aws-load-balancer-backend-protocol: 'tcp'
            service.beta.kubernetes.io/aws-load-balancer-ssl-ports: "tcp"
            service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout: "60"
          labels:
            app: client
            tier: master
        spec:
          type: LoadBalancer
          selector:
            app: client
            tier: master
          ports:
          - name: client-ui
            port: 80
            protocol: TCP
            targetPort: 8800
          - name: client-ui-https
            port: 443
            protocol: TCP
            targetPort: 8800

它在 AWS 帐户中创建一个 ELB,并将其映射到 EKS 集群中的服务端点。现在我们想知道是否可以通过任何函数引用新创建的 ELB DnsNames 并将其显示为输出。

这是我的 YAML 示例

Resources:
  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: !Ref EnvironmentName
      Subnets: !Ref Subnets
      SecurityGroups:
        - !Ref SecurityGroup
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref DefaultTargetGroup

LoadBalancerLoadBAlanceListener 都必须包括在内。然后,您必须添加声明您希望可用于描述堆栈 API 调用的值的输出。

Outputs:
  LoadBalancer:
    Description: A reference to the Application Load Balancer
    Value: !Ref LoadBalancer

  LoadBalancerUrl:
    Description: The URL of the ALB
    Value: !GetAtt LoadBalancer.DNSName

  Listener:
    Description: A reference to a port 80 listener
    Value: !Ref LoadBalancerListener

我们查看了 post: aws-quickstart-examples-eks

我们可以使用

获取映射到服务端点的新创建的 loadBalancer 的 DnsNames

Custom::KubeGet

资源。

您可以参考 DNS 名称:

Fn::GetAtt: [LoadBalancer, DNSName]

LoadBalancer 是创建的负载均衡器资源。