By Yi Xian
This tutorial discusses how you can develop Function Compute with a web page snipping service. But before we get down into it, let’s first take a look at several important concepts mentioned in this article.
First, there’s Alibaba Cloud’s Function Compute, which is an event-driven service that allows users to write and upload code without having to manage server health or consider some other factors. Function Compute prepares and auto scales to the appropriate amount of computing resources necessary to run user code. The user only pays for the resources required to run their code.
Next, there’s Fun, which is a deployment tool for serverless applications. It helps you manage resources, such as Function Compute, API Gateway, and Log Service. You can use Fun to develop, build, and deploy resources by describing specified resources in the
And there’s also fun install, which is a sub-command of the Fun tool for installing pip and apt dependencies. It provides two forms, command line interface and
fun.yml description file.
Note: The techniques described in this article are applicable to Fun 2.9.3 or later.
This tutorial discusses how to develop the web page snipping service. The project outlined in this tutorial is developed on macOS but it also involves platform-independent tools, which are compatible with Linux and Windows. Before proceeding to this example, make sure that Docker, Fun, and Fcli are correctly installed, updated to the latest version and properly configured. You can download, update, and configure these tools with the instructions and resources on the pages linked here.
Fun and Fcli depend on Docker to simulate the local environment. MacOS users can use homebrew to install these tools:
brew cask install docker
brew tap vangie/formula
brew install fun
brew install fcli
Windows and Linux users can refer to Installation to learn how to install these tools. After the installation, don’t forget to first run
fun config to initialize the configuration.
Develop the Web Page Snipping Service
You can use the
fun init subcommand to easily initialize a local project using this template.
$ fun init vangie/puppeteer-example
? Please input oss bucket to upload chrome shell? chrome-headless
? Please select a region? cn-hangzhou
? Please input oss accessKeyId for upload? xxxxxxxxxxxKbBS
? Please input oss accessKeySecret for upload? xxxxxxxxxxxx5ZgM
The preceding code will prompt you to:
- Enter an OSS Bucket. Note that this OSS Bucket must be globally unique. The chrome-headless in the preceding sample is already in use. You need to enter a new name or use a Bucket that has been created (make sure that the region of the created Bucket is consistent).
- Select an OSS region. Make sure that the region is the same as the region where Function Compute is deployed.
- Enter a key with write access to OSS.
Next, you’ll want to install dependencies using the
fun install command:
$ fun install
skip pulling image aliyunfc/runtime-nodejs8:build-1.2.0...
Task => [UNNAMED]
=> apt-get update (if need)
=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp libnss3
=> bash -c 'for f in $(ls /code/.fun/tmp/archives/*.deb); do dpkg -x $f /code/.fun/root; done;'
=> bash -c 'rm -rf /code/.fun/tmp/archives'
Task => [UNNAMED]
=> bash -c 'curl -L https://github.com/muxiangqiu/puppeteer-fc-starter-kit/raw/master/chrome/headless_shell.tar.gz --output headless_shell.tar.gz'
fun install command runs the tasks in the
- Install the
.sofile that puppeteer depends on.
- Upload the
chrome headlessbinary file that puppeteer depends on to OSS.
To deploy the project, use the
fun deploy command:
$ fun deploy
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********KbBS
using timeout: 60Waiting for service puppeteer to be deployed...
Waiting for function html2png to be deployed...
Waiting for packaging function html2png code...
package function html2png code done
function html2png deploy success
service puppeteer deploy success
Last, run the project.
$ fcli function invoke -s puppeteer -f html2png
The screenshot has been uploaded to http://chrome-headless.oss-cn-shanghai.aliyuncs.com/screenshot.png
Open the preceding link returned and you can see that the screenshot taken is a full-screen scrolling picture. Only a part of the screenshot is provided due to the space limit:
To change a URL, you can use the following command format:
fcli function invoke -s puppeteer -f html2png --event-str 'http://www.alibabacloud.com'
Then, to debug your code locally, you can use the following command:
fun local invoke html2png <<<'http://www.alibaba.com'