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
| Perintah | Deskripsi |
|---|---|
sudo s | Mulai wizard interaktif proyek (Metode 1) |
sudo s init <template> | Inisialisasi proyek dari templat tertentu (Metode 2) |
sudo s build | Bangun proyek secara lokal |
sudo s local start | Jalankan server lokal untuk fungsi HTTP |
sudo s local invoke | Panggil fungsi event secara lokal |
sudo s deploy -y | Terapkan proyek ke Function Compute |
sudo s invoke -e '<payload>' | Panggil fungsi yang telah diterapkan secara remote |
sudo s remove service | Hapus layanan dan semua resource terkait |
Prasyarat
Sebelum memulai, pastikan Anda telah:
Mengaktifkan layanan Function Compute. Untuk informasi selengkapnya, lihat Aktifkan Function Compute
Pilih metode
| Metode | Perintah | Paling cocok untuk | Apa yang Anda dapatkan |
|---|---|---|---|
| Metode 1: Penyiapan interaktif | s | Pengguna baru | Wizard terpandu untuk pembuatan proyek dan penerapan satu langkah |
| Metode 2: Alur kerja langkah demi langkah | s init | Kontrol granular | Build, debug lokal, panggil remote, dan penghapusan resource |
Metode 1: Penyiapan interaktif dengan s
Jalankan s untuk memulai wizard interaktif:
sudo sWizard 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: 60Pilihan utama selama alur interaktif:
Cloud vendor: Pilih Alibaba Cloud Serverless.
Template: Pilih tipe fungsi dan runtime, misalnya [Event] Node.js 14.
Project name: Terima nilai default atau masukkan nama kustom.
Credential alias: Pilih alias yang dikonfigurasi saat penyiapan Serverless Devs.
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-nodejs14Contoh ini menerapkan fungsi HTTP dalam Node.js 14. Gantihttpdengan tipe pemicu lain sepertieventuntuk mengubah tipe fungsi. Gantinodejs14dengan 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? NoSetelah inisialisasi, direktori proyek berisi file-file berikut:
| File | Deskripsi |
|---|---|
s.yaml | Mendefinisikan resource Function Compute. Untuk informasi selengkapnya, lihat Sintaks YAML dan pengelolaan izin. |
s_en.yaml | Berisi 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.md | Mendokumentasikan dependensi, modul, dan konfigurasi proyek. |
Masuk ke direktori proyek
cd start-fc-http-nodejs14Bangun proyek (opsional)
Jalankan s build untuk mengompilasi dan mengemas proyek secara lokal:
sudo s buildPerintah 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 startContoh 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 invokeHasil langsung dikembalikan setelah perintah dijalankan.
Terapkan ke Function Compute
sudo s deploy -yContoh 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: httpTriggerPanggil 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: invokeAnda 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 serviceContoh 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: removePerintah ini menghapus pemicu, fungsi, dan layanan secara berurutan, serta membersihkan resource layanan NAS terkait.