All Products
Search
Document Center

Apsara File Storage NAS:Mount a file system on a Function Compute function

Last Updated:Jun 25, 2024

Apsara File Storage NAS (NAS) is seamlessly integrated with Function Compute. You can mount a NAS file system on a Function Compute function so that the function can access files in the NAS file system in a way similar to accessing an on-premises file system. NAS allows you to implement persistent storage of files. This feature eliminates limits of on-premises disks and allows different function instances to share files. This topic describes how to mount a NAS file system on a Function Compute function.

Prerequisites

Limits

  • You can configure a maximum of five NAS mount targets for a Function Compute function in a region.

  • The local directories in the function runtime environment of the NAS mount target cannot conflict with each other.

Procedure

Step 1: Configure a NAS file system

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

  2. In the top navigation bar, select a region. On the Function page, click the function that you want to manage.

  3. On the function details page, click the Configuration tab. In the left-side navigation pane, click the Storage tab. In the NAS section, click Modify. In the NAS panel, configure the following parameters and click Deploy.

    Parameter

    Description

    Example

    Mount a NAS File System

    Specify whether to enable NAS mounting. Valid values:

    • Enable

    • Disable

    Enable

    NAS Configuration Mode

    Specify the configuration mode of the NAS file system. Valid values:

    • Automatic Configuration: The system automatically configures a NAS file system.

    • Custom Configuration: Manually configure a NAS file system by using parameters.

    Note
    • Before you select Automatic Configuration, you must allow functions to access resources in a VPC, and configure the VPC, vSwitch, and Security Group parameters. If you do not configure these parameters, the system automatically creates the preceding resources and allocates them to the service. For information about billing, see Billing.

    • If you select Automatic Configuration, the system creates a General-purpose NAS file system for you. If you select Automatic Configuration again after the NAS file system is created, the system queries and uses the existing General-purpose NAS file system that was created last time when you selected Automatic Configuration. For information about billing, see Billing of General-purpose NAS file systems.

    Custom Configuration

    Parameters of a NAS file system: If you set NAS Configuration Method to Custom Configuration, you must specify the configurations.

    Apsara File Storage NAS

    Select created NAS file systems.

    To create a new NAS file system, click Create NAS File System in the lower part of the parameter to go to the NAS console. Only Network File System (NFS)-based NAS file systems are supported. Server Message Block (SMB)-based NAS file systems are not supported.

    01d394****

    (Optional) User

    Specify the user that is used to access the NAS file. You must enter a custom user ID. If you do not specify a value, the system uses the root user ID. In this case, UID=0. For more information, see NAS users and user groups.

    0

    (Optional) User Group

    Specify the user group that is used to access the NAS file. You must enter a custom user group ID. If you do not specify a value, the system uses the root user group ID. In this case, GID=0. For more information, see NAS users and user groups.

    0

    NAS Mount Target: Specify mount targets and configure directories. For more information about how to configure a directory, see Configure a NAS mount target.

    Mount Target

    Specify a NAS mount target that matches the VPC and vSwitch configured for the function.

    Note
    • Before you configure NAS Mount Target, you must allow the function to access resources in the VPC and select a VPC and vSwitch that reside in the same region. For more information, see Configure network settings.

    01d394****-e****.cn-hangzhou.nas.aliyuncs.com

    Data Transmission Mode

    Select Regular Transmission or Encrypted Transmission. Only general-purpose NAS file systems support encrypted transmission. Encrypted transmission encrypts data transmission paths at a cost of certain resources. For more information, see Encryption in transit for NFS file systems.

    Regular Transmission

    Remote Directory

    • The remote directory of a general-purpose NAS file system must start with /.

    • The remote directory of an extreme NAS file system must start with /share.

    If the remote directory you configure in the NAS file system, Function Compute automatically creates the directory for you. The owner of the directory is the preceding user and user group that you configure, and the permission level is 777.

    /

    Local directory of a function

    Specify the local directory. We recommend that you use a subdirectory of /home, /mnt, /tmp, or /data.

    Note

    Do not use common Linux and Unix system directories and their subdirectories, such as /bin, /opt, /var, or /dev, to avoid mounting failures.

    /mnt/nas

Step 2: Access the NAS file

After you configure the NAS file system, you can perform the following steps to access NAS files:

  1. On the function details page, click the Code tab, write code in the code editor, and then click Deploy.

    The following code snippet shows the sample code of writing data to and read data from a NAS file in a Python runtime.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import random
    import subprocess
    import string
    import os
    
    
    def handler(event, context):
        # report file system disk space usage and check NAS mount target
        out, err=subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE).communicate()
        print('disk: ' + str(out))
        lines = [ l.decode() for l in out.splitlines() if str(l).find(':') != -1 ]
        nas_dirs = [ x.split()[-1] for x in lines ]
        print('uid : ' + str(os.geteuid()))
        print('gid : ' + str(os.getgid()))
    
        for nas_dir in nas_dirs:
            sub_dir = randomString(16)
            file_name = randomString(6)+'.txt'
            new_dir = nas_dir + '/' + sub_dir + '/'
            print('test file: ' + new_dir + file_name)
            # Write data to the NAS file system.
            content = "NAS here I come"
            os.mkdir(new_dir)
            fw = open(new_dir + file_name, "w+")
            fw.write(content)
            fw.close()
            # Showing the folder tree in NAS
            for home, dirs, files in os.walk(nas_dir):
                level = home.replace(nas_dir, '').count(os.sep)
                indent = ' ' * 2 * (level)
                print('{}{}/'.format(indent, os.path.basename(home)))
                subindent = ' ' * 2 * (level + 1)
                for f in files:
                    print('{}{}'.format(subindent, f))
            # Read data from the NAS file system.
            f = open(new_dir + file_name, "r")
            print(f.readline())
            f.close()
        
        return 'success'
    
    
    def randomString(n):
        return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))
      
  2. After the code is deployed, click Test Function on the Code tab.

    After the code is executed, you can view the execution result in the lower part of the Code tab. On the Logs tab, you can view logs that are generated during the function execution.

Step 3: Check whether the NAS file system is mounted

After you complete Step 2, you can connect to the instance to view files in the local directory of the function or check NAS usage on the NAS console to verify whether the NAS file system is successfully mounted.

  • Check whether the file in the local directory of the function is consistent with that in the code of the function.

    1. On the details page of the function, click the Instance tab to view instances in the Running state and click Connect to Instance in the Actions column of an instance. If no instance is running, you can run the instance again on the Code tab.

      image

    2. After you connect to the instance, you can run commands to view files in the local directory of the function. The following figure shows an example.

      image

  • Check whether the NAS file system usage is consistent with the file operation to verify whether the NAS file system is mounted. For more information, see View the capacity monitoring data of a NAS file system.

    image

Appendix

NAS users and user groups

Valid values of user IDs and user group IDs range from 0 to 65534. You must configure the owner and group permissions of files as required to ensure consistent read and write permissions on the files.

The user ID and user group ID are optional. If you do not specify these parameters, the default value 0, which indicate the root user ID and the root user group ID, is used.

If Function Compute uses a non-root user to execute your code and a remote NAS subdirectory is required, you must ensure that the function instance has the required read and write permissions on the subdirectory.

  • Mount a root directory of the remote NAS file system by using a NAS instance. For more information, see Procedure.

    After the root directory is mounted, create a subdirectory. Example: fc-1. Then, change the value of Remote Directory in the NAS mount target from the root directory to the created subdirectory (from / to /fc-1) to remotely mount the subdirectory.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os
    
    
    def handler(event, context):
        print('uid : ' + str(os.geteuid()))
        print('gid : ' + str(os.getgid()))
    
        # Change the value to the local directory in the on-premises file system to which the NAS file system is mounted.
        local_nas_dir = "/mnt/nas"
        # Change the value to the name of the destination subdirectory.
        target_sub_dir = "fc-1"
    
        # Create the destination subdirectory by using the permissions of the current instance in Function Compute.
        new_dir = local_nas_dir + '/' + target_sub_dir + '/'
        print('new_dir : ' + str(new_dir))
    
        os.mkdir(new_dir)
        return 'success'
      
  • If the subdirectory to be mounted does not exist in the remote NAS file system, Function Compute automatically creates the directory for you after you configure Remote Directory. The owner of the directory is the configured user and user group, and the permission is 777.

Note
  • If you want different functions to share NAS file resources, you must use the same user and group when you configure the NAS file system of the function.

  • The permissions on files that are uploaded to NAS are the same as those on on-premises files.

Directory configuration

Each NAS mount target consists of a remote directory and a local directory. A local directory in the on-premises file system is mapped to a remote directory in a NAS file system.

  • Remote Directory

    A directory in the remote NAS file system refers to the directory of the NAS file system that a service needs to access. The directory consists of a mount target and an absolute directory. You can add mount targets in the Apsara File Storage NAS console. You can assemble a mount target and the absolute directory into a remote directory. For example, if the mount target of a NAS file system is xxxx-nas.aliyuncs.com, and the absolute directory is /workspace/document, then the remote directory is xxxx-nas.aliyuncs.com:/workspace/document.

    You can log on to the NAS console. In the file system list, click the desired file system, and then click Mount Target to obtain the mount target.

  • Local directory of a function

    The local directory in a function runtime refers to the mount target of a local file system. Do not use a common Linux or Unix system directory or its subdirectories, such as /bin, /opt, /var, or /dev. Function Compute allows you to use a non-system directory, such as /mnt, /home, or /data to mount a NAS file system.

References

For more information about the scenarios of NAS storage and the differences between the storage types of Function Compute, see Selection of function storage.