Apsara File Storage NAS (NAS) is a distributed file system that provides secure, reliable, high-performance, and easy-to-use file storage services for services such as Elastic Compute Service (ECS), Elastic High Performance Computing (E-HPC), Docker, and Batch Compute.
Background information
Function Compute can seamlessly integrate with NAS. You can configure the NAS settings for a service in Function Compute. The settings includes the information such as the region, mount target, and group. After you configure a NAS file system for a service, all functions in the service can access the files in the NAS file system in the same manner in which you access the on-premises file system.
- Temporary files can be stored in a NAS file system. The size of temporary files is not limited by the capacity of on-premises disks.
- Multiple functions can share files by using one NAS file system.
Prerequisites
- Function Compute
- Configure the network
A NAS file system can be only mounted in a virtual private cloud (VPC). You must configure a valid VPC that allows you to access the specified NAS file system.
- Configure the network
- NAS
Configure a NAS file system
NAS settings in Function Compute are configured at the service level. After a NAS mount target is configured for a service, all functions in the service can access the files in the specified NAS file system.
NAS users and user groups
When you configure a NAS mount target for a function, you must first specify the user ID and group ID, which are equivalent to the user and user group in NAS. You must specify the file owner and configure the corresponding group permissions based on your business requirements to ensure the consistency of read and write permissions on files.
Valid values of the user ID and group ID range from 0 to 65534. If you do not specify
the user ID, the system uses the ID of the root user (0
). If you do not specify the group ID, the system uses the ID of the root user group
(0
).
- Use a NAS instance to mount a root directory of the remote NAS file system. For more
information, see Configure a NAS file system. After the root directory is mounted, create a subdirectory, for example, fc-1, by using the code provided in the following example. Then, change the value of The directory in the remote NAS file system in the NAS mount target from the root directory / to the created subdirectory /fc-1 to mount the subdirectory remotely.
#!/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 = "/home/app" # Change the value to the name of the destination subdirectory. target_sub_dir = "fc-1" # Create a subdirectory in the directory to which the NAS file system is mounted. new_dir = local_nas_dir + '/' + target_sub_dir + '/' print('new_dir : ' + str(new_dir)) os.mkdir(new_dir) return 'success'
- Mount a NAS file system on an ECS instance. For more information, see Mount a file system on an ECS instance. After the NAS file system is mounted on the ECS instance, create a subdirectory
and run
chmod 777
to grant permissions on the subdirectory.
- The default user and user group do not have the read and write permissions on files. Therefore, we recommend that you set the user ID and group ID to specific values from 1 to 65534. After that, different functions in the service can share these file resources.
- The permissions on files that are uploaded to NAS are the same as those on on-premises files.
Configure a NAS mount target
For each mount target, you must configure The directory in the remote NAS file system and The local directory in the function runtime environment. A local directory in the on-premises file system is mapped to a remote directory in a NAS file system.
- The directory in the remote NAS file system
A directory in the remote NAS file system specifies the directory of the NAS file system that the service needs to access. The 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 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, the remote directory is xxxx-nas.aliyuncs.com:/workspace/document.
You can log on to the NAS console, click the destination file system in the file system list, and then click Mount Targets to obtain the mount target.
- The local directory in the function runtime environment
The local directory in the function runtime environment is a mount target in an on-premises file system. Do not use common directories in Linux or UNIX, such as bin, opt, var, and dev, to mount a NAS file system. Function Compute allows you to use non-system directories such as mnt and home to mount a NAS file system.
References
Aside from the Function Compute console, you can also use Serverless Devs to configure a NAS file system. For more information, see Run the s nas command to mount an Apsara File Storage NAS file system.