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
A NAS file system is created. For more information, see Create a file system.
A function is created. For more information, see Create a web function.
Network settings are configured for the function. For more information, see Configure network settings.
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.
Configure a NAS file system
Log on to the Function Compute console. In the left-side navigation pane, click Functions.
In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.
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.
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
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.
ImportantOnly 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
andGID=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.
NoteDo 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
NoteIf 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.
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
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.
(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.
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.
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.
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.
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.
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.