Run, Debug, and Release NAS Configured Functions Locally

By Yi Xian

This article covers the local debugging, running, and releasing of functions configured with NAS, covering several different possible methods you can use. But before we get into that, let’s first take a look at several important concepts mentioned in this article.

Background Information

First, there’s Alibaba Cloud’s Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the correct amount of computing resources to run user code. The user only pays for the resources required to run their code.

Then, there’s also Fun, which is a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the template.yml file.

Last, there’s Fun Local, which is a sub-command of Fun. You can use it directly through the fun local command. The Fun Local tool can fully simulate and run the functions in Function Compute locally and provides the single-step debugging feature, which makes up for Function Compute shortcomings when compared with traditional application development experience, and provide users with a new way to solve Function Compute problems.

Note: The tips described in this article require the Fun version 2.8.0 and later.

Configure the NAS Service for Function Compute

First, let’s assume that you have already activated and created the NAS service. From there, just add the following configuration in the file:

NasConfig: 
UserId: -1
GroupId: -1
MountPoints:
- ServerAddr: '012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com:/'
MountDir: '/mnt/test'

The above configuration indicates that the NAS remote directory is mounted under for Function Compute. UserId and GroupId are -1, indicating that UID and GID are randomly assigned by the system.

Running It Locally

Run in an Event-Triggered Manner

For the sample code that triggers the NAS configured function to run by events, see here.

The template description is as follows:

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
nasDemo:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'fc nas test'
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: 'vpc-bp12hm92gdpcjtai7ua82'
VSwitchIds: [ 'vsw-bp1gitru7oicyyb4uiylj' ]
SecurityGroupId: 'sg-bp1243pi65bw4cjj4bks'
NasConfig:
UserId: -1
GroupId: -1
MountPoints:
- ServerAddr: '012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com:/'
MountDir: '/mnt/test'
readNas:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: read.handler
Runtime: nodejs8
CodeUri: './read.js'
Timeout: 100
EnvironmentVariables:
ROOT_DIR: /mnt/test
writeNas:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: write.handler
Runtime: python2.7
CodeUri: './write.py'
Timeout: 100
EnvironmentVariables:
ROOT_DIR: /mnt/test

We can see that a service and two functions are described in the file, and the VPC and NAS services are configured. The method of running the function configured with NAS in the event-triggered mode is very simple. You can directly run the function using the command.

The following is a dynamic demonstration of the NAS demo triggered by a local event:

Run through the HTTP Trigger

For the sample code that triggers the NAS configured function to be invoked by the HTTP trigger, see here. The template description is as follows:

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
nasHttpDemo:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'fc nas test'
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: 'vpc-bp12hm92gdpcjtai7ua82'
VSwitchIds: [ 'vsw-bp1gitru7oicyyb4uiylj' ]
SecurityGroupId: 'sg-bp1243pi65bw4cjj4bks'
NasConfig:
UserId: -1
GroupId: -1
MountPoints:
- ServerAddr: '012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com:/'
MountDir: '/mnt/test'
readNas:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: read.handler
Runtime: nodejs8
CodeUri: './read.js'
Timeout: 100
EnvironmentVariables:
ROOT_DIR: /mnt/test
Events:
http-test: # trigger name
Type: HTTP # http trigger
Properties:
AuthType: ANONYMOUS
Methods: ['GET', 'POST', 'PUT']
writeNas:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: write.handler
Runtime: python3
CodeUri: './write.py'
Timeout: 100
EnvironmentVariables:
ROOT_DIR: /mnt/test
Events:
http-test: # trigger name
Type: HTTP # http trigger
Properties:
AuthType: ANONYMOUS
Methods: ['GET', 'POST', 'PUT']

We can see that a service named , and two functions called and are described in the file, and the VPC and NAS services are configured. The runtime of these two functions are and and both are configured with HTTP Triggers.

The method of running the function in the HTTP trigger mode is also relatively simple. You can run the function directly in the project using the command. Fun Local automatically recognizes that the two functions are configured with HTTP triggers and the NAS service is configured, and will mount the corresponding directories for the function to use according to the configuration.

The following is a dynamic demonstration of the NAS HTTP demo triggered by the HTTP trigger:

Run through APIs

The method of triggering the function configured with NAS to run through APIs is similar to that through HTTP triggers. You only need to run the command. When the HTTP trigger is not configured for the function, it is automatically registered as an API. The call can be completed by configuring the prompted URL into the SDK and writing relevant code.

Local Debugging

The method of debugging the function locally is the same as that of triggering the function through events, HTTP Triggers and APIs. For more information, see here.

Releasing

Regardless of the type of the NAS configured function, to release NAS, you only need to run the command to release the function.

For functions triggered by events, the release log is as follows:

After the event-triggered function is successfully released, you can call it through the Function Compute console, fcli, and APIs.

For functions triggered by HTTP Triggers, the release log is as follows:

The corresponding function can be called by opening the address in the log.

Simulating NAS Locally

Fun Local scans the configuration in the template before simulating and running the function locally. If NAS configuration is detected, a directory is created locally to simulate the NAS service mount point, and mount it to the container where the function is located. When the function is running, the directory can be accessed.

The simulated NAS directory created by Fun Local is located at: .

For example, for the example above, the simulated NAS directory is .

After knowing the rule, you can make some preparations for the function to run. For example, if the running of the function depends on some resources, such as machine learning models, data files, and PHP code, these resources can be copied to the simulated NAS directory in advance, so as to verify the behavior of the function faster.

Original Source

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store