arrow

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

image desc

1.4 Cloud resources required

  • ECS
  • Function Compute

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.

image desc.

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.

image desc

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:

image desc

Go to the logon page of Alibaba Cloud console.

image desc

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

image desc

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

image desc

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

image desc

3. Install Serverless Devs

Click Elastic Compute Service, as shown in the following figure.

image desc

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.

image desc

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

image desc

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

image desc

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

s -v

image desc

4. Configure Serverless Devs

4.1 Create AccessKey

As shown below, click AccessKey Management.

image desc

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

image desc

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

image desc

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

s config get

image desc

4.3 Install Docker

Run the following command to update the APT installation source:

apt update

image desc

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 -

image desc

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

image desc

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

apt update

image desc

Run the following command to install Docker:

apt install docker-ce -y

image desc

Run the following command to view the Docker status:

systemctl status docker

image desc

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

image desc

image desc

image desc

Run the following command to open the s.yaml configuration file of the project:

cd django-blog

vim s.yaml

image desc

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

image desc

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

s deploy

image desc

image desc

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

image desc

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

image desc

Select the Singapore region.

image desc

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

image desc

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

image desc

Go back to the CLI of the ECS instance and run the following command to delete the deployed resource:

cd ~/django-blog/

s remove

image desc

image desc

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

image desc

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

image desc

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.

image desc

Set the name of the repository.

image desc

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

image desc

Set the whitelist as shown in the following figure.

image desc

Go to Personal Settings and add the SSH public key.

image desc

How to obtain the SSH public key? Run the following command in the CLI of the ECS instance:

cat ~/.ssh/id_rsa.pub

image desc

The whitelist is added.

image desc

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

image desc

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

image desc

Run the following commands:

cd frontent

echo "Hello, LabEx!" > index.js

echo "Hello, LabEx2!" > index.html

image desc

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

git add .

image desc

Run the following command to configure the git information:

git config --global user.email "labex@aliyun.com"

git config --global user.name "labex"

image desc

Run the following command to commit the file to the repository:

git commit -m "first, prepare file"

image desc

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

git push

image desc

The file is uploaded.

image desc

6.3 Prepare the Object Storage Service (OSS) Environment

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

image desc

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

image desc

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

image desc

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.

image desc

Upload both files to the bucket.

image desc

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

image desc

image desc

Run the following command to go to the directory of the project:

cd start-fc-http-python3 && ls

image desc

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]

image desc

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

image desc

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

s deploy

image desc

image desc

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

image desc

image desc

You can see that the code has been deployed.

image desc

Copy the trigger address of the function.

image desc

Copy the address to the webhook of your repository.

image desc

image desc

The trigger address is added.

image desc

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

image desc

Allow the function to access the Internet.

image desc

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

image desc

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

image desc

image desc

Obtain the URL of the bucket.

image desc

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

image desc

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:

image descimage desc

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.