Volcano Engine Function as a ServiceBest PracticesData processingUsing Function as a Service to read and write NAS file system
Copy page
Download PDF
Data processing
Using Function as a Service to read and write NAS file system
Copy page
Download PDF
Using Function as a Service to read and write NAS file system
Mounting a NAS file system on a function enables persistent data storage and sharing among multiple functions. This topic introduces how to use Volcano Engine Function as a Service to read and write the NAS file system.
Background information
This article uses the following example code package to demonstrate to you how to read from and write into a FileNAS filesystem. The core logic of the code is: When receiving HTTP trigger request, the function records the request ID in a .txt file, writes it in the mounted NAS file system, and returns all received request IDs along with the current request ID.
vefaas-golang-nas-sample.zip
tip
This code logic is for reference only. Please modify it as needed according to your business scenario when actually using it.
Prerequisites
For Virtual Private Cloud (VPC)
Permission for using Volcano Engine Virtual Private Cloud has been obtained.
In the top navigation bar, select the target region.
On the Function list page, click Create function, then select Create [Event function].
Basic configuration
Enter a function name and notes.
Function code
Runtime: Select Golang.
Code template: Select vefaas-golang-default.
Advanced configuration
Configure VPC access and storage and keep other options unchanged.
VPC access
Configure the Virtual Private Cloud, subnet, and security groups to be bound to the function. The VPC configured for the function must match the VPC of the NAS mount point.
Storage configuration
Configure the NAS file system, mount point, remote directory, and local directory.
NAS file system: Select a NAS file system of the same region.
Mount point: select the NAS mount point that is located in the same VPC as the function.
Remote directory: configure the remote directory to be accessed on the NAS. For example /doc-nas-1.
Local directory: Configure the local directory for the function operation environment. This example is: /mnt/nas1.
Click Create to complete function creation.
Step Two: Upload the code package and configure environment variables
Upload the code package
On the code tab of the current function, click Upload to upload the example code package provided in the Background information.
Configure environment variables
Switch to the Basic information tab of the current function.
Click the Edit icon next to the configuration information to configure environment variables for the function: NAS_LOCAL_PATH:{the local directory corresponding to the NAS mount point}. This example is NAS_LOCAL_PATH:/mnt/nas1.
tip
In this sample code, Function as a Service obtains the local directory of the NAS file system of the current function configuration through NAS_LOCAL_PATH.
Step Three: Release the function
Release the current code and configuration of the function online.
On the function details page, click Release in the upper-right corner.
Select Latest for the function version. Keep other parameters as default.
Click OK. Wait until the function release is finished.
Validating results
Test the function to verify whether the response result meets expectations.
On the function details page, click Test in the upper-right corner.
On the pop-up function test page, configure the Trigger type and test request.
Trigger type: Select API Gateway Trigger.
Test request: Keep the default.
Click Test. Wait for the test result. As shown in the figure: The function returned all received request IDs and the current request ID as expected.
Log in to the function instance through Webshell to check whether a txt file named with the request ID is written in the local directory.
Switch to the Resource management tab.
In the instance list, select any running instance, click Webshell, and log in to the current instance.
Enter the local directory corresponding to the NAS mount point (This example is /mnt/nas1). You can see multiple txt files named with request IDs are written in the directory as expected.
tip
This sample code generates a .txt file named after the request ID each time a request is received. Since this example performs 7 function tests, there are a total of 7 files in the directory.