def create_route53_zone(client, zone_name): """Create the given zone_name if it doesn't already exists. Also sets the SOA negative caching TTL to something short (300 seconds). Args: client (:class:`boto3.client.Client`): The connection used to interact with Route53's API. zone_name (str): The name of the DNS hosted zone to create. Returns: str: The zone id returned from AWS for the existing, or newly created zone. """ if not zone_name.endswith("."): zone_name += "." zone_id = get_or_create_hosted_zone(client, zone_name) old_soa = get_soa_record(client, zone_id, zone_name) # If the negative cache value is already 300, don't update it. if old_soa.text.min_ttl == "300": return zone_id new_soa = copy.deepcopy(old_soa) LOGGER.debug("Updating negative caching value on zone %s to 300.", zone_name) new_soa.text.min_ttl = "300" client.change_resource_record_sets(HostedZoneId=zone_id, ChangeBatch={ "Comment": "Update SOA min_ttl to 300.", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": zone_name, "Type": "SOA", "TTL": old_soa.ttl, "ResourceRecords": [{ "Value": str(new_soa.text) }] } }, ] }) return zone_id
def create_route53_zone(client, zone_name): """Creates the given zone_name if it doesn't already exists. Also sets the SOA negative caching TTL to something short (300 seconds). Args: client (:class:`botocore.client.Route53`): The connection used to interact with Route53's API. zone_name (string): The name of the DNS hosted zone to create. Returns: string: The zone id returned from AWS for the existing, or newly created zone. """ if not zone_name.endswith("."): zone_name += "." zone_id = get_or_create_hosted_zone(client, zone_name) old_soa = get_soa_record(client, zone_id, zone_name) # If the negative cache value is already 300, don't update it. if old_soa.text.min_ttl == "300": return zone_id new_soa = copy.deepcopy(old_soa) logger.debug("Updating negative caching value on zone %s to 300.", zone_name) new_soa.text.min_ttl = "300" client.change_resource_record_sets( HostedZoneId=zone_id, ChangeBatch={ "Comment": "Update SOA min_ttl to 300.", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": zone_name, "Type": "SOA", "TTL": old_soa.ttl, "ResourceRecords": [ { "Value": str(new_soa.text) } ] } }, ] } ) return zone_id
def register_zone_records( session: boto3.Session, zone_id: str, domain_name: str, load_balancer_arn: str, acm_valid_name: str, acm_valid_target: str, ) -> int: """ Creates the required zone records for this mephisto hosted zone. Requires the load balancer target, and the ACM certificate addresses Returns the change id """ # Get details about the load balancer ec2_client = session.client("elbv2") balancer = ec2_client.describe_load_balancers( LoadBalancerArns=[load_balancer_arn], )["LoadBalancers"][0] load_balancer_dns = balancer["DNSName"] load_balancer_zone = balancer["CanonicalHostedZoneId"] # Create the records client = session.client("route53") response = client.change_resource_record_sets( HostedZoneId=zone_id, ChangeBatch={ "Comment": "Creating records for Mephisto load balancer and DNS validations for certs", "Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": f"*.{domain_name}", "Type": "A", "AliasTarget": { "HostedZoneId": load_balancer_zone, "DNSName": load_balancer_dns, "EvaluateTargetHealth": True, }, }, }, { "Action": "CREATE", "ResourceRecordSet": { "Name": f"{domain_name}", "Type": "A", "AliasTarget": { "HostedZoneId": load_balancer_zone, "DNSName": load_balancer_dns, "EvaluateTargetHealth": True, }, }, }, { "Action": "CREATE", "ResourceRecordSet": { "Name": acm_valid_name, "Type": "CNAME", "TTL": 300, "ResourceRecords": [ { "Value": acm_valid_target }, ], }, }, ], }, ) return response["ChangeInfo"]["Id"]