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 template.yml
file.
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.
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:
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'
...
The fun install
command runs the tasks in the fun.yml
:
.so
file that puppeteer depends on.chrome headless
binary file that puppeteer depends on to OSS.Install the npm
dependency.
To deploy the project, use the fun deploy
command:
$ fun deploy
using region: cn-shanghai
using accountId: ***********4733
using accessKeyId: ***********KbBS
using timeout: 60
Waiting 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'
99 posts | 7 followers
FollowAlibaba Cloud Serverless - September 29, 2022
Alibaba Clouder - January 20, 2021
Alibaba Cloud Serverless - June 28, 2022
Ryan Zhang - September 18, 2018
Alibaba Developer - February 1, 2021
Alibaba Cloud Serverless - May 9, 2020
99 posts | 7 followers
FollowAlibaba 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 MoreAlibaba Cloud offers an accelerated global networking solution that makes distance learning just the same as in-class teaching.
Learn MoreConnect your business globally with our stable network anytime anywhere.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreMore Posts by Alibaba Cloud Serverless