阿里雲函數計算 VSCode 使用,及部署 Docusaurus


使用簡介

產品頁開通服務。使用流程,如下:

新手示例,如下:

創建函數

阿里雲提供了如下幾種方式創建函數:

以下為使用 VSCode 插件創建函數。

前提准備

安裝軟件

安裝插件

插件: Aliyun Serverless VSCode Extension

VSCode 插件市場搜索 "aliyun" ,安裝:

左側邊欄,打開插件,綁定賬戶:

依次輸入 Account ID, AccessKey ID, AccessKey Secret, Account Alias 。

綁定后, "REMOTE RESOURCES" 可以看到該賬戶的雲端服務與函數列表。

本地創建

"LOCAL RESOURCES" 面板 "+" 創建函數:

輸入內容:

service name: demo
function name: test
function runtime: nodejs12
function type: NORMAL (Event Trigger)

本地運行

函數名稱上 "Local Run" 運行:

函數日志,輸出在終端。

遠程部署

"LOCAL RESOURCES" 面板 "Deploy" 部署函數:

"REMOTE RESOURCES" 可查看部署的函數。

或者到阿里雲函數計算控制台查看:

部署 Docusaurus

環境准備

安裝 Funcraft

Funcraft 是函數計算提供的一種命令行工具,通過該工具,您可以便捷地管理函數計算、API網關、日志服務等資源。通過一個資源配置文件 template.yml , Funcraft 即可協助您進行開發、構建、部署操作。

npm 安裝:

npm install @alicloud/fun -g

fun 版本:

❯ fun --version
3.6.18

fun 配置:

❯ 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

創建應用

npx @docusaurus/init@next init my-site classic

運行:

cd my-site
yarn start

效果:

部署應用

❯ cd my-site

初始化:

❯ 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

rm index.js

修改 template.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

❯ cat <<EOF >bootstrap
#!/usr/bin/env bash
export PORT=9000
npm run serve -- --port \$PORT
# yarn serve --port \$PORT
EOF
❯ chmod a+x bootstrap

Something is already running on port: https://github.com/facebook/docusaurus/issues/3505.
Check npx docusaurus --version >= 2.0.0-alpha.66 !

編譯:

yarn build

部署:

❯ fun deploy
using template: template.yml
using region: cn-shanghai
using accountId: ***********
using accessKeyId: ***********AmpG
using timeout: 60

Collecting your services information, in order to caculate devlopment changes...

Resources Changes(Beta version! Only FC resources changes will be displayed):

┌─────────────┬──────────────────────────────┬────────┬─────────────────────┐
│ Resource    │ ResourceType                 │ Action │ Property            │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│ my-site     │ Aliyun::Serverless::Service  │ Add    │ Description         │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│             │                              │        │ Handler             │
│             │                              │        ├─────────────────────┤
│             │                              │        │ Runtime             │
│             │                              │        ├─────────────────────┤
│             │                              │        │ CodeUri             │
│ my-site     │ Aliyun::Serverless::Function │ Add    ├─────────────────────┤
│             │                              │        │ MemorySize          │
│             │                              │        ├─────────────────────┤
│             │                              │        │ InstanceConcurrency │
│             │                              │        ├─────────────────────┤
│             │                              │        │ Timeout             │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│             │                              │        │ AuthType            │
│ httpTrigger │ HTTP                         │ Add    ├─────────────────────┤
│             │                              │        │ Methods             │
└─────────────┴──────────────────────────────┴────────┴─────────────────────┘

? Please confirm to continue. Yes
Waiting for service my-site to be deployed...
        Waiting for function my-site to be deployed...
                Waiting for packaging function my-site code...
                The function my-site has been packaged. A total of 29625 files were compressed and the final size was 37.05 MB
                Waiting for HTTP trigger httpTrigger to be deployed...
                triggerName: httpTrigger
                methods: [ 'GET', 'POST', 'PUT' ]
                trigger httpTrigger deploy success
        function my-site deploy success
service my-site deploy success

Detect 'DomainName:Auto' of custom domain 'Domain'
Request a new temporary domain ...
The assigned temporary domain is http://33146085-1117460590419785.test.functioncompute.com,expired at 2020-10-18 23:59:03, limited by 1000 per day.
Waiting for custom domain Domain to be deployed...
custom domain Domain deploy success

訪問 http://33146085-1117460590419785.test.functioncompute.com:

結語

歡迎關注 GoCoding 公眾號,分享日常 Coding 中實用的小技巧、小知識!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM