使用Ruby AWS SDK为Cloudfront分发创建Route53别名

时间:2014-10-06 10:34:23

标签: ruby amazon-web-services amazon-cloudfront amazon-route53

我尝试自动创建在Route53托管DNS上提供的S3支持的CloudFront缓存网站。

我可以使用静态网站托管创建S3存储桶,我知道如何创建CloudFront分配和Route53记录,但是在创建作为CloudFront分配的别名的Route53记录时,需要提供一个"托管区域ID" - Route53接口没有问题,但我无法弄清楚如何使用AWS SDK获取该信息。

以下是我所拥有的:

def create_cf(domain)
  AWS::CloudFront.new.client.create_distribution distribution_config: createOptions(domain)
end

def create_r53(cfdistro, domain)
  target = {
    hosted_zone_id: cfdistro.id,
    dns_name: cfdistro.domain_name,
    evaluate_target_health: false
  }
  AWS::Route53.new.hosted_zones[myzone].rrsets.create "#{domain}.", 'A',
    alias_target: target
end

不幸的是,从id(或create_distribution)返回的字段get_distribution是CloudFront控制台显示为分发ID的字段,但不是Route53的控制台当我选择CloudFront分配作为别名目标时显示。我实际上无法确定在CloudFront控制台中找到托管区域ID的位置!

1 个答案:

答案 0 :(得分:2)

好的,这很愚蠢 - 如AWS Route53 documentation中所述,所有CloudFront发行版都托管在ID为Z2FDTNDATAQYW2的区域中。

"托管区域ID" (我在进一步阅读后发现)是Route53区域的加密标识符。公共AWS域(例如cloudfront.net)的Route53区域ID由亚马逊在其文档中发布,完全用于此目的。

上面的代码应该是:

def create_r53(cfdistro, domain)
  target = {
    hosted_zone_id: 'Z2FDTNDATAQYW2',
    dns_name: cfdistro.domain_name,
    evaluate_target_health: false
  }
  AWS::Route53.new.hosted_zones[myzone].rrsets.create "#{domain}.", 'A',
    alias_target: target
end