Configure custom DNS

更新时间:
复制 MD 格式

You can use Custom DNS for scenarios such as accelerating site access.

Configuring custom DNS overwrites the /etc/resolv.conf file on the function instance. To learn about the resolv.conf format, run man 5 resolver on a Linux system, or see the Linux manual page.

Prerequisites

Before you begin, ensure that you have:

  • A function created in Function Compute (Custom Image functions are not supported; GPU-accelerated functions are not supported)

Configure DNS settings

  1. Log on to the Function Compute console. In the left-side navigation pane, choose Function Management > Function.

  2. In the top navigation bar, select a region. On the Function page, click the target function.

  3. On the function details page, click the Configuration tab. To the right of Advanced Settings, click Modify.

  4. In the Advanced Settings panel, locate the DNS section and configure the following parameters:

    ParameterDescriptionExample
    Name serversIP addresses of the DNS servers. Add multiple addresses as needed. To use the default Function Compute DNS servers, add 100.100.2.136 and 100.100.2.138.223.5.5.5, 223.6.6.6
    Search domainDNS search domains. Add multiple domains as needed. When a domain name fails to resolve, the system appends each search domain in order and retries the query.example.com
    DNS optionsAdditional resolver options in key:value format. The key is required.attempts:1
  5. Click Deploy.

Verify the configuration

After deploying, confirm that your custom DNS settings are active by running a test function.

  1. On the function details page, click the Code tab.

  2. In the code editor, paste the following sample code:

    import logging
    import subprocess
    
    def handler(event, context):
      logger = logging.getLogger()
      f = open('/etc/resolv.conf')
      logger.info("".join(f.readlines()))
      f.close()
      output = subprocess.check_output(["ping", "-c", "1", "www.aliyun.com"])
      return output
  3. Click Deploy, then click Test Function.

  4. Review the results:

    • Response: view the content generated for resolv.conf.

    • Log Output: view the IP address of the returned domain name.