×
Community Blog Using Alibaba Cloud's Network Attached Storage (NAS) with Function Compute

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.

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.

1

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

2

Select the following details in your region and click OK.

3

In the file system details, click Add Mount Point.

4

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

Click OK.

5

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

Click through to Manage.

6

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

7

You will see details of the Mount Point.

8

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.

9

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

10

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

11

Add the NAS configuration details.

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

12

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

13

Confirm the Authorization Policy.

14

Once the role has been authorized, click OK.

15

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.

16

We'll select the Empty Function.

17

Leave Trigger and click Next.

18

Give the Function a name and select the runtime.

Then click Next.

19

Add NAS Full Access role Permission Configuration and Authorize.

20

Confirm the Authorization Policy.

21

Click Next.

22

Check the details and click Create.

23

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

Click through to the Code.

24

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.

25

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

26

Now click Invoke to Invoke the code.

27

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

28

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.

29

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

30

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.

31

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

file_path is the result of the previous read function.

Click OK.

32

Click Save and Invoke.

33

You will see the successful result in the output.

34

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.

0 0 0
Share on

Alibaba Clouder

1,408 posts | 218 followers

You may also like

Comments

Alibaba Clouder

1,408 posts | 218 followers

Related Products