In Function Compute, multiple applications or functions may need to share access to the same dataset. For example, in a machine learning application, a trained model needs to be shared among several inference functions. You can configure a NAS file system for your function to store this data. This enables file sharing, simplifies data management, and overcomes local disk space limitations. After you configure a NAS file system for a function, your Function Compute function can read from and write to NAS files as if they were on a local file system.
Prerequisites
-
Function Compute
A function has been created and configured to access a VPC. For more information, see Create a function and Configure network settings.
NoteYou can add NAS mount targets only within a VPC. Therefore, you must configure the function's network settings to access the specific VPC where the NAS file system resides.
-
File Storage NAS
A NAS file system has been created and a mount target has been added. For more information, see Create a file system and Add a mount target.
Limits
-
In Function Compute, a single function supports up to five NAS mount targets and five Object Storage Service (OSS) mount targets in the same region.
-
The local directories for NAS and OSS mount targets must not conflict.
Configure a NAS file system
-
Log on to the Function Compute console. In the left-side navigation pane, choose .
-
In the top navigation bar, select a region. On the Functions page, click the target function.
-
On the function details page, select the Configuration tab and click Modify next to Advanced Settings. In the Advanced Settings panel, go to the Storage section, enable Mount NAS file system, configure the required settings, and then click Deploy.
You can configure the NAS file system using either Automatic Configuration or Custom Configuration.
Auto config
The system automatically creates a general-purpose NAS file system named
Alibaba-Fc-V3-Component-Generated, along with a VPC, vSwitch, and security group with the same name.When you use Automatic Configuration again in the same region, the system reuses the existing NAS file system and VPC configurations instead of creating new ones. For more information about pricing, see VPC billing and Billing of general-purpose NAS.
Custom config
You must manually select a NAS file system and configure its parameters, such as user, user group, and mount target.
The following table describes key parameters.
Parameter
Description
Example
NAS
Select an existing NAS file system.
To create a NAS file system, click Create NAS File System and create one in the File Storage console.
ImportantOnly NAS file systems that use the NFS protocol are supported. NAS file systems that use the SMB protocol are not supported.
01d394****
User, User Group
Specify a custom user ID and user group ID. If unspecified, the configuration defaults to the root user:
UID=0andGID=0. For more information, see NAS users and user groups.1
Remote Directory
-
For a general-purpose NAS file system, this directory must start with /.
-
For an extreme NAS file system, this directory must start with /share.
If the specified directory does not exist on the remote NAS, Function Compute automatically creates it. The new directory will be owned by the user and user group you specified and will have
777permissions.For more information, see Remote directory.
/
Local directory of a function
We recommend using a subdirectory of /home, /mnt, /tmp, or /data.
NoteDo not use common Linux and Unix system directories or their subdirectories, such as /bin, /opt, /var, or /dev. Otherwise, the mount may fail.
For more information, see Local function directory.
/mnt/nas
Note-
To share NAS resources across multiple functions, configure each function with the same user and user group.
-
Files written to NAS will have the same permissions as files created locally within the function.
-
Verify the mount
Prepare the function code to access NAS
After configuring the NAS file system, you can access it in your code using the local directory path you specified for the mount.
-
On the function details page, click the Code tab. Write your code in the code editor, and then click Deploy.
This topic uses a Python event-driven function as an example. The code writes content to and reads content from the NAS file system.
#!/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 to a NAS file. content = "NAS here I come" os.mkdir(new_dir) fw = open(new_dir + file_name, "w+") fw.write(content) fw.close() # Display the directory tree on the NAS mount 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 from a NAS file. 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 result
-
After the code is deployed, click Test Function on the Code tab.
After the execution is complete, you can view the results at the bottom of the Code tab. On the Log Output tab, you can see that the function successfully wrote to and read from the NAS file.
C9JFL0.txt DCFIC887M7R74WYH/ 5CXI5H.txt H0630SH2QHPL9MHZ/ EUV7ZA.txt H4UNM032L2TDLPPQ/ YXDBPM.txt HYMF79FH11AYW66D/ LU3WNG.txt Z3QNEH0MY0K3VM6G/ 9D3FM2.txt fc-1/ modelscope-60c13a-model-download-func/ NAS here I come FC Invoke End RequestId: 1-66878b18-16cd285c-27f5980efe23 -
(Optional) After the function runs, you can log on to the instance to view the files in the local function directory and confirm that they match the log output.
-
On the function details page, click the Instance tab. In the Actions column of the target instance, click Connect to Instance.
If no instances are running, click Test Function on the Code tab to run the function again and create an instance.
-
After you log on to the instance, you can run commands to view the files in the configured local function directory.
root@c-668b4cff-xxx:/code# cd /mnt/nas root@c-668b4cff-xxx:/mnt/nas# ls 0Q0V5ZYWMW7SLUYV BVT5FA0OV9ZLIZMU DCFIC887M7R74WYH F8I2CGH79QQGPZKC HO63OSH2QHPL9MHZ H4UNM032L2TDLPPQ HYMF79FH11AYW66D KEDRGMD67X64A6FD Z3QNEH0MY0K3VM6G fc-1 modelscope-60c13a-model-download-func root@c-668b4cff-xxx:/mnt/nas# cd ../ root@c-668b4cff-xxx:/mnt# cd nas/Z3QNEH0MY0K3VM6G root@c-668b4cff-xxx:/mnt/nas/Z3QNEH0MY0K3VM6G# ls 9D3FM2.txt root@c-668b4cff-xxx:/mnt/nas/Z3QNEH0MY0K3VM6G#
-
-
(Optional) You can also use a NAS browser application to manage files in the mounted NAS file system directly from your browser. For more information, see Use Function Compute to quickly build a visual NAS browser application.
In the China (Hangzhou) and China (Shanghai) regions, you do not need to build a NAS browser. Instead, go to the NAS console, find the target file system, and in the Actions column, choose to manage your files visually.
After you access the NAS browser, the folders from your mounted NAS file system appear in the root directory on the File Management page. This confirms that the mount is configured correctly.
Related concepts
NAS users and user groups
The UserID and GroupID values can range from 0 to 65534. If you do not specify these values, they both default to 0, which corresponds to the root user ID and root group ID, respectively. You must set the file owner and corresponding group permissions as needed to ensure consistent read and write access. For example, if you want different functions to share NAS file resources, you must use the same user and user group when you configure the NAS file system for these functions.
Remote directory and local function directory
Mounting a NAS file system maps a Local directory of a function in the function's environment to a Remote Directory in the NAS file system.
-
Remote Directory
The remote directory is a directory path on the NAS file system, such as /workspace/document. This path is combined with a mount target, for example xxxx-nas.aliyuncs.com, to form the full address for mounting. The complete mount source would look like xxxx-nas.aliyuncs.com:/workspace/document.
To find the mount target, log on to the NAS console, click your file system, and then click Mount Usage.
-
Local directory of a function
The local directory in the function's runtime environment serves as the mount point on the local file system. We recommend using a subdirectory of /home, /mnt, /tmp, or /data. Do not use common Linux and Unix system directories or their subdirectories to mount a NAS file system. Examples include /bin, /opt, /var, and /dev.
Related documents
-
Function Compute supports several storage types, including File Storage NAS, Object Storage Service (OSS), temporary disk, and layers. To understand the use cases and differences between these storage types, see Select a storage type for a function.
-
For storing large amounts of unstructured data like images, videos, and documents, we recommend using Object Storage Service (OSS). For more information, see Configure an OSS mount target.
-
You can also use Serverless Devs to mount a NAS file system to a function. For more information, see Common Serverless Devs commands.
-
To visually manage files on the NAS file system, you can create a NAS browser from the Function Compute Application Center. For more information, see Quick Start (FC).