阿里雲函式計算 VSCode 使用,及部署 Docusaurus
阿新 • • 發佈:2020-11-12
- 程式碼: https://github.com/ikuokuo/start-serverless
## 使用簡介
[產品頁](https://www.aliyun.com/product/fc)開通服務。使用流程,如下:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231721.png)
新手示例,如下:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231720.png)
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231719.png)
## 建立函式
阿里雲提供瞭如下幾種方式建立函式:
* [使用控制檯建立函式](https://help.aliyun.com/document_detail/51783.html)
* [使用Funcraft建立函式](https://help.aliyun.com/document_detail/155100.html)
* [使用VSCode外掛建立函式](https://help.aliyun.com/document_detail/155679.html)
以下為使用 VSCode 外掛建立函式。
### 前提準備
#### 安裝軟體
* [Visual Studio Code](https://code.visualstudio.com/)
* [Docker](https://docs.docker.com/get-docker/)
#### 安裝外掛
外掛: [Aliyun Serverless VSCode Extension](https://github.com/alibaba/serverless-vscode/)
VSCode 外掛市場搜尋 "aliyun" ,安裝:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231726.png)
左側邊欄,開啟外掛,繫結賬戶:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231727.png)
依次輸入 Account ID, AccessKey ID, AccessKey Secret, Account Alias 。
繫結後, "REMOTE RESOURCES" 可以看到該賬戶的雲端服務與函式列表。
### 本地建立
"LOCAL RESOURCES" 面板 "+" 建立函式:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231725.png)
輸入內容:
service name: demo
function name: test
function runtime: nodejs12
function type: NORMAL (Event Trigger)
### 本地執行
函式名稱上 "Local Run" 執行:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231723.png)
函式日誌,輸出在終端。
### 遠端部署
"LOCAL RESOURCES" 面板 "Deploy" 部署函式:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231724.png)
"REMOTE RESOURCES" 可檢視部署的函式。
或者到阿里雲[函式計算控制檯](https://fc.console.aliyun.com)檢視:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110231722.png)
## 部署 Docusaurus
### 環境準備
#### 安裝 Funcraft
Funcraft 是函式計算提供的一種命令列工具,通過該工具,您可以便捷地管理函式計算、API閘道器、日誌服務等資源。通過一個資源配置檔案 template.yml , Funcraft 即可協助您進行開發、構建、部署操作。
`npm` 安裝:
```zsh
npm install @alicloud/fun -g
```
`fun` 版本:
```zsh
❯ fun --version
3.6.18
```
`fun` 配置:
```zsh
❯ fun config
? Aliyun Account ID ***********
? Aliyun Access Key ID ***********AmpG
? Aliyun Access Key Secret ***********XZlY
? Default region name cn-shanghai
? The timeout in seconds for each SDK client invoking 60
? The maximum number of retries for each SDK client 3
? Allow to anonymously report usage statistics to improve the tool over time? Ye
s
? Use custom endpoint? No
```
### 建立應用
```zsh
npx @docusaurus/init@next init my-site classic
```
執行:
```zsh
cd my-site
yarn start
```
效果:
![](https://gitee.com/ikuokuo/pic/raw/master/pic/20201110234256.png)
### 部署應用
```zsh
❯ cd my-site
```
初始化:
```zsh
❯ fun init
? Select a template to init http-trigger-nodejs12
? You've created /users/ikuokuo/start-serverless/my-site before. Is it okay to override it? Yes
Start rendering template...
+ /users/ikuokuo/start-serverless/my-site
+ /users/ikuokuo/start-serverless/my-site/.funignore
+ /users/ikuokuo/start-serverless/my-site/index.js
+ /users/ikuokuo/start-serverless/my-site/template.yml
finish rendering template.
```
刪除 `index.js`。
```zsh
rm index.js
```
修改 `template.yml`:
```yml
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
my-site:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'helloworld'
my-site:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: custom
CodeUri: './'
MemorySize: 1024
InstanceConcurrency: 5
Timeout: 120
Events:
httpTrigger:
Type: HTTP
Properties:
AuthType: ANONYMOUS
Methods: ['GET', 'POST', 'PUT']
Domain:
Type: Aliyun::Serverless::CustomDomain
Properties:
DomainName: Auto
Protocol: HTTP
RouteConfig:
Routes:
"/*":
ServiceName: my-site
FunctionName: my-site
```
建立 `bootstrap`:
```bash
❯