Sign in

Change TTL value of AWS Route 53 DNS records

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task modifies the Time-to-Live (TTL) value for a specific DNS record in AWS Route 53.

import boto3 from botocore.exceptions import ClientError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def change_ttl(record_data_list, new_ttl): """ Change the TTL value of multiple DNS records in AWS Route 53. Args: record_data_list (list): List of dictionaries, each containing hosted_zone_id, record_name, and record_type. new_ttl (int): The new TTL value in seconds. Returns: None """ # Initialize the Route 53 client client = boto3.client('route53',aws_access_key_id=access_key,aws_secret_access_key=secret_key) for record_data in record_data_list: try: # Extract the necessary data from each record_data dictionary hosted_zone_id = record_data['HostedZoneId'] record_name = record_data['RecordName'] record_type = record_data['RecordType'] # Fetch the existing resource records for each DNS entry response = client.list_resource_record_sets( HostedZoneId=hosted_zone_id, StartRecordName=record_name, StartRecordType=record_type, MaxItems='1' ) # Locate the existing DNS record current_record_set = next((record for record in response['ResourceRecordSets'] if record['Name'] == record_name and record['Type'] == record_type), None) # Validate if the DNS record exists if current_record_set is None: print(f"DNS record not found for {record_name}. Skipping.") continue # Prepare the change batch for modifying the DNS record change_batch = { 'Changes': [{ 'Action': 'UPSERT', 'ResourceRecordSet': { 'Name': record_name, 'Type': record_type, 'TTL': new_ttl, 'ResourceRecords': current_record_set['ResourceRecords'] # Use existing ResourceRecords } }] } # Execute the change in Route 53 client.change_resource_record_sets( HostedZoneId=hosted_zone_id, ChangeBatch=change_batch ) print(f"Successfully changed TTL for {record_name}") except ClientError as ce: print(f"ClientError for {record_name}: {ce}") except Exception as e: print(f"Unexpected error for {record_name}: {e}") ''' # Example record data # Replace with your specific details record_data_list = [ {'HostedZoneId': '/hostedzone/XXXXXXXX', 'RecordName': 'example.com.', 'RecordType': 'A', 'TTL': 300}, {'HostedZoneId': '/hostedzone/XXXXXXXX', 'RecordName': 'example.com.', 'RecordType': 'SOA', 'TTL': 900} ] ''' #new_ttl = 86400 # 1 day in seconds (86400) if records: # Function call to change TTL for multiple records change_ttl(records, int(new_ttl_secs)) else: print("No records were passed.")
copied