LoginSignup
3
2

More than 3 years have passed since last update.

【AWS】CloudFormationでRoute53 Private Hosted Zoneを構築してみる

Last updated at Posted at 2020-06-25

皆さんはEC2インスタンスなどAWS上のリソースの名前解決はどのようにしているでしょうか?
オンプレだとInternal DNS構築して管理すると思います
AWS上で管理するならマネージドの方がいいですよね
そこで今回はRoute53 Private Hosted Zoneを利用してマネージドで内部向けのDNS管理してみます
手ポチやawscliで地道に構築するより、CloudFormation (以下CFnと略) で構築した方が早いですしレコードの管理もしやすいので、CFnを使用した構築方法をご紹介します

CloudFormation

今回はシンプルルーティングポリシーという極一般的なDNSを構築していきます

テンプレート

Private Hosted Zoneの作成にはAWS::Route53::HostedZoneのVPCsでVPCオブジェクトを指定するだけです

AWSTemplateFormatVersion: "2010-09-09"

Parameters:
  HostedZoneName:
    Type: String
  HostedZoneVPCId:
    Type: AWS::EC2::VPC::Id
  FirstServerFQDN:
    Type: String
  FirstServerIPAddress:
    Type: CommaDelimitedList
  SecondServerFQDN:
    Type: String
  SecondServerIPAddress:
    Type: CommaDelimitedList

Resources:
  MyPrivateHostedZone:
    Type: AWS::Route53::HostedZone
    Properties:
      Name:
        Ref: HostedZoneName
      HostedZoneConfig:
        Comment: My Private Domain
      VPCs:
        - VPCId:
            Ref: HostedZoneVPCId
          VPCRegion:
            Fn::Sub: "${AWS::Region}"

  FirstServerRecordSet:
    Type: AWS::Route53::RecordSet
    Properties:
      Name:
        Ref: FirstServerFQDN
      Comment: FirstServer
      Type: A
      TTL: "300"
      HostedZoneId:
        Ref: MyPrivateHostedZone
      ResourceRecords:
        Ref: FirstServerIPAddress

  SecondServerRecordSet:
    Type: AWS::Route53::RecordSet
    Properties:
      Name:
        Ref: SecondServerFQDN
      Comment: SecondServer
      Type: A
      TTL: "300"
      HostedZoneId:
        Ref: MyPrivateHostedZone
      ResourceRecords:
        Ref: SecondServerIPAddress

注意点

VPCの関連付け

Private Hosted ZoneをCFnで構築する際に関連付けられるのは1つのVPCだけです
追加で関連付けを行いたい場合はマネジメントコンソールやawscliなどから操作を行ってください
このようなにCFnでできないことがあるので、実運用ではPrivate Hosted ZoneとRecord setでCFnテンプレートを分けた方がいいかもしれません

また異なるAWSアカウントのVPCを関連付けしたい場合は、awscliやAPIからでしかできないので注意が必要です
手順は作成済みの Amazon VPC と、別の AWS アカウントで作成したプライベートホストゾーンを関連付けるを参考にしてください

工夫点

複数IPアドレスの設定

AWS::Route53::RecordSetで複数のIPアドレスを設定できるように、FirstServerIPAddress / SecondServerIPAddressのパラメータタイプでCommaDelimitedListを指定します
これで冗長構成でも使えるCFnテンプレートになります

参考

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2