This topic describes how to configure an Apsara File Storage NAS file system and its dependent resources. This topic also shows you how to run the fun nas command to manage NAS files after the configuration.

Background information

  • Use the NasConfig: Auto property to configure a NAS file system and its dependent Virtual Private Cloud (VPC) resources.

    You can store large-scale and fixed resources in NAS when you use Function Compute. After you configure a NAS file system and its dependent resources, the functions of a specified service can access the NAS file system in the same way as they access an on-premises file system. This way, you can use Function Compute to efficiently develop a serverless application that depends on large-scale resources.

    However, the complex configurations of the NAS file system increase the difficulty in using Function Compute. To simplify the NAS configuration process, the NasConfig: Auto property is added to Funcraft. You can use this property to configure a NAS file system and its dependent VPC resources.

  • Run the fun nas command to manage NAS files and upload on-premises NAS files.
    After you configure the NAS file system, you must upload on-premises files to the NAS file system. You can use one of the following methods to upload on-premises files to the NAS file system:
    • Buy an Elastic Compute Service (ECS) instance, mount the NAS file system, and use an FTP server or a file hosting service to upload files to the NAS file system.
    • Use Data Transport to migrate Object Storage Service (OSS) resources to the NAS file system. For more information, see Background information.
    • Use Function Compute to develop a serverless application to manage NAS files. For more information, see Guidelines for Function Compute Development - Develop an Application to Manage NAS Files.
    • Run the fun nas command to manage NAS files and upload on-premises NAS files.
      The fun nas command provides an upgraded version of the third method to resolve the following issues:
      • A service must be manually deployed.
      • Directories cannot be uploaded.
      • Files larger than 6 MB in size cannot be uploaded.

Features of the fun nas command

Run the following command to query the features of the fun nas command:
fun nas -h
Expected output:
Usage: fun nas [options] [command]

Operate your remote NAS file system locally.

Options:
  -h, --help      display help for command

Commands:
  info            Print nas config information, such as local temp directory of NAS.
  init            For each service with NAS config, create local NAS folder and deploy fun nas server service.
  sync            Synchronize the local NAS directory with the remote NAS file system.
  ls              List contents of remote NAS directory
  rm              Remove remote NAS file.
  cp              copy local file/folder to remote NAS
  help [command]  display help for command

Configure a NAS file system

This section describes how to configure a NAS file system in two ways. Your functions can access specified NAS resources after the configuration. We recommend that you use Method 2 to configure a NAS file system.

Method 1

Use the NAS console and the VPC console to configure a NAS file system and its dependent resources.

  1. Log on to the VPC console. Create a virtual private cloud, a vSwitch, and a security group. For more information, see Overview.
  2. Log on to the NAS console. Create a NAS file system and add a mount target. For more information, see Get started with Apsara File Storage NAS.
  3. Prepare a template.yml file, which contains the following content. Set the VpcConfig and NasConfig properties in the template.yml file based on the resources created in the preceding steps. Then, save the template.yml file.
     ROSTemplateFormatVersion: '2015-09-01'
     Transform: 'Aliyun::Serverless-2018-04-03'
     Resources:
       nasDemo:
         Type: 'Aliyun::Serverless::Service'
         Properties:
           VpcConfig:
             VpcId: 'vpc-bp12hm92gdpcjtai7****'
             VSwitchIds: [ 'vsw-bp1gitru7oicyyb4u****' ]
             SecurityGroupId: 'sg-bp1243pi65bw4cjj****'
           NasConfig:
             UserId: 10003
             GroupId: 10003
             MountPoints:
                - ServerAddr: '012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com:/'
                 MountDir: '/mnt/nas'

    After you perform the preceding steps, you can run the fun local and fun deploy commands to deploy and debug functions.

Method 2

Use the template.yml file and the fun deploy command to configure a NAS file system and its dependent resources.

Note If you want to use a NAS file system and its dependent resources without involving the virtual private cloud, vSwitch, and mount target, we recommend that you use Method 2 for configuration.
  1. Create a template.yml file in the root directory of a function. For more information, see Serverless Application Model. Add the following content to the template.yml file that contains the NasConfig: Auto property:
    ROSTemplateFormatVersion: '2015-09-01'
    Transform: 'Aliyun::Serverless-2018-04-03'
    Resources:
      nasDemo:
        Type: 'Aliyun::Serverless::Service'
        Properties:
          NasConfig: Auto
  2. Run the fun deploy command in the root directory.
    Expected output:
    Waiting for service nasDemo to be deployed...
        ... ...
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically
            ... ...
        generated auto VpcConfig done:  {"vpcId":"vpc-bp1pcr7o8ksmgojt4****","vswitchIds":["vsw-bp13qdw9qe1i2it4y****"],"securityGroupId":"sg-bp161u0547x0lq5k****"}
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically
            ... ...
        generated auto NasConfig done:  {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"****c4a7ac-vrk7.cn-hangzhou.nas.aliyuncs.com:/nasDemo","MountDir":"/mnt/auto"}]}
        Checking if nas directories /mnt/auto/nasDemo exists, if not, it will be created automatically
        Checking nas directories done ["/mnt/auto/nasDemo"]
        ... ...
    service nasDemo deploy success            
    Note
    • The result of NasConfig: Auto is idempotent. If the dependent resources do not exist, they are automatically created. If the dependent resources exist, they are directly used.
    • The fun local and fun deploy commands both support the NasConfig: Auto property.
    • When you use the NasConfig: Auto property, the NAS file system is automatically attached to the /mnt/auto directory of the Function Compute runtime.

References

You can run the following commands to view the features of the fun nas command.

Create a template.yml file in the root directory of a function. For more information, see Serverless Application Model. Add the following content to the template.yml file that contains the NasConfig: Auto property:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  nasDemo:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      NasConfig: Auto            
  • Run the following command in the root directory to initialize the configurations:
    fun nas init
    Expected output:
    start fun nas init... 
    Waiting for service fun-nas-NasDemo to be deployed...   
      ... ...     
        using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically     
      ... ...     
        generated auto VpcConfig done:  {"vpcId":"vpc-bp1pcr7o8ksmgojt4****","vswitchIds":["vsw-bp13qdw9qe1i2it4y****"],"securityGroupId":"sg-bp161u0547x0lq5k****"}     
        using 'NasConfig: Auto', Fun will try to generate related nas file system automatically     
      ... ...     
        generated auto NasConfig done:  {"UserId":100**,"GroupId":100**,"MountPoints":[{"ServerAddr":"****c4a6d9-gry74.cn-hangzhou.nas.aliyuncs.com:/fun-nas-NasDemo","MountDir":"/mnt/auto"}]}     
        Checking if nas directories /mnt/auto/fun-nas-NasDemo exists, if not, it will be created automatically     
        Checking nas directories done ["/mnt/auto/fun-nas-NasDemo"]     
      ... ... 
    service fun-nas-NasDemo deploy success
     
    Fun nas init Success 
    
    Tips for next step 
    ====================== 
    $ fun nas info      # Show NAS info 
    $ fun nas ls        # List NAS files 
    $ fun nas sync      # Synchronize files to nas 
    $ fun deploy        
    # Deploy Resources
    Note After you run the fun nas init command, a NAS file system and its dependent VPC resources are created. If you modify the NAS or VPC configuration, you need only to run the fun nas init or fun vpc init command to initialize the configurations.
  • Run the following command in the root directory to view the NAS directory:
    fun nas info
    Expected output:
    Local NAS folder of service NasDemo includes: 
    /NasDemo/.fun/nas/auto-default/NasDemo
  • Run the following command in the root directory to synchronize the information about on-premises NAS resources. If the on-premises NAS resources are modified, you need only to run the following command again:
    fun nas sync
    Expected output:
    Starting upload /NasDemo/.fun/nas/auto-default/NasDemo to nas://NasDemo:/mnt/auto/ 
    NAS path checking... 
    zipping /NasDemo/.fun/nas/auto-default/NasDemo
      /NasDemo/.fun/nas/auto-default/.NasDemo.zip - zipped
    generate tmpDir: /NasDemo/.fun/nas/auto-default/.fun_nas_tmp/42658c92eef63f2b3b13e5f46315ee7d
    Split zip file to 1 small files 
    Uploading... 
    Upload /NasDemo/.fun/nas/auto-default/NasDemo to /mnt/auto/ done!
  • Run one of the following commands in the root directory to view NAS files:
    • Use fun nas ls nas://ServiceName/Path to display the files pointed by the NAS path in a service. Run the following command to view the NAS files:
      fun nas ls -a nas://NasDemo/mnt/auto
      Expected output:
      total 10 
      drwxr-xr-x 4 user10003 10003 4096 Aug  4 11:38 . 
      drwxr-xr-x 1 root      root  4096 Aug  4 11:37 .. 
      drwxr-xr-x 4 user10003 10003 4096 Aug  4 11:38 .fun_nas_tmp 
      -rw-r--r-- 1 user10003 10003   19 Aug  4 11:38 test 
      drwxr-xr-x 2 user10003 10003 4096 Aug  4 11:38 testDir
    • If the template.yml file declares only one service, you can use the fun nas ls nas:///Path command by removing the ServiceName parameter. Run the following command to view the NAS files:
      fun nas ls -a nas://mnt/auto

Demo

fun nas