出遊之前要規劃路線,在開發之前當然也要先想好目標。
先定義好要做的功能,才不會在實作的時候想到哪做到哪,最後卻發現方向跑偏、流程混亂。
雖然不用立刻就計較每個細節,但至少要有先訂好明確的目標、核心功能,甚至可以先區分好最小可行目標,其他的想法再另外安排優先順序,這樣才能確保專案能一步一步往前推進。
核心需求:抽牌
資料結構設計
{
"name": "愚者 — The Fool",
"upright_meta": "正位關鍵詞",
"reversed_meta": "逆為關鍵詞"
}
功能設計
結果輸出:將抽出的牌整理成易讀的格式。
系統架構
專案結構
draw-card/ # 專案根目錄
├── main.py # 主程式碼
├── service/
│ ├── cards.py # 塔羅牌資料
│ ├── deck.py # 洗牌 & 抽牌
├── requirements.txt # 套件
參考官方文件:Working with .zip file archives for Python Lambda functions
You can declare simple functions written in Python or Node.js inline in an AWS CloudFormation template.
Because of these limitations, declaring functions inline is best suited for very simple code that does not change frequently.
Lambda 適用於功能單純、邏輯簡單的實作,很符合這次的練習情境。
Your .zip file should have a flat directory structure, with your function's handler code and all your dependency folders installed at the root as follows.
zip 檔案應該具有扁平的目錄結構,將函式處理程式碼和所有依賴資料夾都安裝在根目錄。
Lambda expects your source code and its dependencies all to be at the root of the .zip file. If the .py file containing your function’s handler code is not at the root of your .zip file, Lambda will not be able to run your code.
Lambda 要求原始碼和依賴都位於 .zip 檔案的根目錄。
如果處理函式 .py 檔案如果不是放在 .zip 的根目錄,Lambda 就跑不起來。
draw-card/ # 專案根目錄
├── main.py # 主程式碼
├── cards.py # 塔羅牌資料
├── deck.py # 洗牌 & 抽牌
├── requirements.txt # 套件
Lambda 可以使用 .zip 檔案部署,也支援 容器映像檔(Container Image)。
考慮到這次要實作的功能單純,只做串接而不是一個完整的服務,所以選用 .zip 的方式,因此優先參考 .zip 的部署文件。
到這裡,最小可行的需求、資料結構、功能設計和執行環境都已經構思完成。
如果一開始就盲目動手寫,不但可能寫到一半發現方向跑偏,還可能因為元件選擇錯誤,在部署和測試時遇到困境。
光有幹勁沒有規劃,當系統越是複雜,就越容易在多個功能和資料流之間迷路。
想要紮實地將專案往前推進,勢必需要先有謀略再付諸行動。
AWS 上那麼多運算服務,為麼選 Lambda?
最主要的原因,當然還是...