5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Route53でDNS切り替え自動化の実装めも

Last updated at Posted at 2015-01-24

Route53でDNS切り替え自動化の実装めも

環境切り替えは、
Route53のWeighted Roud Robin(WRR)レコードでWeighted値の変更で実現

→cli53で簡単にWRRレコード更新できないため、dnscurl.plを利用

実行環境

EC2(AmazonLinux)
AWSCLI,cli53,jq,

実装めも

①Hosted Zone作成(存在しない場合)
xxx.xxx
Hosted Zone ID:めもしておく

②2つのELBを作成
ELB名、Endpoint

③Route53でWRRレコード作成(AレコードのAlias)
Alias Target:ELBのEndpoint
Alias Hosted Zone ID:めもしておく
Set ID:env1/env2
Weighted:
ennv1 -> 255
ennv2 -> 0

切り替えスクリプトの概要:

引数で受け取る:
HOSTED_ZONE_NAME=xxx.xxx
HOSTED_ZONE_ID=XXXXXXXXXXX
RECORD_SET=xxx.xxx.xxx
ALIAS_HOSTED_ZONE_ID=YYYYYYYYYY

切り替え前のWeight値取得


ENV1_WEIGHT=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}"|jq ".ResourceRecordSets[] | select(.Name == \"${RECORD_SET}.\" and .SetIdentifier == \"env1\") |.Weight")
ENV2_WEIGHT=$(aws route53 list-resource-record-sets --hosted-zone-id "${HOSTED_ZONE_ID}"|jq ".ResourceRecordSets[] | select(.Name == \"${RECORD_SET}.\" and .SetIdentifier == \"env2\") |.Weight")

weighted値0/255で新旧環境判断、切り替え後のWeighted値

(0->255,255->0)
NEW_ENV1_WEIGHT
NEW_ENV2_WEIGHT
エラー処理

DNS切り替え


sed \
-e "s/%ENV1_WEIGHT%/${NEW_ENV1_WEIGHT}/g" \
-e "s/%ENV2_WEIGHT%/${NEW_ENV2_WEIGHT}/g" \
-e "s/%ELB_ENDPOINT_1%/${ELB_ENDPOINT_1}/g" \
-e "s/%ELB_ENDPOINT_2%/${ELB_ENDPOINT_2}/g" \
-e "s/%RECORD_SET%/${RECORD_SET}/g" \
-e "s/%ALIAS_HOSTED_ZONE_ID%/${ALIAS_HOSTED_ZONE_ID}/g" \
/path/to/route53_template_xxxxxx.xml \
>/path/to/route53_work_xxxxxx.xml

change_result=$(/path/to/dnscurl.pl --keyfile /path/to/.aws_secrets --keyname=as-aws-account -- -H "Content-Type: text/xml; charset=UTF-8" -X POST --upload-file /path/to/route53_work_xxxxxx.xml https://route53.amazonaws.com/2013-04-01/hostedzone/${HOSTED_ZONE_ID}/rrset)

エラー処理

テンプレート

/path/to/route53_template_xxxxxx.xml

<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
   <ChangeBatch>
      <Comment>
      This change creates two weighted resource record sets,  each of which has one value.
      </Comment>
      <Changes>
         <Change>
            <Action>UPSERT</Action>
            <ResourceRecordSet>
               <Name>%RECORD_SET%.</Name>
               <Type>A</Type>
               <SetIdentifier>env1</SetIdentifier> 
               <Weight>%ENV1_WEIGHT%</Weight>
               <AliasTarget>
                   <HostedZoneId>%ALIAS_HOSTED_ZONE_ID%</HostedZoneId>
                   <DNSName>%ELB_ENDPOINT_1%</DNSName>
                  <EvaluateTargetHealth>false</EvaluateTargetHealth>
               </AliasTarget>
            </ResourceRecordSet>
         </Change>
         <Change>
            <Action>UPSERT</Action>
            <ResourceRecordSet>
               <Name>%RECORD_SET%.</Name>
               <Type>A</Type>
               <SetIdentifier>env2</SetIdentifier> 
               <Weight>%ENV2_WEIGHT%</Weight>
               <AliasTarget>
                  <HostedZoneId>%ALIAS_HOSTED_ZONE_ID%</HostedZoneId>
                  <DNSName>%ELB_ENDPOINT_2%</DNSName>
                  <EvaluateTargetHealth>false</EvaluateTargetHealth>
              </AliasTarget>
            </ResourceRecordSet>
         </Change>
      </Changes>
   </ChangeBatch>
</ChangeResourceRecordSetsRequest>
5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?