Help Center> FunctionGraph> FAQ> General FAQs> How Does FunctionGraph Resolve Private DNS Domain Names?

How Does FunctionGraph Resolve Private DNS Domain Names?

FunctionGraph cannot directly parse private Domain Name Service (DNS) domain names. To parse them, call DNS APIs and perform the following steps.

Procedure

  1. Obtain the private domain name and ID.

    To obtain the private domain name and ID:
    1. Log in to the DNS console.
    2. Obtain the domain ID.
    3. Obtain the private domain name.

  2. Create a function.

    Create a function whose runtime is Python 2.7. The following is sample code.

    The italic content in bold indicates the DNS endpoint. Change it based on the site requirements. You can obtain the DNS endpoint from Regions and Endpoints. The following example uses the CN North-Beijing4 region.

    # -*- coding:utf-8 -*-
    import json
    import os
    import requests
    
    def handler(event, context):
        zone_id = context.getUserData("zone_id")
        domain =  context.getUserData("domain")
        token=context.getToken()
        ips = domainResolved(zone_id, domain,token)
        if ips != []:
            print(ips)
            return ips
    
    # GET /v2/zones/{zone_id}/recordsets   
    def domainResolved(zone_id, domain,token):
        url = "https://dns.cn-north-1./v2/zones/%s/recordsets" % zone_id
        headers = {'content-type': 'application/json',
                   "X-Auth-Token": token}
        resp = requests.get(url, headers=headers)
        if resp.status_code == 200:
            text = json.loads(resp.text)
            for record in text["recordsets"]:
                if record["name"] == domain:
                    ips = record["records"]
                    return ips
        else:
            print("[GET /v2/zones/{zone_id}/recordsets] failed, response: %s" % resp.text)
            return []

  3. Create a DNS agency for the function.

    On the IAM console, create an agency with the DNS ReadOnlyAccess system policy configured.

    You need to configure the permission to read DNS resource data because the function needs to obtain such data when parsing a domain name. Otherwise, the following error message is displayed, indicating that the DNS resource data failed to be obtained.

    2020/08/20 10:37:12 GMT+08:00  Start invoke request 'a2f105b4-2e72-4fda-94a5-86d3837e961d', version: latest
    [GET /v2/zones/{zone_id}/recordsets] failed, response: {"code":"DNS.1802","message":"Policy doesn't allow dns:recordset:list to be performed."}
    2020/08/20 10:37:13 GMT+08:00  Finish invoke request 'a2f105b4-2e72-4fda-94a5-86d3837e961d', duration: 1030.072ms, billing duration: 1100ms, memory used: 77.039MB.

  4. Configure a function.

    On the details page of the function created in 2, click the Configuration tab and perform the following operations:

    1. For Agency, select the agency created in 3.
    2. Enter the environment variables, that is, the domain name and domain ID obtained in 1.

  5. Save the configuration and execute the function.

    If the function is successfully executed, the corresponding IP address is returned. If an error occurs during DNS API invocation, no IP address is returned.