This topic describes how to configure custom DNS resolution using Function Compute.
Prerequisites
You must add the domain name for which you want to configure custom DNS resolution. For more information, see Add a domain name.
1. Grant permissions to a service-linked role
When HTTPDNS calls Function Compute for custom DNS resolution, you must grant permissions to a service-linked role in the HTTPDNS console. This authorization allows HTTPDNS to access Function Compute.
Procedure
Log on to the EMAS console.
In the navigation pane on the left, choose DNS Management > Custom DNS.
Click Add Custom DNS. If you set Policy Type to Function Compute Policy and do not have the required permissions, an authorization message appears. Click Authorize.
On the service-linked role authorization page that appears, click OK to complete the authorization. You cannot use the Function Compute policy feature without these permissions.
You must grant permissions the first time you use a Function Compute policy. The authorization is triggered in two scenarios:
When you create a custom DNS record and select Function Compute Policy.
When you navigate to the policy management list, a prompt to grant permissions appears.
2. Create a custom DNS resolution function
First, you must activate Alibaba Cloud Function Compute and develop a custom DNS resolution function. After you develop the function, you must publish the service and function, and then generate a formal version or an alias.
To ensure the security and stability of the HTTPDNS Function Compute policy, do not use the default Latest version when you configure the policy. You must specify a published service and function version.
HTTPDNS supports both FC 2.0 and FC 3.0, but FC 3.0 is recommended because FC 2.0 is deprecated. For more information about the differences between the two versions, see Differences between Version 2.0 and Version 3.0.
If you still need to create a 2.0 function, see Create a 2.0 function.
Create a 3.0 function
Log on to the Function Compute console. In the top menu bar, select a Region.
In the navigation pane on the left, choose Functions. On the function list page, click Create Function.

On the Create Function page, select Event Function and configure the parameters as needed. You can select the 'HTTPDNS Custom DNS Resolution' template to use a code example. After you configure the parameters, click Create to create the function.

After the editor is initialized, enter the function code. For more information about how to configure the function code, see 3. Write the custom DNS resolution function code. You can skip this step if you selected the 'HTTPDNS Custom Parsing' template.
Next, test your function. For information about the input parameter settings, see 3. Write the custom DNS resolution function code. Click Test Function and confirm that the function executes correctly and that the returned data is as expected.

After the test is complete, click Deploy Code and confirm that the code is deployed.
Click Version Management, enter a description, and create a version.

3. Write a custom parsing function
Write the function code
In the function editor, enter your custom DNS resolution logic. The function must process context parameters passed by HTTPDNS, such as client IP, region, and carrier, and return the processed DNS resolution result. For example:
This example uses
nodejs6ornodejs8as the programming language.'use strict'; exports.handler = (event, context, callback) => { // Format the input parameters into an object. const eventObj = JSON.parse(event.toString()); const { location, // Region ips, // Authoritative DNS result ttl, // Original TTL } = eventObj; if (location.province === 'zhejiang' && location.isp === 'chinanet') { // When a user from Zhejiang with China Telecom accesses the domain name, return the specified IP address. callback(null, { ips: ips.concat(['1.1.X.X']), // Add the specified IP address to the returned IP array. ttl: event.ttl * 2, // Modify the TTL. extra: "", // Carry additional parameters for the client. }); } else { // Default return. callback(null, { ips, ttl, extra: "", }); } };Configure test input parameters
Before you click Test Function, configure the test event with sample input parameters to verify that the function behavior is as expected. The test event must simulate the actual HTTPDNS call format. For example:
{ "domainName": "www.aliyun.com", // The domain name being resolved "clientIp": "192.168.1.4", // Client IP "location": { "continent": "asia", // Continent where the client IP is located "region": "cn", // Country or region where the client IP is located "isp": "bgp", // Carrier line for the client IP "province": "zhejiang" // Province where the client IP is located }, "ips": ["192.168.1.3"], // List of DNS resolution results. This result is not available in the BEFORE_READ_CACHE stage. "ttl": 60, // TTL of the DNS resolution result. This result is not available in the BEFORE_READ_CACHE stage. "hookType": "BEFORE_WRITE_CACHE", // Stage in which the function runs "parameters":{ // Parameter object carried in the resolution request "param1":"p1", // Corresponds to the "sdns-param1=p1" parameter in the URL "param2":"p2" } }Run the test and verify the result
Click Test Function. Check whether the returned result contains the expected ips, ttl, or extra fields and is in the correct format. The following code provides a sample response:
{ "ips": event.ips.concat(['192.168.1.2']), "ttl": event.ttl * 2, "extra": "some-thing-send-to-user" // ,"domainName": "www.alibabacloud.com" // Valid only in the BEFORE_READ_CACHE stage // ,"cacheKey": "cache-key-001" // Valid only in the BEFORE_READ_CACHE and BEFORE_WRITE_CACHE stages }NoteFor more information about how custom DNS resolution functions work and for complete descriptions of input and response parameter fields, see Write custom parsing function code.
4. Attach the Function Compute function to the domain name
You can configure Function Compute services in multiple regions for each domain name that requires custom DNS resolution. You can also use the same Function Compute service for policies of different domain names.
Procedure
Log on to the EMAS console.
In the navigation pane on the left, choose DNS Management > Custom DNS.
Click Add Custom DNS and set Policy Type to Function Compute Policy.

Configure the following parameters. After you complete the configuration, click OK to return to the policy list.
Parameter
Description
Region
Required. Select the region where the Function Compute service is located. For example, if you created a Function Compute service in the Shanghai region, select Shanghai.
FC Version
Both FC 2.0 and FC 3.0 functions are supported. The default version is FC 3.0, and FC 3.0 is recommended.
Differences and compatibility between Function Compute 3.0 and 2.0
Service
Required. Select the Function Compute service in the specified region. This field is available only for FC 2.0.
Service Version/Alias
Required. Select the version or alias of the service. This field is available only for FC 2.0.
NoteDo not use the LATEST version of Function Compute.
Custom DNS resolution function for the BEFORE_READ_CACHE stage
Configures a Function Compute function for the hook stage before the HTTPDNS server reads the cache. This function is optional.
Custom DNS resolution function for the BEFORE_WRITE_CACHE stage
Configures a Function Compute function for the hook stage that occurs after the HTTPDNS server performs recursive resolution and before it writes to the cache. This function is optional.
Custom parsing function for the BEFORE_WRITE_RESPONSE phase
Configures a Function Compute function for the hook stage that occurs after the HTTPDNS server writes to the cache and before it constructs the response. This function is optional.
Unit testing for the hook function
Tests the connectivity between the HTTPDNS service and the Function Compute service:
Test Succeeded: The HTTPDNS service can connect to the configured Function Compute service.
Test Failed: The HTTPDNS service cannot connect to the configured Function Compute service.
What to do next
After you configure custom DNS resolution based on Function Compute, see Overall workflow to complete the remaining steps.