Serverless Devs açık kaynaklı bir sunucusuz CLI aracıdır. Bu aracı kullanarak bir Python işlevini Function Compute hizmetine dağıtabilir, işlevi çağırabilir ve kaynakları temizleyebilirsiniz.
Ön koşullar
Aşağıdakilere sahip olduğunuzdan emin olun:
Bir AccessKey çifti içeren bir Alibaba Cloud hesabı. Oluşturma sırasında AccessKey secret değerini kaydedin. AccessKey çifti oluşturun
Node.js 14 veya daha yeni bir sürüm. Node.js web sitesinden indirin.
Node.js sürümünüzü doğrulayın:
node --version
1. Adım: Serverless Devs yükleyin
Serverless Devs; npm, ikili dosya indirme veya komut dosyası kullanılarak yüklenebilir. Bu kılavuzda npm yöntemi kullanılmaktadır.
sudo npm install @serverless-devs/s -g
Yüklemeyi doğrulayın:
sudo s -v
Beklenen çıktı:
@serverless-devs/s: 3.0.1, s-home: /root/.s, linux-x64, node-v14.17.4
2. Adım: AccessKey çiftinizi yapılandırın
AccessKey ID ve AccessKey secret değerleriniz Alibaba Cloud hesabınıza tam erişim sağlar. Bu bilgileri güvenli bir yerde saklayın.
-
Yapılandırma sihirbazını başlatın:
sudo s config addCLI, desteklenen bulut sağlayıcılarını listeler:
> Alibaba Cloud (alibaba) AWS (aws) Azure (azure) Baidu Cloud (baidu) Google Cloud (google) Huawei Cloud (huawei) Tencent Cloud (tencent) (Move up and down to reveal more choices) Alibaba Cloud (alibaba) seçeneğini seçin ve Enter tuşuna basın.
-
İstendiğinde AccessKey çiftinizi girin.
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
3. Adım: Örnek bir işlev başlatın
-
Python dilinde bir Hello World projesi oluşturun:
sudo s init start-fc3-pythonBir bölge, işlev adı ve çalışma zamanı seçmek için istemleri takip edin:
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.comBu işlem, aşağıdaki dosyaları içeren bir start-fc3-python klasörü oluşturur:
Dosya Açıklama s.yaml Function Compute için YAML formatındaki kaynak tanımı. code/index.py İşlev kodu giriş noktası readme.md Proje modüllerini ve yapılandırmayı tanımlar -
Proje klasörüne gidin:
cd start-fc3-python
4. Adım: İşlevi dağıtın
İşlevi dağıtın. Ad, çalışma zamanı ve bellek gibi meta veriler s.yaml dosyasında tanımlanır.
sudo s deploy
Beklenen çıktı:
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/0229164849
Çıktının işlev adınızı ve bölgenizi gösterdiğini doğrulayın.
5. Adım: İşlevi çağırın
İşleve bir test olayı gönderin:
sudo s invoke -e "test"
Beklenen çıktı:
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/0229165200
Invoke Result alanında test değeri görünür; bu durum işlevin girdiyi başarıyla işlediğini onaylar.
6. Adım: Temizleme (isteğe bağlı)
İşleve artık ihtiyacınız olmadığında silin:
sudo s remove
Silme işlemini onaylamak için istendiğinde yes seçeneğini seçin.
Beklenen çıktı:
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