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.
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.
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.
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.
Go to the console page and click through to NAS.
In the NAS console in your region, click Create File System.
Select the following details in your region and click OK.
In the file system details, click Add Mount Point.
Connect up your VPC to the NAS Mount Point and configure the default Permission Group.
The NAS console will refresh and you will see the details of the Mount Point.
Click through to Manage.
Make a note of the Mount Address then hover over the Mount Address.
You will see details of the Mount Point.
Now we are ready to create a Function Compute Service that connects to the NAS via the Mount Point.
Click through to the Function Compute console from the Products page.
In your region, click the Create Service add button, enter a Service Name, and slide open Advanced Settings.
In Advanced Settings, select your VPC details and scroll down to NAS Config settings.
Add the NAS configuration details.
-1 is the default UserId and indicates a random user. We've used 168 as the recommended GroupId.
Scroll down to configure and Authorize the NAS Full Access role.
Confirm the Authorization Policy.
Once the role has been authorized, click OK.
Now we're ready to write some code that reads and writes data to the NAS we just set up.
In the Service details, click the add button to create a new Function.
We'll select the Empty Function.
Leave Trigger and click Next.
Give the Function a name and select the runtime.
Then click Next.
Add NAS Full Access role Permission Configuration and Authorize.
Confirm the Authorization Policy.
Check the details and click Create.
You will see the details of the Function you just created.
Click through to the Code.
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.
In the Event details, add the following key-value pairs and click OK.
Now click Invoke to Invoke the code.
In the output underneath the code you will see the result. Here we can see the file path for the successful NAS write.
Make a note of the resulting file path.
Now we're ready to create a NAS read function with another Function Compute function.
Click the add button next to Functions and click Create Function.
Follow all the setup steps as in the previous section until you are ready to enter the code.
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.
In the Event pull out, add the following key-value pairs.
file_path is the result of the previous read function.
Click Save and Invoke.
You will see the successful result in the output.
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.
Alibaba Clouder - June 16, 2020
Ryan Zhang - September 18, 2018
Alibaba Cloud Serverless - April 7, 2020
Alibaba Clouder - November 3, 2017
Alibaba Cloud Serverless - April 7, 2020
Alibaba Clouder - June 23, 2020
Alibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.Learn More
Simple, scalable, on-demand and reliable network attached storage for use with ECS instances, HPC and Container Service.Learn More
An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the worldLearn More
More Posts by Alibaba Clouder