Sign in

Get DNS Records with TTL under given time duration

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

This task fetches DNS records in AWS Route 53 with a TTL value less than a specified number of hours. Identifying records with low TTL can help in optimizing DNS query costs and performance. A lower TTL means more frequent DNS queries, which could lead to higher costs and increased load on the DNS servers.

import boto3 from botocore.exceptions import ClientError creds = _get_creds(cred_label)['creds'] access_key = creds['username'] secret_key = creds['password'] def get_ttl_under_threshold(threshold): """ Retrieve DNS records with a TTL under the specified threshold. Args: threshold (int): TTL threshold in seconds. Returns: list: List of records with TTL under the threshold. """ # Initialize a list to store records with low TTL lower_ttl_records = [] try: # Create a Route53 client client = boto3.client('route53',aws_access_key_id=access_key,aws_secret_access_key=secret_key) # Get all hosted zones hosted_zones = client.list_hosted_zones()['HostedZones'] # Loop through each hosted zone for zone in hosted_zones: zone_id = zone['Id'] # Use pagination to get all resource record sets for this hosted zone paginator = client.get_paginator('list_resource_record_sets') # Loop through pages of resource record sets for page in paginator.paginate(HostedZoneId=zone_id): # Loop through each record set on this page for record_set in page['ResourceRecordSets']: # Check if the record set has a TTL and if it's under the threshold if 'TTL' in record_set and record_set['TTL'] < threshold: record_data = { 'HostedZoneId': zone_id, 'RecordName': record_set['Name'], 'RecordType': record_set['Type'], 'TTL': record_set['TTL'] } # Append the record data to our list lower_ttl_records.append(record_data) # Handle specific boto3 client errors except ClientError as ce: print(f"ClientError: {ce}") # Handle all other exceptions except Exception as e: print(f"Unexpected error: {e}") # Return the list of records with TTL under the threshold return lower_ttl_records # Define the TTL threshold in seconds (1 hour = 3600 seconds) #threshold_value = 3600 # Get and store records with TTL under the threshold records = get_ttl_under_threshold(threshold_value_secs) # Display the records if records: print("Records with TTL under the threshold:") for record in records: print(record) else: print("No records found with TTL under the threshold.") context.proceed = False
copied