All Products
Search
Document Center

Function Compute:Kelola resource fungsi menggunakan Serverless Devs

Last Updated:Feb 28, 2026

Serverless Devs adalah alat open source untuk mengembangkan, membangun, menguji, dan menerapkan proyek serverless sepanjang siklus hidupnya. Topik ini menjelaskan cara menggunakan Serverless Devs untuk melakukan inisialisasi dan penerapan proyek Function Compute. Semua contoh menggunakan Node.js 14.

Referensi cepat perintah

PerintahDeskripsi
sudo sMulai wizard interaktif proyek (Metode 1)
sudo s init <template>Inisialisasi proyek dari templat tertentu (Metode 2)
sudo s buildBangun proyek secara lokal
sudo s local startJalankan server lokal untuk fungsi HTTP
sudo s local invokePanggil fungsi event secara lokal
sudo s deploy -yTerapkan proyek ke Function Compute
sudo s invoke -e '<payload>'Panggil fungsi yang telah diterapkan secara remote
sudo s remove serviceHapus layanan dan semua resource terkait

Prasyarat

Sebelum memulai, pastikan Anda telah:

Pilih metode

MetodePerintahPaling cocok untukApa yang Anda dapatkan
Metode 1: Penyiapan interaktifsPengguna baruWizard terpandu untuk pembuatan proyek dan penerapan satu langkah
Metode 2: Alur kerja langkah demi langkahs initKontrol granularBuild, debug lokal, panggil remote, dan penghapusan resource

Metode 1: Penyiapan interaktif dengan s

Jalankan s untuk memulai wizard interaktif:

sudo s

Wizard akan meminta Anda mengonfigurasi proyek. Contoh berikut menunjukkan sesi khas:

 No Serverless-Devs project is currently detected. Do you want to create a new project? Yes

 Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md

 Hello Serverless for Cloud Vendors Alibaba Cloud Serverless
 Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]
 Which template do you like? [Event] Node.js 14

 Create application command: [s init devsapp/start-fc-event-nodejs14]

 Please input your project name (init dir) start-fc-event-nodejs14
 file decompression completed

     ____  _     _ ___  _ _     _        _____ ____
    /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
    | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
    | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
    \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
 please select credential alias default

    Welcome to the Aliyun FC start application
     This application requires to open these services:
         FC : https://fc.console.alibabacloud.com/

     * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project.

 Thanks for using Serverless-Devs
 You could [cd /test/start-fc-event-nodejs14] and enjoy your serverless journey!
 If you need help for this example, you can use [s -h] after you enter folder.
 Document Star: https://github.com/Serverless-Devs/Serverless-Devs

 Do you want to deploy the project immediately? Yes
 ......
helloworld:
  region:   cn-hangzhou
  service:
    name: hello-world-service
  function:
    name:       event-nodejs14
    runtime:    nodejs14
    handler:    index.handler
    memorySize: 128
    timeout:    60

Pilihan utama selama alur interaktif:

  1. Cloud vendor: Pilih Alibaba Cloud Serverless.

  2. Template: Pilih tipe fungsi dan runtime, misalnya [Event] Node.js 14.

  3. Project name: Terima nilai default atau masukkan nama kustom.

  4. Credential alias: Pilih alias yang dikonfigurasi saat penyiapan Serverless Devs.

  5. Deploy immediately: Pilih Yes untuk melakukan deploy dalam satu langkah, atau No jika ingin menunda deploy.

Metode 2: Alur kerja langkah demi langkah dengan s init

Inisialisasi proyek

sudo s init devsapp/start-fc-http-nodejs14
Contoh ini menerapkan fungsi HTTP dalam Node.js 14. Ganti http dengan tipe pemicu lain seperti event untuk mengubah tipe fungsi. Ganti nodejs14 dengan identifier runtime lain untuk mengubah runtime.

Contoh output:

Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md
 Please input your project name (init dir) start-fc-http-nodejs14
 file decompression completed

     ____  _     _ ___  _ _     _        _____ ____
    /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
    | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
    | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
    \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
 please select credential alias default

    Welcome to the Aliyun FC start application
     This application requires to open these services:
         FC : https://fc.console.alibabacloud.com/

     * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project.

 Thanks for using Serverless-Devs
 You could [cd /test/start-fc-http-nodejs14] and enjoy your serverless journey!
 If you need help for this example, you can use [s -h] after you enter folder.
 Document Star: https://github.com/Serverless-Devs/Serverless-Devs

 Do you want to deploy the project immediately? No

Setelah inisialisasi, direktori proyek berisi file-file berikut:

FileDeskripsi
s.yamlMendefinisikan resource Function Compute. Untuk informasi selengkapnya, lihat Sintaks YAML dan pengelolaan izin.
s_en.yamlBerisi konfigurasi yang sama dengan s.yaml. Secara default, sistem menggunakan s.yaml. Untuk menggunakan s_en.yaml sebagai gantinya, tentukan parameter -t: s deploy -t s_en.yaml -y.
code/Berisi index.js dengan logika fungsi.
readme.mdMendokumentasikan dependensi, modul, dan konfigurasi proyek.

Masuk ke direktori proyek

cd start-fc-http-nodejs14

Bangun proyek (opsional)

Jalankan s build untuk mengompilasi dan mengemas proyek secara lokal:

sudo s build

Perintah ini menghasilkan direktori .s di root proyek yang menyimpan artefak build untuk debugging dan penerapan.

Debug secara lokal (opsional)

Setelah proses build, uji fungsi di mesin Anda sebelum menerapkannya.

Fungsi HTTP:

sudo s local start

Contoh output:

[2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - Using trigger for start: name: httpTrigger
type: http
config:
  authType: anonymous
  methods:
    - GET

[2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of hello-world-service/http-trigger-nodejs14 was registered
        url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
        methods: GET
        authType: anonymous

Tips: You can also use these commands to run or debug custom domain resources:

Start with customDomain:
* s local start auto

Debug with customDomain:
* s local start -d 3000 auto


Tips for next step
======================
* Deploy Resources: s deploy
helloworld:
  status: succeed
function compute app listening on port 7013!

Picu fungsi HTTP dengan cURL atau browser:

curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/

Atau buka URL berikut di browser:

http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/

Fungsi event:

sudo s local invoke

Hasil langsung dikembalikan setelah perintah dijalankan.

Terapkan ke Function Compute

sudo s deploy -y

Contoh output:

Checking Service, Function, Triggers (0.82s)
 Creating Service, Function, Triggers (0.6s)
 Creating custom domain (0.31s)

Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function
* Remove Trigger: s remove trigger
* Remove CustomDomain: s remove domain

helloworld:
  region:   cn-hangzhou
  service:
    name: hello-world-service
  function:
    name:       http-trigger-nodejs14
    runtime:    nodejs14
    handler:    index.handler
    memorySize: 128
    timeout:    60
  url:
    custom_domain:
      -
        domain: http://http-trigger-nodejs14.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net
  triggers:
    -
      type: http
      name: httpTrigger

Panggil fungsi secara remote (opsional)

Setelah penerapan, panggil fungsi di cloud untuk memverifikasi fungsinya:

sudo s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'

Contoh output:

Request url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
========= FC invoke Logs begin =========
FC Invoke Start RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c
load code for handler:index.handler
2022-03-04T08:39:14.770Z 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c [verbose] hello world
FC Invoke End RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c

Duration: 66.23 ms, Billed Duration: 67 ms, Memory Size: 128 MB, Max Memory Used: 42.48 MB
========= FC invoke Logs end =========

FC Invoke Result[Code: 200]:
{
  path: '//',
  queries: { key: 'value' },
  headers: {
    accept: 'application/json',
    authorization: 'FC yourAccessKeyID:yourAccessKeySecret',
    date: 'Fri, 04 Mar 2022 08:39:14 GMT',
    header_1: 'v1',
    host: '188077086902****.cn-hangzhou.fc.aliyuncs.com',
    'user-agent': 'Node.js(v14.17.4) OS(linux/x64) SDK(@alicloud/fc2@v2.5.0)',
    'x-forwarded-proto': 'http'
  },
  method: 'GET',
  requestURI: '/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14//?key=value',
  clientIP: '47.97.XX.XX',
  body: ''
}

End of method: invoke

Anda juga dapat memicu fungsi HTTP dengan cURL:

curl https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/

Hapus resource (opsional)

Saat Anda tidak lagi memerlukan resource yang telah diterapkan, hapus resource tersebut:

sudo s remove service

Contoh output:

Need to delete the resource in the cn-hangzhou area, the operation service is hello-world-service:
 ......
 Delete trigger hello-world-service/http-trigger-nodejs14/httpTrigger success.
 Delete function hello-world-service/http-trigger-nodejs14 success.
 Delete service hello-world-service success.
[2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service
[2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service
End of method: remove

Perintah ini menghapus pemicu, fungsi, dan layanan secara berurutan, serta membersihkan resource layanan NAS terkait.

Referensi