Tencent Cloud Computing is a leading innovator in Serverless computing, providing powerful services such as function as a Service (FaaS) runtimes, triggers, connectors and development tools.
Tencent Serverless Cloud Function (SCF) already supports more than a dozen programming languages and runtime frameworks. Tencent Cloud’s recent release of SCF Custom Runtime goes a step further. SCF can now support functions written in any programming language.
This article shows you how to run WebAssembly functions written in Rust in SCF.
In this paper, the content
Let’s start with some basic concepts, then review a complete but simple Hello World example to deploy your first WebAssembly Serverless function.
Finally, we’ll do something useful with a machine Learning as a Service (MLaaS) example. The example takes the data and returns a fitting model and visualization in SVG format.
Click to see the final application you’ll create at the end of this tutorial. It is completely “serverless” and incurs a cost only when used.
HTML and JavaScript UIs can be hosted on any computer, including laptops. Back-end functions on Tencent Cloud Serverless perform machine learning and SVG drawing.
Why WebAssembly and Rust
Traditional Serverless functions are based on a heavyweight framework. Developers must write functions within a specific application framework, such as JavaScript in Node.js or Python Boto.
Tencent Cloud SCF Custom Runtime breaks this pattern by allowing developers to write Serverless functions in any language.
To demonstrate this advantage, this article provides examples of Bash script-based functions, deno-based TypeScript functions, and Rust based native binary functions. This enables us to create and deploy Serverless functions based on web components on Tencent Cloud.
Why are we doing this? Here are some reasons.
- WebAssembly is designed for performance. WebAssembly functions can be up to 10 times faster than using JavaScript or Python.
- WebAssembly functions are portable. Although you can run native binaries on SCF Custom Runtime, these binaries must be compiled into the exact operating system environment for Custom Runtime. Currently, CentOS 7.6 is used on X86 cpus, which may change later. As we’ll see, WebAssembly functions are portable and very easy to deploy and manage.
- The WebAssembly function is secure. It is well known that even with Docker, native binary applications can break containers. Since your application may depend on many third-party libraries, the risk of dangerous code in your dependencies is real. WebAssembly has a competency-based security model that provides better runtime protection for your code.
- Although WebAssembly is compatible with a variety of programming languages, Rust, AssemblyScript (TypeScript), C/C++, and Go are the best languages for writing WebAssembly functions, especially Rust. Rust is a popular and increasingly visible programming language with a very active community. Rust allows us to write an efficient but memory-safe function.
Writing and deploying WebAssembly functions on Tencent Cloud is actually quite simple and can be done in less than an hour.
preparation
First, sign up for a Tencent cloud account. For most development and personal projects, development work can be done within the free amount.
Make sure you have the Rust and SSvmup toolchains installed on your local development computer or Docker container. The SSvmup toolchain compiles and optimizes Rust to WebAssembly bytecode. Just use the following simple command to install. You can also refer to this guide.
$curl, proto '= HTTPS' - tlsv1.2 - sSf https://sh.rustup.rs | sh $source $HOME /. Cargo/env... . $ curl https://raw.githubusercontent.com/second-state/ssvmup/master/installer/init.sh -sSf | shCopy the code
The WebAssembly functions are executed in the Second State virtual machine, SSVM. SSVM is a high-performance WebAssembly virtual machine optimized for server-side use cases and applications.
Hello, world
To deploy Rust and WebAssembly functions on the Tencent Cloud, we recommend clone or fork the template repo on GitHub and use this template as the basis for your own application.
The Rust function on main.rs is the Serverless function that we will deploy to SCF. As you can see from the source code, it reads the input from the STDIN function and uses println! Macro sends the results to STDOUT.
use std::io::{self, Read}; use serde::Deserialize; fn main() { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).expect("Error reading from STDIN"); let obj: FaasInput = serde_json::from_str(&buffer).unwrap(); let key1 = &(obj.key1); let key2 = &(obj.key2); println! ("Hello! {}, {}", key1, key2); } #[derive(Deserialize, Debug)] struct FaasInput { key1: String, key2: String }Copy the code
The Rust Main () function uses the Serde library to parse a JSON string from STDIN.
JSON looks like this. We wrote the function this way because SCF uses the built-in Hello World JSON template to test the deployed function.
{
"key1": "test value 1",
"key2": "test value 2"
}
Copy the code
The key1 and key2 function extracts the values and prints the following Hello message to STDOUT.
Hello! test value 1, test value 2
Copy the code
However, how is the Web request for this function converted to STDIN? How do I convert a function response in STDOUT into an HTTP response?
This is done using the SCF Custom Runtime infrastructure and bootstrap program in our template.
As you can see, the bootstrap is just a bash shell program that constantly polls SCF for incoming requests. It converts the incoming request to STDIN and calls the WebAssembly function through SSVM. It then accepts the STDOUT output and sends it to SCF as a response to the function.
If you use our template, you don’t need to modify the bootstrap.
You can now use SSVMup to build Rust functions into WebAssembly bytecode and then package the ZIP file for deployment on the Tencent Cloud SCF Custom Runtime.
$ ssvmup build ... . $ cp pkg/hello_bg.wasm cloud/ $ cd cloud $ zip hello.zip *Copy the code
Follow the instructions and screenshots to deploy and test the hello.zip file above. You have now successfully deployed a WebAssembly Serverless function!
Next, let’s create a valuable Web service using the Rust function.
Machine learning as a service
In this example, we chose a computation-intensive machine learning task to demonstrate the performance of the Rust WebAssembly function.
The serverless function takes a comma-separated numeric input string that represents a set of points on a two-dimensional plane. The input data format is X1,y1,x2,y2…
The function analyzes the data and calculates two eigenvectors indicating the direction of the maximum variance in the data. Eigenvectors provide data scientists with clues about the underlying factors driving changes in data. This is known as principal component analysis.
Our function creates an SVG graph and draws the input data points and the feature vectors computed above on the graph. The function finally returns the SVG diagram as XML text.
To get started, you can clone or fork the repo. The project is in the TencentCloud/SSVM/PCA folder. Or you can copy Cargo. Toml and SRC /* to the hello World template above. If you choose the latter, make sure you modify Cargo. Toml to point it to the correct source folder of the Rust machine Learning library.
The details of Rust source code generated by PCA or SVG are not explored in this tutorial because they involve a lot of computational code. If you’re interested, check out more resources at the end of this article.
Follow the same steps as in the Hello World example. Use SSVMup to build a PCA.zip package and deploy it to the Tencent Cloud SCF Custom Runtime.
Next, we associate the deployed function with the Web API gateway so that it can be invoked from a Web HTTP or HTTPS request. Do this in the trigger Administration TAB of SCF’s Web console. Check out the tutorial and screenshots here.
The API console converts the HTTP request into JSON input to the Serverless function. For example, here is an HTTP POST request to the API gateway URL. We put the comma-separated data points from the iris.csv file in the POST body.
$ curl -d @iris.csv -X POST https://service-m9pxktbc-1302315972.hk.apigw.tencentcs.com/release/PCASVG
Copy the code
The API gateway passes the following JSON to the STDIN of the Rust function. The POST Body is now the body property in JSON.
{" body ":" 3.5, 0.2, 3,0.2,..." , "headerParameters": {}, "headers": { "accept": "/", "content-length": "11", "content-type": "application/x-www-form-urlencoded", "host": "service-aj0plx8u-1302315972.hk.apigw.tencentcs.com", "user-agent": "Curl / 7.54.0", "x - anonymous - consumer" : "true", "x - API - requestid" : "e3123014742e7dd79f0652968bf1f62e", "x - b3 - traceid" : "e3123014742e7dd79f0652968bf1f62e", "x-qualifier": "$DEFAULT" }, "httpMethod": "POST", "path": "/my_hk", "pathParameters": {}, "queryString": {}, "queryStringParameters": {}, "requestContext": { "httpMethod": "ANY", "identity" : {}, "path" : "/ my_hk", "serviceId" : "the service - aj0plx8u", "sourceIp" : "136.49.211.114", "stage" : "release" } }Copy the code
The Rust function parses the data in the body, performs PCA, and generates AN SVG graph. It prints the SVG content to STDOUT, which is retrieved by the API gateway and sent back as an HTTP response.
To use this API gateway URL in AJAX requests, Tencent CSA must also be configured to accept CORS Web requests. Check out the guide for how to do this.
The following HTML JavaScript example shows how to use the Serverless function in a web page.
It gets CSV data from the Textarea field with ID csv_data, makes an AJAX HTTP POST request to the Serverless function, and then puts the return value (an SVG graph) into an HTML element with ID SVg_img. Click here to see the demo.
$.ajax({
method: "POST",
url: "https://service-m9pxktbc-1302315972.hk.apigw.tencentcs.com/release/PCASVG",
data: $('#csv_data').val(),
dataType: "text"
}).done(function(data) {
$('#svg_img').html(data);
})
Copy the code
(Running Web application)
The following
Tencent SCF Custom Runtime is a very powerful serverless runtime environment. It provides a common Linux environment for any application function you want to write, and provides a standard Web interface to interact with the input and output of the function. It’s definitely worth a try.
As discussed in this article, we believe Rust and WebAssembly provide a high-performance, secure, portable, future-oriented stack for Serverless functions. Rust, WebAssembly and SCF Costum Runtime represent the future!
More resources
- Learn more about why you want to use WebAssembly on the server
- Learn more about Tencent SCF
- Learn more about machine learning algorithms
- Introduction to Rust
- Machine learning algorithms in Rust and WebAssembly
- Getting started: Using Rust in Node.js
- Learn more about the Second State WebAssembly VM, SSVM