実行条件
- awscliをインストールしておく
- EC2インスタンスにアクセスキー ID とシークレットアクセスキーを登録する
#route53.py
import commands
import json
MKDIR=commands.getoutput("mkdir RECORD")
HostZone = commands.getoutput("aws route53 list-hosted-zones")
HostZoneJson = json.loads(HostZone)
for h in HostZoneJson["HostedZones"]:
HostZoneId = h['Id']
HostZoneName = h['Name']
File = "RECORD/" + HostZoneName + "txt"
f = open(File, 'w')
AwsCli = "aws route53 list-resource-record-sets --hosted-zone-id " + HostZoneId
Record = commands.getoutput(AwsCli)
RecordJson = json.loads(Record)
for r in RecordJson['ResourceRecordSets']:
Name = r.get('Name')
Type = r.get('Type')
TTL = r.get('TTL')
Resouce = r.get('ResourceRecords')
Alias = r.get('AliasTarget')
if Alias != None:
DNSName = Alias.get('DNSName')
OutPut = Name + "," + Type + "," + "Alias " + DNSName + "," + "60" + "\n"
f.write(OutPut)
else:
Value = ""
for v in Resouce:
Value = Value + " " + v.get('Value')
OutPut = Name + "," + Type + "," + Value + "," + str(TTL) + "\n"
f.write(OutPut)
実行方法
python route53.py
ホストゾーン毎にファイルが作成され、レコード一覧が各ファイルに記載される