Last active
June 12, 2022 16:57
-
-
Save mogaal/13eb37dd4212ad699ae692b3c7430844 to your computer and use it in GitHub Desktop.
python script to update external-dns owner value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import boto3 | |
client = boto3.client('route53') | |
def update_txt_record(HostzoneID, Name, value): | |
try: | |
response = client.change_resource_record_sets( | |
HostedZoneId=HostzoneID, | |
ChangeBatch= { | |
'Comment': 'add %s -> %s' % (Name, value), | |
'Changes': [ | |
{ | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': Name, | |
'Type': 'TXT', | |
'TTL': 300, | |
'ResourceRecords': [{'Value': value}] | |
} | |
}] | |
}) | |
except Exception as e: | |
print(e) | |
def main(): | |
response = client.list_hosted_zones() | |
for x in response['HostedZones']: | |
hostzoneID = [x.strip() for x in x['Id'].split('/hostedzone/')] | |
recordSets = client.list_resource_record_sets(HostedZoneId=hostzoneID[1]) | |
for i in recordSets['ResourceRecordSets']: | |
if i['Type'] == 'TXT' and i['ResourceRecords'][0]['Value']: | |
match = re.search("owner=default", i['ResourceRecords'][0]['Value']) | |
if match: | |
print('Entry {} from hostzone {} has owner=default'.format(i['Name'], hostzoneID[1])) | |
newValue = re.sub('owner=default', 'owner=live-1', i['ResourceRecords'][0]['Value']) | |
update_txt_record(hostzoneID[1], i['Name'], newValue) | |
print("Done hostzone {}".format(hostzoneID[1])) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment