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 Function Compute console.
    If you have not activated Function Compute, read the terms, select I have read and agree, and then click Activate Now.
  3. Optional. In the left-side navigation pane, click Services and Functions. On the Services and Functions page, click Create Service. On the Create Service page, set the parameters as required and click Submit.
    The Service Name parameter is required. In this example, the Service Name parameter is set to EdgeFC. You can set other parameters based on your needs.
    Note If the EdgeFC service has been created for other scenarios or applications, you do not need to recreate the service.
  4. After you create the service, you must create a function in the service. On the Services and Functions page, click Create Function. On the Create Function page, click Configure and Deploy in the Event Function section.
  5. Configure the following primary parameters of the time synchronization function.
    ParameterDescription
    Service NameSelect EdgeFC.
    Function NameEnter syncNetworkTime.
    RuntimeThe runtime environment of the function. Select nodejs8.
    Function HandlerUse the default value index.handler.
    MemorySelect 512 MB.
    TimeoutEnter 10. Unit: seconds.
    ParameterDescription
    Service NameThe service where the function resides. Select EdgeFC.
    Function NameEnter syncNetworkTime.
    RuntimeThe runtime environment of the function. Select nodejs8.
    Function HandlerThe handler of the function. Use the default value index.handler.
    MemoryThe size of memory that is required to execute the function. Select 512MB.
    TimeoutThe timeout period of the function. Enter 10. Unit: seconds.
    Single Instance ConcurrencyThe number of concurrent requests that can be processed by an instance. Use the default value.

    You can set other parameters based on your needs or leave them unspecified. For more information, see What is 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, click 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.

    ParameterDescription
    Application NameThe name of the application. Example: syncNetworkTime.
    Application TypeSelect Function Compute.
    RegionSelect the region where the service is deployed.
    ServiceSelect EdgeFC.
    FunctionSelect syncNetworkTime.
    AuthorizationSelect AliyunIOTAccessingFCRole.
    Application VersionThe version of the application. You must specify a unique version number for a single application.

    The following table describes the function parameters.

    ParameterDescription
    Running ModeTwo 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 ExecutionTurn 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.
    Important 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 Use Function Compute to create edge applications in the Function Compute-based edge applications topic.

    You do not need to specify other parameters.

  3. In the left-side navigation pane, click Edge Instances.
  4. Find the created edge instance and click View in the Actions column.
  5. On the Instance Details page, click the Edge Applications tab. On the Edge Applications tab, click Assign 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 message that appears, click OK to assign resources such as sub-devices and Function Compute-based edge applications to the edge instance.
  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.