Serverless Devs adalah alat open-source untuk pengembang aplikasi serverless. Alat ini menyediakan manajemen siklus hidup penuh untuk proyek Anda dan memungkinkan Anda membuat, mendeploy, serta menginvoke fungsi secara cepat. Topik ini menggunakan fungsi dengan runtime Python sebagai contoh untuk menjelaskan cara mengelola fungsi menggunakan Serverless Devs.
Prosedur
Langkah 1: Instal Serverless Devs
Anda dapat menginstal Serverless Devs melalui antarmuka baris perintah, mengunduh file biner, atau menjalankan skrip. Topik ini menggunakan instalasi melalui tool manajemen paket npm sebagai contoh.
Instal Node.js dan tool manajemen paket npm.
Untuk petunjuk instalasi, lihat website resmi Node.js.
CatatanNode.js harus versi 14 atau lebih baru. Setelah instalasi, Anda dapat menjalankan perintah
node --versionuntuk memeriksa versi Node.js.Jalankan perintah berikut untuk menginstal Serverless Devs.
sudo npm install @serverless-devs/s -gJalankan perintah berikut untuk memverifikasi instalasi.
sudo s -vBerikut ini contoh output dari instalasi yang berhasil.
@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4
Langkah 2: Konfigurasikan Serverless Devs
Dapatkan kunci Alibaba Cloud Anda.
Saat membuat AccessKey, pastikan Anda menyimpan Rahasia AccessKey tersebut. Untuk informasi lebih lanjut, lihat Create an AccessKey.
CatatanID AccessKey dan Rahasia AccessKey Anda merupakan kredensial untuk mengakses API Alibaba Cloud. Kunci-kunci ini memberikan izin penuh ke akun Anda. Anda harus menjaganya agar tetap aman.
Ikuti langkah-langkah terpandu untuk mengonfigurasi kunci.
Jalankan perintah berikut untuk memilih penyedia cloud.
sudo s config addOutput:
> Alibaba Cloud (alibaba) AWS (aws) Azure (azure) Baidu Cloud (baidu) Google Cloud (google) Huawei Cloud (huawei) Tencent Cloud (tencent) (Geser ke atas dan ke bawah untuk menampilkan lebih banyak pilihan)Pilih penyedia cloud yang diinginkan dan tekan Enter. Topik ini menggunakan
Alibaba Cloud (alibaba)sebagai contoh.Ikuti prompt interaktif untuk mengonfigurasi informasi kunci Anda.
Please select a provider: Alibaba Cloud (alibaba) Refer to the document for alibaba key: http://config.devsapp.net/account/alibaba AccountID 188077086902**** AccessKeyID yourAccessKeyID AccessKeySecret yourAccessKeySecret Please create alias for key pair. If not, please enter to skip default Alias: default AccountID: 188077086902**** AccessKeyID: yourAccessKeyID AccessKeySecret: yourAccessKeySecret Configuration successful
Langkah 3: Inisialisasi fungsi contoh
Jalankan perintah berikut untuk membuat proyek Hello World dalam Python.
sudo s init start-fc3-pythonOutput-nya sebagai berikut:
More applications: https://registry.serverless-devs.com ? Please input your project name (init dir) start-fc3-python Downloading[/v3/packages/start-fc3-python/zipball/0.0.8]... Download start-fc3-python successfully ... Region where the application is created ? Region cn-huhehaote Must contain only letters, digits, underscores (_), and hyphens (-). Cannot start with a digit or a hyphen. The length must be 1 to 128 characters. ? Function name start-python-8i32 Region where the application is created ? python runtime python3.9 ? please select credential alias default * Before using, please check whether the actions command in Yaml file is available * Carefully reading the notes in s.yaml is helpful for the use of the tool * If need help in the use process, please apply to join the Dingtalk Group: 33947367 Thanks for using Serverless-Devs You could [cd /start-fc3-python] 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 More applications: https://registry.serverless-devs.comSetelah proyek diinisialisasi, folder start-fc3-python dibuat di direktori saat ini. Folder ini berisi file-file berikut:
s.yaml: File definisi resource untuk Function Compute. File ini ditulis dalam format YAML.
code: Folder code berisi file index.py. Anda dapat mengedit logika kode fungsi di file ini.
readme.md: File manifest, yang mendefinisikan modul dan informasi konfigurasi yang diperlukan untuk proyek.
Jalankan perintah berikut untuk masuk ke folder proyek.
cd start-fc3-python
Langkah 4: Sebarkan fungsi
Jalankan perintah berikut untuk mendeploy fungsi ke Function Compute. Metadata fungsi, seperti nama, runtime, dan memori, didefinisikan dalam file s.yaml.
sudo s deployOutput-nya sebagai berikut:
Steps for [deploy] of [hello-world-app]
====================
✔ [hello_world] completed (3.1s)
Result for [deploy] of [hello-world-app]
====================
region: cn-huhehaote
description: hello world by serverless devs
functionName: start-python-8i32
handler: index.handler
internetAccess: true
memorySize: 128
role:
runtime: python3.9
timeout: 30
A complete log of this run can be found in: /Users/x1/.s/logs/0229164849Langkah 5: Panggil fungsi
Jalankan perintah berikut untuk menginvoke fungsi.
sudo s invoke -e "test"Output-nya sebagai berikut:
Steps for [invoke] of [hello-world-app]
====================
========= FC invoke Logs begin =========
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: 1-65e045b1-123e1745-79e146cd8b01
2024-02-29T08:52:01.317Z 1-65e045b1-123e1745-79e146cd8b01 [INFO] b'test'
FC Invoke End RequestId: 1-65e045b1-123e1745-79e146cd8b01
Duration: 2.38 ms, Billed Duration: 3 ms, Memory Size: 128 MB, Max Memory Used: 8.78 MB
========= FC invoke Logs end =========
Invoke instanceId: c-65e045b1-12f566fe-ca21d8fe46f1
Code Checksum: 3418420950487476908
Qualifier: LATEST
RequestId: 1-65e045b1-123e1745-79e146cd8b01
Invoke Result:
test
✔ [hello_world] completed (0.46s)
A complete log of this run can be found in: /Users/x1/.s/logs/0229165200Langkah 6: Hapus fungsi (Opsional)
Jika Anda tidak lagi memerlukan fungsi tersebut, Anda dapat menjalankan perintah berikut untuk menghapusnya.
sudo s removeOutput-nya sebagai berikut:
Steps for [remove] of [hello-world-app]
====================
Remove function: cn-huhehaote/start-python-8i32
? Are you sure you want to delete the resources listed above yes
✔ [hello_world] completed (11.67s)
A complete log of this run can be found in: /Users/x1/.s/logs/0229165500