All Products
Search
Document Center

Function Compute:Configure a NAS file system

Last Updated:Sep 06, 2024

When use Function Compute, you may need to access the same set of data between different applications and functions. For example, in a machine learning application, a trained model needs to be shared by multiple inference functions. You can configure an Apsara File Storage NAS (NAS) file system to store and share the data. This helps simplify data management and address limitations of on-premises disk space. After you configure a NAS file system for a function, the function can read data from and write data to the NAS file system in the same way as an on-premises file system.

Before you start

  • Function Compute

    Create a function and configure VPC access for the function. For more information, see Create a function and Configure network settings.

    Note

    You can configure NAS file systems only in private virtual private clouds (VPCs). Therefore, you must allow your function to access a VPC and correctly configure the VPC in the function network settings if you want to configure NAS mounting.

  • NAS

    Create a NAS file system and add a mount target for the file system. For more information, see Create a file system and Create a mount target.

Limits

  • You can configure a maximum of five NAS mount targets and five Object Storage Service (OSS) mount targets for a function in Function Compute in a region.

  • For the same function, the local directories in the function runtime of NAS mount targets cannot be the same as those of OSS mount targets.

Configure a NAS file system

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

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

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

    You can select Automatic Configuration or Custom Configuration as the configuration mode to configure a NAS file system.

    Automatic Configuration

    The system automatically creates a general-purpose NAS file system named Alibaba-Fc-V3-Component-Generated, and a VPC, a vSwitch, and a security group with the same name.

    The next time you select Automatic Configuration, the system detects that the preceding NAS file system already exists in the region. In this case, the system reuses the file system and related VPC configurations and does not create the file system again. For more information about billing of VPC and NAS, see Billing and Billing of General-purpose NAS file systems.

    image

    Custom Configuration

    If you select this option, you need to manually select a NAS file system, and configure related settings, such as the user, user group, and NAS mount target.

    The following table describes key parameters:

    Parameter

    Description

    Example

    Apsara File Storage NAS

    Select an existing NAS file system.

    To create a new NAS file system, click Create NAS File System below the parameter to go to the NAS console.

    Important

    Only Network File System (NFS)-based NAS file systems are supported. Server Message Block (SMB)-based NAS file systems are not supported.

    01d394****

    User and User Group

    Enter a custom user ID and user group ID. If you do not specify a value, the system root ID is used. Specifically, UID=0 and GID=0. For more information, see NAS users and user groups.

    1

    Remote Directory

    • Specify the 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 specify does not exist in the remote 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.

    For more information, see Remote directory.

    /

    Local Function 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. Otherwise, the mounting may fail.

    For more information, see Local directory.

    /mnt/nas

    image

    Note
    • If you want different functions to share NAS file resources, you must use the same user and user group when you configure NAS mounting for these functions.

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

Verify whether the NAS file system is mounted

Prepare function code for accessing NAS

After you configure the NAS file system, you can access the NAS file system by using the specified path in your function code.

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

    The following code snippet provides an example of writing data to and read data from a NAS file system by using Python event function code.

    #!/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))
      

Verify the mounting

  1. 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. The output logs indicate that the function has successfully written data to and read data from the NAS file system.

    image

  2. (Optional) After you write and read data, you can log on to the instance to view files in the local directory of the function and check whether the queried NAS file is consistent with the log output of the function.

    1. On the function details page, click the Instances tab and click Connect in the Actions column.

      If no instance is running, you can click Test Function on the Code tab to run the function again and activate an instance.

      image

    2. After you log on to the instance, you can run the following command to view files in the local directory of the function: The following figure shows an example.

      image

Concepts

NAS users and user groups

Valid values of the user ID and user group ID are [0, 65534]. If you do not specify a value for the user ID or user group ID. The values are default to 0, which indicates the root user ID and the root user group ID, respectively. You need to specify the owner and corresponding group permissions on files to ensure that the file read and write permissions are consistent. For example, if you want different functions to share NAS file resources, you need to use the same user and user group when you configure NAS mounting for these functions.

Remote directory and local directory

The endpoint of each NAS mount target consists of a remote directory and a local directory. The process of mounting a NAS file system essentially creates a mapping from the local directory of a Function Compute instance to the remote directory of the NAS file system.

  • Remote directory

    A remote directory is a directory in the remote NAS file system. A remote directory consists of a mount target and an absolute directory. You can add mount targets in the NAS console. You can assemble a mount target and an 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 you want to access is /workspace/document, the corresponding complete 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.

    image

  • Local directory

    The local directory in a function runtime refers to the mount target of a local file system. We recommend that you use a subdirectory of /home, /mnt, /tmp, or /data. You cannot use a common Linux or UNIX system directory or its subdirectories, such as /bin, /opt, /var, or /dev, to mount a NAS file system.

More information

  • Function Compute supports the following storage types: NAS, Object Storage Service (OSS), temporary hard disks, and layers. For more information about the scenarios and differences between these storage types, see Selection of function storage.

  • If you need to store a large amount of unstructured data such as images, videos, and documents, we recommend that you mount an OSS file system. For more information, see Configure an OSS file system.

  • You can also use Serverless Devs to mount a NAS system to a function. For more information, see Common commands of Serverless Devs.