This topic describes how to use a Function Compute-based edge application to deploy a network time synchronization assistant to synchronize the local time of devices with the network time. If you have installed the Network Time Protocol (NTP) client on your devices, NTP can synchronize the local time with the cloud server time on a regular basis. In this case, you can skip this topic.

Prerequisites

An edge instance is created. Only Link IoT Edge Pro Edition is supported. For more information about how to create an edge instance, see Build environments for Link IoT Edge Pro Edition.

Background information

For various edge devices that run the Linux operating system, the NTP client is not installed by default. After the system is started, the local time starts from 1970. In another scenario, the local time of a Linux virtual machine is synchronized only once after the system is started. If the system hibernation occurs, the virtual machine suspends timing. The interval between the local time and the network time increases in the long run.

If the local time and the network time are not the same, you may fail to access Alibaba Cloud services and install software online. Valid timestamps can provide valuable reference information for log analysis.

You can create a Function Compute-based edge application in Link IoT Edge to synchronize the local time of devices with the network time.

1. Create a time synchronization function

  1. Download the following package of a time synchronization function that is used to synchronize the local time with the NTP server time:
  2. Log on to the Alibaba Cloud Function Compute console.
    If you have not activated the Function Compute service, read the terms, select I have read and agree, and click Activate Now.
  3. Optional. In the left-side navigation pane, select Service-Function. From the drop-down list of Create Function, select Create Service. On the Create Service page, configure parameters and click Create.
    The Service Name parameter is required. In this example, you must specify EdgeFC for the Service Name parameter. You can specify other parameters based on your needs.
    Note If the EdgeFC service has been created for other scenarios or applications, you do not need to create a new one.
  4. After creating the service, you must create a function. On the Service-Function page, click Create Function. On the Create Function page, select Event Function and click Next.
  5. Configure the following primary parameters of the time synchronization function.
    Parameter Description
    Service Name Select EdgeFC.
    Function Name Enter syncNetworkTime.
    Runtime The runtime environment of the function. Select nodejs8.
    Function Handler Use the default value index.handler.
    Memory Select 512 MB.
    Timeout Enter 10. Unit: seconds.
    Parameter Description
    Service Name Select EdgeFC.
    Function Name Enter syncNetworkTime.
    Runtime The runtime environment of the function. Select nodejs8.
    Function Handler Use the default value index.handler.
    Memory Select 512 MB.
    Timeout Enter 10. Unit: seconds.
    Single Instance Concurrency Use the default value.

    You can configure other parameters based on your needs or leave them empty. For more information about how to configure parameters, see Function Compute.

    Confirm the function parameters and click Create.

  6. After the function is created, the function details page appears. On the Code tab, select Upload Zip File and click Select File to upload the syncNetworkTime-code.zip package that you downloaded in step 1. Then, click Save.
    After the package is uploaded, you can view the source code on the In-line Edit tab.The In-line Edit tab

2. Assign the function to the edge instance

  1. In the left-side navigation pane of the IoT Platform console, choose Link IoT Edge > Applications.
  2. Create a Function Compute-based edge application from the function that is created in step 1: Create a time synchronization function. For more information about how to create a Function Compute-based edge application, see Use Function Compute to create edge applications.

    The following table describes the application parameters.

    Parameter Description
    Application Name The name of the application. Example: syncNetworkTime.
    Application Type Select Function Compute.
    Region Select the region where the service is deployed.
    Service Select EdgeFC.
    Function Select syncNetworkTime.
    Authorization Select AliyunIOTAccessingFCRole.
    Application Version The version of the application. You must specify a unique version number for a single application.

    The following table describes the function parameters.

    Parameter Description
    Running Mode Two running modes are available. In this example, select Continuous. The application runs immediately after being deployed.
    Memory Limit (MB) The maximum memory that is available for running the function. Unit: MB. Enter 512. If the memory that is used by the function exceeds the limit, the Function Compute application is forced to restart.
    Timeout Limit (Seconds) The maximum processing period after the function receives an event. Use the default value 5. If the function does not return the result within the specified period, the Function Compute application is forced to restart.
    Scheduled Execution Turn on the Scheduled Execution switch and enter * * * * * in the text box. The CRON expression indicates that the function is scheduled to run every minute. For more information about CRON expressions, visit the CRONTAB website.
    Notice If you have built an environment for Link IoT Edge Pro Edition, the time synchronization assistant needs to modify the local time of the host. Therefore, in the Container Configurations (Optional) section, you must select Yes for the Privilege Mode parameter. For more information about the parameters, see Table 3 in the Function Compute-based edge applications topic.

    You do not need to specify other parameters.

  3. In the left-side navigation pane, choose Link IoT Edge > Edge Instances.
  4. Find the created edge instance and click View.
  5. On the Instance Details page, click the Edge Applications tab. On this tab, click Allocate Application.
  6. Assign the syncNetworkTime function to the edge instance, and click Close.

3. Deploy the edge instance

  1. On the Instance Details page, click Deploy in the upper right corner. In the dialog box that appears, click OK to assign resources such as sub-devices and Function Compute-based edge applications to the edge instance.
    You can click Deployment Details to view the deployment progress and result.
  2. After the instance is deployed, run the date command in the gateway to view the local time. The local time of the gateway is updated to match the network time.

You have completed the procedure of using a Function Compute-based edge application to synchronize the local time of a device with the network time.