All Products
Search
Document Center

NAS Access

Last Updated: Oct 17, 2018

Alibaba Cloud Network Attached Storage (NAS) is a highly reliable and available distributed file system which supports standard file access protocols. This allows existing applications to be ported to the Alibaba cloud without any modification.

Alibaba Cloud Function Compute now seamlessly integrates with NAS. This allows you to write functions that access the files stored on one of your NAS file systems the same way as they access local files. All you have to do is to provide NAS specific configurations which includes the NAS mount points info on the service. Once the service gets a valid NAS configuration, all its functions can access the specific NAS file system like its local file system.

Configure NAS

The nas related setting is a service level configuration which means all the functions under the service share the same NAS configuration. Please make sure that the NAS feature is enabled before trying to use it in function compute.

VPC and NAS

One important prerequisite of adding NAS to a service is that the service has to have VPC settings. It is because NAS can only safely expose mount points inside a user created VPC network, thus the user has to make sure that the serivce is configurated with the correct VPC settings to access the specified NAS file system.

nasConfig

Here is the definition of nasConfig:

  1. "nasMountConfig": {
  2. "ServerAddr" : string,
  3. "MountDir" : string
  4. }
  5. "nasConfig" : {
  6. "UserId" : int,
  7. "GroupId" : int,
  8. "MountPoints" : [nasMountConfig]
  9. }
UserId and GroupId

The first part in the nasConfig is the userId and groupId value your functions uses to access the NAS file system. All functions in this service run under a user with the user id equal touserId and group id equal to groupId to access the file system. Therefore, please make sure to set the correct NAS Permission Groups on the NAS file system so that the function can read or write them.

The range of valid userId and groupId values are both from -1 to 65534 with 0 excluded as we do not allow users to run as root. The userId and groupIdare both optional on service creation. The default user id is a system defined value (which can change from one invoke from another) and the default group id is always the same as the user id if not specified. Please only use the default user id if your function does not rely on the a specific ID. The best practice is to use a specific group id to share the files between different functions while leaving the user id as system default.

Users could use -1 to reset the userId or the groupId value to default when updating the service.

Mount Points

The second part in the nasConfig is the NAS mount point configuration. One can provide up to five NAS mount points in each service. Each nasMountConfig consists of a pair of NAS ServerAddr and MountDir.

ServerAddr

The ServerAddr field describes a remote directory in a NAS file system that the service need access. It has two parts. The first part is a NAS mount point. One can create a mountpoint in the NAS system by navigating to the NAS console and find the file system that the service wants to access (as the following figure illustrates)add mountThe second part is an absolute directory on that file system. One then can copy the value in the Mount Address and concatenate it with the directory path to serve as the value of ServerAddr. For example, if the NAS filesystem’s mount address is “xxxx-nas.aliyuncs.com” while you want to restrict the function access to be under the /workspace/document directory then the ServerAddr would be “xxxx-nas.aliyuncs.com:/workspace/document”.

MountDir

The MountDir points to the mount point in the local file system. One is advised not to mount the NAS directory under any common Linux system direcotry such as “/bin”, “/opt”, “/var”, “/dev” etc. We allow users to mount their NAS filesystem under the “/mnt”, “/home” and other none system directories.

Combined with ServerAddr, it forms a mapping from a certain directory in the NAS file system to a directory in the local file system. Following the previous example, if one sets MountDir as /mnt/fc in the nasMountConfig then all functions under this service can use the local diretory /mnt/fc as the remote /workspace/document directory in the NAS file system.

Use NAS on the console

User can set the nasConfig on the console during service creation or updating. The Nas Config panel is enabled only if the service has VPC configuration as one can only access NAS through VPC. On creation, the default user/group id is -1 if left not filled.nas console

The console has a drop down menu that contains all the possible mount points in the NAS that is accessible from inside the service VPC network. User can pick one mount point from the list and then enter the absolute path of the remote root path to form the ServerAddr. The console combines this with the value of the local mount directory to form one nasMountConfig entry.

Use SDK to create NAS Configuration

User can programmatically create and update NAS Config too. Here is a code snippet written in Python2.7 that creates a service with NAS.

  1. vpcConfig = {
  2. 'vpcId': 'my-vpcId',
  3. 'vSwitchIds': [id],
  4. 'securityGroupId': 'my-securityGroupId'
  5. }
  6. nasConfig = {
  7. "userId": my-uid,
  8. "groupId": my-gid,
  9. "mountPoints": [
  10. {
  11. "serverAddr": 'xxx-nas.com:/fc',
  12. "mountDir": '/mount/dir'
  13. }
  14. ]
  15. }
  16. service = client.create_service(name, role=self.vpcRole, vpcConfig=vpcConfig, nasConfig=nasConfig)

Use NAS in a function

The NAS system is ready to use after configuration. One doesn’t need to use any other client or libraries in any runtime. It is exactly like using a local file system. Here is a python2.7 code sample of creating/writing/reading from a NAS file system and one can debug the same code on the local machine and then upload to Function Compute without any change.

  1. import json
  2. import logging
  3. import random
  4. import string
  5. import os
  6. def handler(event, context):
  7. logger = logging.getLogger()
  8. evt = json.loads(event)
  9. root_dir = evt["root_dir"]
  10. sub_dir = randomString(16)
  11. logger.info('uid : ' + str(os.geteuid()))
  12. logger.info('gid : ' + str(os.getgid()))
  13. file_name = randomString(6)+'.txt'
  14. newDir = root_dir + '/' + sub_dir + '/'
  15. content = "NAS here I come"
  16. os.mkdir(newDir)
  17. fw = open(newDir+file_name, "w+")
  18. fw.write(content)
  19. fw.close()
  20. fr = open(newDir+file_name)
  21. line = fr.readline()
  22. if line != content:
  23. return False
  24. fr.close()
  25. os.remove(newDir+file_name)
  26. os.rmdir(newDir)
  27. return True
  28. def randomString(n):
  29. return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))