Use Serverless Devs To Manage Function Computing Resources
1. Experiment
1.1 Knowledge points
In this experiment, Alibaba Cloud Function Compute is used. This experiment describes how to use Serverless Devs to manage Function Compute resources.
Serverless Devs is an open source platform for serverless application developers. Serverless Devs allows you to develop, create, test, and deploy an application and to manage the full lifecycle of the application.
1.2 Experiment process
- Install Serverless Devs
- Configure Serverless Devs
- Deploy the service
- Example: Building a frontend CICD
1.3 Scene architecture diagram

1.4 Cloud resources required
1.5 Prerequisites
- If you’re using your own Alibaba Cloud account instead of the account provided by this lab during the experiment, please note that you’ll need to choose the same Ubuntu 16.04 operating system for your ECS, in order to run the experiment smoothly.
- Before starting the experiment, please confirm that the previous experiment has been closed normally and has been exited.
2. Start the experiment environment
Click Start Lab in the upper right corner of the page to start the experiment.
.
After the experiment environment is successfully started, the system has deployed resources needed during the experiment in the background. For example, the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.

After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have an hour to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:

Go to the logon page of Alibaba Cloud console.

Fill in the sub-user account and click on Next.

Fill in the sub-user password and click on Login.

After successfully log on to the console, you will see the page showing as the following page.

3. Install Serverless Devs
Click Elastic Compute Service, as shown in the following figure.

We can see one running ECS instance in Singapore region.
Copy this ECS instance’s Internet IP address and remotely log on to this ECS (Ubuntu system) instance. For details of remote login, refer to login.

The default account name and password of the ECS instance:
Account name: root
Password: nkYHG890.
After you log on to the ECS instance, run the following command to download the latest script and install and execute Serverless Devs.
curl -o- -L http://cli.so/install.sh | bash

After you install Serverless Devs, run the last command in the preceding figure to make the Serverless Devs take effect.

Run the following command to check whether Serverless Devs is installed:
s -v

4.1 Create AccessKey
As shown below, click AccessKey Management.

Click Create AccessKey. After AccessKey has been created successfully, AccessKeyID and AccessKeySecret are displayed. AccessKeySecret is only displayed once. Click Download CSV FIle to save the AccessKeySecret

4.2 Add the AccessKey pair
Go to the CLI of the ECS instance.
Run the following command to add the AccessKey pair:
s config add

Run the following command to view the AccessKey pair that you added:
s config get

4.3 Install Docker
Run the following command to update the APT installation source:
apt update

Run the following command to add the GPG key of the official Docker repository to the system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Run the following command to add the IP address of the Docker repository to the APT installation source:
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" | sudo tee -a /etc/apt/sources.list.d/docker.list

Run the following command to update the APT installation source again:
apt update

Run the following command to install Docker:
apt install docker-ce -y

Run the following command to view the Docker status:
systemctl status docker

Docker has been started.
Enter “q” to exit.
5. Deploy the service
Run the following command to download the sample service: It takes about 5 minutes.
s init django-blog



Run the following command to open the s.yaml configuration file of the project:
cd django-blog
vim s.yaml

Set the region in the file to ap-southeast-1, as shown in the following figure. Save the file and exit.

Run the following command to deploy the project to Function Compute: It takes about 3 minutes.
s deploy


Copy the domain name in the preceding figure to the browser. You can see that the service is started.

Go to the Alibaba Cloud Function Compute console, as shown in the following figure.

Select the Singapore region.

Click the service name to go to the service details page.

You can see that a function has been created. Click the function name to view the code of the function.

Go back to the CLI of the ECS instance and run the following command to delete the deployed resource:
cd ~/django-blog/
s remove


Go back to the Function Compute console. You can see that the resource has been deleted.
6. Example: Building a frontend CICD
6.1 Create an SSH key pair
Run the following command and press Enter for all prompts.
ssh-keygen -t rsa

Run the following command. You can see the id_rsa private key file and the id_rsa.pub public key file. You will use the files later.
cd ~/.ssh && ls

6.2 Create a repository
Enter the link below to enter the Alibaba Cloud code repository console.
https://codeup.aliyun.com/
create a repository as shown in the following figure.

Set the name of the repository.

After you create the repository, go to the project and click Settings.

Set the whitelist as shown in the following figure.

Go to Personal Settings and add the SSH public key.

How to obtain the SSH public key? Run the following command in the CLI of the ECS instance:
cat ~/.ssh/id_rsa.pub

The whitelist is added.

Go back to the file list of the project and copy the project address.

Go back to the CLI of the ECS instance. Run the following command. Replace <YOUR-SSH-URL> with your actual value.
git clone YOUR-SSH-URL

Run the following commands:
cd frontent
echo "Hello, LabEx!" > index.js
echo "Hello, LabEx2!" > index.html

Run the following command to add a file to the workspace.
git add .

Run the following command to configure the git information:
git config --global user.email "labex@aliyun.com"
git config --global user.name "labex"

Run the following command to commit the file to the repository:
git commit -m "first, prepare file"

Run the following command to upload the file to the repository:
git push

The file is uploaded.

6.3 Prepare the Object Storage Service (OSS) Environment
Go to the Alibaba Cloud OSS console, as shown in the following figure.

You can see that a bucket has been created. Click the bucket name to enter the bucket.

Click Upload to upload the file, as shown in the following figure.

Create a file named my_ssh_executable.sh on your local computer. Copy the following content to the file and save the file.
#!/bin/sh
ID_RSA=/tmp/id_rsa
exec /usr/bin/ssh -o StrictHostKeyChecking=no -i $ID_RSA "$@"
Create a file named id_rsa. Copy the content in the ~/.ssh/id_rsa file to the id_rsa file and save the content.

Upload both files to the bucket.

6.4 Create a function
Go to the CLI of the ECS instance.
Run the following command to install a sample project:
cd && s init start-fc-http-python3 -d start-fc-http-python3


Run the following command to go to the directory of the project:
cd start-fc-http-python3 && ls

Enter the vim code/index.py
command, delete the original content, and copy the following content to the index.py file and save the file. Replace <YOUR-ACCESS-KEY>, <YOUR-ACCESS-SECRET>, and <YOUR-BUCKET-NAME> with your actual values.
# -*- coding: utf-8 -*-
import oss2
import subprocess
import os
import time
import ast
import shutil
HELLO_WORLD = b'200 OK!\n'
def handler(environ, start_response):
# Configure OSS access endpoints
endpoint = 'oss-ap-southeast-1-internal.aliyuncs.com'
# Configure bucket name
bucketname = 'YOUR-BUCKET-NAME'
# To configure accessKeyId and accessKeySecret, you need to have read and write OSS permissions
accessKeyId = 'YOUR-ACCESS-KEY'
accessKeySecret = 'YOUR-ACCESS-SECRET'
auth = oss2.Auth(accessKeyId, accessKeySecret)
bucket = oss2.Bucket(auth, endpoint, bucketname)
# Download the sshkey private key file from OSS
bucket.get_object_to_file('id_rsa', '/tmp/id_rsa')
# Download the custom executable file from OSS and select the specified sshkey public key file
bucket.get_object_to_file('my_ssh_executable.sh', '/tmp/my_ssh_executable.sh')
# Modify file permissions
subprocess.Popen(['chmod 0600 /tmp/id_rsa'], shell=True)
subprocess.Popen(['chmod +x /tmp/my_ssh_executable.sh'], shell=True)
# get request_body
try:
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
except (ValueError):
request_body_size = 0
request_body = environ['wsgi.input'].read(request_body_size)
request_body = request_body.decode()
print (request_body)
# json formatted object
request_body = ast.literal_eval(request_body)
# get branch
branch = request_body.get('ref').split('/')[-1]
# get sshurl
sshurl = request_body.get('repository').get('git_ssh_url')
# sshurl = request_body.get('html_url')
# get repository name
repositoryname = request_body.get('repository').get('name')
print (branch, sshurl, repositoryname)
localpath = '/tmp/{}'.format(repositoryname)
# delete existing directory
if os.path.exists(localpath):
shutil.rmtree(localpath)
if os.path.exists("/tmp/my_ssh_executable.sh") and os.path.exists("/tmp/id_rsa"):
print("id_rsa and ssh_executable.sh config exists")
else:
print("not exists\n")
# Use the specified sshkey public key file to download the corresponding branch code file and store it in the /tmp directory
gitclone = 'cd /tmp; GIT_SSH="/tmp/my_ssh_executable.sh" git clone -b {b} {u}'.format(b=branch, u=sshurl)
print(gitclone)
try:
out_bytes = subprocess.check_output([gitclone], shell=True, timeout=10)
except subprocess.CalledProcessError as e:
out_bytes = e.output # Output generated before error
code = e.returncode # Return code
# Traverse all files in the download branch and upload to OSS
rt = subprocess.Popen(['find {localpath} -type f ! -path "{localpath}/.git/*"'.format(localpath=localpath)],
shell=True, stdout=subprocess.PIPE)
files = rt.stdout.readlines()
for f in files:
localfile = f.decode().replace("\n", "")
ossfile = localfile.replace(localpath, repositoryname, 1)
print (localfile, ossfile)
if os.path.isfile(localfile):
bucket.put_object_from_file(ossfile, localfile)
# do something here
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [HELLO_WORLD]

Run the vim s.yaml
command to open the configuration file. Modify the configuration file, as shown in the following figure.

Run the following command to deploy the service to Function Compute:
s deploy


Go to the Alibaba Cloud Function Compute console and select the Singapore (Singapore) region.


You can see that the code has been deployed.

Copy the trigger address of the function.

Copy the address to the webhook of your repository.


The trigger address is added.

Go back to the Alibaba Cloud Function Compute console and modify the configurations of the hello-world-service project.

Allow the function to access the Internet.

6.5 Test
Go back to the CLI of the ECS instance. Run the following command to modify a file and upload it to the repository:
cd ~/frontent && echo "Hello, LabEx test" >> "index.js"
git add .
git commit -m "add test"
git push

Go to the Alibaba Cloud OSS console. You can see that the frontent repository has been synchronized to the bucket.


Obtain the URL of the bucket.

Copy the URL to your browser. Add the path /frontend/index.js to the end of the URL to access the bucket.

Reminder:
Before you leave this lab, remember to log out your Alibaba RAM account before you click the Stop button of your lab. Otherwise, you’ll encounter some issues when opening a new lab session in the same browser:


7. Experiment summary
This experiment describes how to use Serverless Devs to manage Function Compute resources. Serverless Devs allows users to use serverless services and frameworks in a pluggable manner and develop components and plug-ins. This improves the efficiency of O&M. Serverless Devs also allows users to efficiently develop, create, test, and deploy an application and to manage the full lifecycle of the application.