如果可以使用AWS Cloudformation模板将Loadbalancers DNSname连接到Route53?

时间:2016-01-07 16:11:24

标签: json amazon-web-services amazon-cloudformation

我尝试连接的是Loadbalancer DNS名称到Route53。 让我们看看例子。 这是资源中模板的Loadbabancer:

"RestELB" : {
        "Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
        "DependsOn": "AttachGateway",
        "Properties": {
            "LoadBalancerName": {"Fn::Join": ["",["Rest-ELB-", {"Ref": "VPC"}]]},
            "CrossZone" : "true",
            "Subnets": [{ "Ref": "PublicSubnet1" },{ "Ref": "PublicSubnet2" }],
            "Listeners" : [
                {"LoadBalancerPort" : "80", "InstancePort" : "80","Protocol" : "HTTP"},
                {"LoadBalancerPort" : "6060", "InstancePort" : "6060","Protocol" : "HTTP"}
            ],
        }
    },

这是Route53:

"ApiRecordSet" : {
      "Type" : "AWS::Route53::RecordSet",
      "Properties" : {
        "AliasTarget" :{
          "DNSName" : [
            {"Fn::Join": ["", [{"ElasticLoadBalancer": "DNSName"},"."]]}
          ],
          "EvaluateTargetHealth" : "Boolean",
          "HostedZoneId" : "String"
        },
        "HostedZoneName" : "example.net.",
        "Comment" : "A records for my frontends.",
        "Name" : "api.example.net.",
        "Type" : "A",
        "TTL" : "900",
      }
    }

只是把{" ElasticLoadBalancer":" DNSName"}放弃了。有人可以建议或给我正确的方法来添加这个吗?

谢谢!

4 个答案:

答案 0 :(得分:5)

您很可能希望为引用为DNSName的LoadBalancer获取属性RestELB。所以你需要Fn::GetAtt喜欢(未经测试)

的东西
   "ApiRecordSet" : {
      "Type" : "AWS::Route53::RecordSet",
      "Properties" : {
        "AliasTarget" :{
          "DNSName" : { "Fn::GetAtt" : [ "RestELB", "DNSName" ]},
          "EvaluateTargetHealth" : "Boolean",
          "HostedZoneId" : "String"
        },
        "HostedZoneName" : "example.net.",
        "Comment" : "A records for my frontends.",
        "Name" : "api.example.net.",
        "Type" : "A"
      }
    }

答案 1 :(得分:4)

请务必阅读AWS :: Route53 :: Recordset AliasTarget类型上的CloudFormation文档: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget.html

这是在为ELB创建别名目标时在CloudFormation中的外观:

"Route53LoadBalancerAlias" : {
      "Type" : "AWS::Route53::RecordSet",
      "Properties" : {
          "AliasTarget" : {
          "DNSName" : { "Fn::GetAtt" : [ "ELB", "DNSName" ]},
          "EvaluateTargetHealth" : False,
          "HostedZoneId" : { "Fn::GetAtt" : [ "ELB", "CanonicalHostedZoneID" ]}
        },

对于负载均衡器,请使用负载均衡器的规范托管区域ID。对于Amazon S3,请使用托管区域网站端点的托管区域ID。对于CloudFront,请使用Z2FDTNDATAQYW2。有关其他服务的托管区域ID的列表,请参阅AWS区域和端点中的相关服务。

答案 2 :(得分:2)

对于在2018年阅读此答案的任何人,我使用CanonicalHostedZoneNameID而不是CanonicalHostedZoneID

让我的工作
"MyRecordSet": {
      "Type": "AWS::Route53::RecordSet",
      "Properties": {
        "HostedZoneName" : "example.com.",
        "Name": "abc.example.com.",
        "Type": "A",
        "AliasTarget": {
            "HostedZoneId" : {"Fn::GetAtt": ["MyELB", "CanonicalHostedZoneNameID"]},
            "DNSName": {"Fn::GetAtt": ["MyELB", "DNSName"]},
            "EvaluateTargetHealth": "false"
        }
      }
    }

答案 3 :(得分:0)

YAML,用于部署引用在同一模板中部署的ELB的RecordSet。

  Route53RecordSet:
    Type: AWS::Route53::RecordSet
    Properties: 
      Name: !Ref HostName
      HostedZoneId: !Ref HostedZoneId
      Type: A
      AliasTarget: 
        DNSName: !GetAtt ElasticLoadBalancer.DNSName
        HostedZoneId: !GetAtt ElasticLoadBalancer.CanonicalHostedZoneIDe