Using Alibaba Cloud’s Network Attached Storage (NAS) with Function Compute

In this tutorial, we will show you how to read and write data to Alibaba Cloud’s Network Attached Storage (NAS) service from an Alibaba Cloud Function Compute serverless function.

What Is Alibaba Cloud’s Function Compute?

Alibaba Cloud’s Function Compute is a fully-managed, event-driven compute service for serverless applications. Developers focus on writing and uploading code without having to worry about building the underlying infrastructure or maintaining complicated server architectures.

Alibaba Cloud’s Function Compute service has myriad applications, including API gateways, data lake analysis, log stores and backups, web crawlers and image recognition, to name just a few. With Function Compute, developing is convenient and reliable, and it supports many different programming languages including Java, Python, PHP, and NodeJS.

Alibaba Cloud’s Function Compute provides real-time auto scaling and dynamic load balancing for managing heavy traffic bursts within millisecond timeframes. The compute resources offered make code flexible and reliable. Furthermore, Function Compute offers a Pay-As-You-Go option which means you only have to pay for consumed resources. No fee is incurred if the code doesn’t run. Moreover, the code run duration is measured in milliseconds.

A few common business scenarios for Alibaba Cloud’s Function Compute include analysis and management of media assets, such as integrating a range of services that run an elastic and highly available backend video system. Another is building a serverless backend that triggers Function Compute code to render dynamic webpages and static webpages housed in Alibaba Cloud’s Object Storage Service. Web applications like these also have the capability to connect to external APIs when Function Compute is integrated with Alibaba Cloud’s API Gateway. Another interesting business scenario is implementing Function Compute to manage real time IoT message processing and monitoring of data streams.

What Is Network Attached Storage (NAS)?

Alibaba Cloud’s Network Attached Storage (NAS) is a file storage service that supports standard file access protocols. This means you don’t have to modify existing applications; both Windows and Linux applications can use the NAS service.

Alibaba Cloud’s NAS service is extremely reliable and guarantees high throughput and low latency. NAS gives you a distributed file system with unlimited capacity as well as performance scaling. Simple and easy to use, NAS provides mount points that connect to Alibaba Cloud Elastic Compute Service nodes residing on VPCs. The mount points allow programmers to perform file operations as if the files were stored locally.

With Function Compute, we will demonstrate the ease with which you can prepare code to access Alibaba Cloud’s Network Attached Storage. We will demonstrate read and write operations in Python, although a number of languages are available.

Prerequisites

You will need an Alibaba Cloud account. If you don’t already have one, head over to the Free Trial page to get $300–1200 worth of Alibaba Cloud credit to play around with in the Alibaba Cloud Free Trial.

You will also need a Virtual Private Cloud (VPC) installed and up and running in your region. The VPC must have a Security Group and an ECS running on it.

Let’s get started.

Create a NAS File System and Mount Point

Go to the console page and click through to NAS.

Image for post
Image for post

In the NAS console in your region, click Create File System.

Image for post
Image for post

Select the following details in your region and click OK.

Image for post
Image for post

In the file system details, click Add Mount Point.

Image for post
Image for post

Connect up your VPC to the NAS Mount Point and configure the default Permission Group.

Click OK.

Image for post
Image for post

The NAS console will refresh and you will see the details of the Mount Point.

Click through to Manage.

Image for post
Image for post

Make a note of the Mount Address then hover over the Mount Address.

Image for post
Image for post

You will see details of the Mount Point.

Image for post
Image for post

Now we are ready to create a Function Compute Service that connects to the NAS via the Mount Point.

Create a Function Compute Service with NAS Configuration

Click through to the Function Compute console from the Products page.

Image for post
Image for post

In your region, click the Create Service add button, enter a Service Name, and slide open Advanced Settings.

Image for post
Image for post

In Advanced Settings, select your VPC details and scroll down to NAS Config settings.

Image for post
Image for post

Add the NAS configuration details.

-1 is the default UserId and indicates a random user. We’ve used 168 as the recommended GroupId.

Image for post
Image for post

Scroll down to configure and Authorize the NAS Full Access role.

Image for post
Image for post

Confirm the Authorization Policy.

Image for post
Image for post

Once the role has been authorized, click OK.

Image for post
Image for post

Now we’re ready to write some code that reads and writes data to the NAS we just set up.

Create a NAS Write Function

In the Service details, click the add button to create a new Function.

Image for post
Image for post

We’ll select the Empty Function.

Image for post
Image for post

Leave Trigger and click Next.

Image for post
Image for post

Give the Function a name and select the runtime.

Then click Next.

Image for post
Image for post

Add NAS Full Access role Permission Configuration and Authorize.

Image for post
Image for post

Confirm the Authorization Policy.

Click Next.

Image for post
Image for post

Check the details and click Create.

Image for post
Image for post

You will see the details of the Function you just created.

Click through to the Code.

Image for post
Image for post

Copy-paste the following code into the IDE.

import json
import logging
import random
import string
import os
def handler(event, context):
logger = logging.getLogger()
evt = json.loads(event)
root_dir = evt["root_dir"]
sub_dir = randomString(16)
logger.info('uid : ' + str(os.geteuid()))
logger.info('gid : ' + str(os.getgid()))
file_name = randomString(6)+'.txt'
newDir = root_dir + '/' + sub_dir + '/'
content = "NAS here I come"
os.mkdir(newDir)
fw = open(newDir+file_name, "w+")
fw.write(content)
fw.close()
return sub_dir + '/' + file_name
def randomString(n):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(n))

Notice that the root_dir parameter points to an event.

Click Event.

Image for post
Image for post

In the Event details, add the following key-value pairs and click OK.

Image for post
Image for post

Now click Invoke to Invoke the code.

Image for post
Image for post

In the output underneath the code you will see the result. Here we can see the file path for the successful NAS write.

Image for post
Image for post

Make a note of the resulting file path.

Now we’re ready to create a NAS read function with another Function Compute function.

Create a NAS Read Function

Click the add button next to Functions and click Create Function.

Image for post
Image for post

Follow all the setup steps as in the previous section until you are ready to enter the code.

Image for post
Image for post

In the Code Management IDE, enter the following code:

import json
import logging
import random
import string
import os
def handler(event, context):
logger = logging.getLogger()
evt = json.loads(event)
root_dir = evt["root_dir"]
file_path = evt["file_path"]
logger.info('uid : ' + str(os.geteuid()))
logger.info('gid : ' + str(os.getgid()))
fw = open(root_dir + '/' + file_path, "r")
content = fw.read()
fw.close()
return content

We now have to create an Event for the key-value pairs. Click Event.

Image for post
Image for post

In the Event pull out, add the following key-value pairs.

file_path is the result of the previous read function.

Click OK.

Image for post
Image for post

Click Save and Invoke.

Image for post
Image for post

You will see the successful result in the output.

Image for post
Image for post

Summary

First, we told you about Alibaba Cloud’s Function Compute serverless cloud product and how you can use it for coding multiple types of applications and scenarios.

We then told you a little bit about Alibaba Cloud’s Network Attached Storage service and how it simplifies applications requiring networked storage. We told you about some of its features and benefits, specifically its ease of use.

We then informed you that the tutorial’s goal is to demonstrate the simplicity and flexibility of Alibaba Cloud’s NAS service by building an Alibaba Cloud Function Compute service that accesses Alibaba Cloud’s Network Attached Storage and performs some simple input/output operations.

We told you about the prerequisites for building a NAS service, detailed the process of how to build a NAS service in Alibaba Cloud and then how to build a Function Compute service that connects to the NAS service. After that, we demonstrated simple Python code that reads and writes from Alibaba Cloud’s Function Compute service to the NAS service.

Watch out for more tutorials on products and services from Alibaba Cloud.

Reference:https://www.alibabacloud.com/blog/using-alibaba-clouds-network-attached-storage-nas-with-function-compute_594657?spm=a2c41.12741410.0.0

Written by

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