Posts
Serverless offline 開發設定 - 基礎篇
說明
為了加速開發的效率,開發過程中不會想要每次都佈署到AWS做測試或開發。Serverless offline套件可以解決這件事情。透過模擬 AWS λ and API Gateway ,開發者在本機端就可以run一個的server去模擬再AWS服務的情況。
前置作業
需要事先安裝過 serverless package sudo npm install serverless -g 安裝serverless offline套件 npm install serverless-offline --save-dev
使用方法
在原本專案的serverless.yml上添加程式碼
plugins plugins: - serverless-offline server conf 將server在 3000 PORT 開啟; 開放所有的 IP 都可以存取
custom: serverless-offline: host: "0.0.0.0" port: 3000 在Terminal上啟動 serverless offline server serverless offline start 接著就可以用各種工具去發出request做開發測試
:::danger
執行API request出現錯誤 { "statusCode": 400, "error": "Bad Request", "message": "Invalid cookie value" } 經過測試,如果將IP綁在127.0.0.1或是沒有設定(系統預設為localhost)的話會出現access error,這個問題只要將 IP 綁在 0.
Posts
在Local machine開發AWS serverless project
AWS dev project 說明:這個 Project 主要是拿來研究 AWS serverless 的開發規劃和未來建構 Project 的基礎參考。主要會用到 AWS Resource 有 API-gateway, Lambda, RDS, Congnito Services。 Install 安裝 serverless package npm install -g serverless 安裝開發所需其他的套件 npm install Dev run Project 開發時,可在本機上做 Debug,確定Lambda的function沒有邏輯錯誤再佈署上去AWS就可以。Dev server開啟時會建立在 port 3000 上,請用自己主機 ip 連,不要用 127.0.0.1 或 localhost 連線,否則會出現錯誤。 npm run dev Deploy to AWS 將自己的程式碼透過serverless package上傳到 CloudFormation後佈署所需資源。 npm run deploy 為 API-gateway建立一個 Usage Plan aws apigateway create-usage-plan --name dev-file-sharing-plan --api-stages apiId={{API_ID}},stage=dev --region ap-northeast-1 為 Usage Plan 綁定一組 API-key aws apigateway create-usage-plan-key --usage-plan-id {{PLAN_ID}} --key-id {{KEY_ID}} --key-type "API_KEY" Migrations 建立一個 Migration 檔案 (若變更 Schema 則需要新增一個 Migration 檔案) db-migrate create <migrate_file> --config .
Posts
部屬Web application到K8S環境
Deploy first API server on K8S prepare server image docker pu image to registery
deploy pod on K8S 執行create pod指令時出現以下錯誤,代表目前K8S架構裡只有Master沒有Node
0/1 nodes are available: 1 node(s) had taints that the pod didn't tolerate 執行 kubectl get pods -o wide 可以檢查目前pod的運行狀態
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE my-pod 1/1 Running 0 19m 10.244.1.2 ubuntu <none> 驗證server是否已經成功的運行,可以用建立另一個暫時的pod,利用curl指令去取得server的資訊 kubectl run -i --tty alpine --image=alpine --restart=Never -- sh apk add curl curl -v http://10.
Posts
Dev issue: Lambda reuse
當系統透過 APIGateway 呼叫 Lambda 執行時,我以為Lambda會全部重新執行打包好的程式。所以開發時,我們將 database connection 物件當成 Global 物件使用,認為每次重新呼叫 Lambda 都會全部重新生成物件。因為這個觀念而導致系統錯誤,當 Project 的物件被 require 後或變成 global 參數,再次執行 Lambda container 會出現 reuse 特性,將不會再被重新執行而生成新物件,以下是一個範例。 DB 的 client 為 global 物件
const pg = require('pg'); const client = new pg.Client('postgres://myrds:5432/dbname'); client.connect(); exports.handler = (event, context, cb) => { const {test_print} = require('./example_module'); test_print(); client.query('SELECT * FROM users WHERE ', (err, users) => { // Do stuff with users cb(null); // Finish the function cleanly }); }; 上面的範例,因為 client 為 global物件,所以如果在程式裡面有 disconnect client 的狀況下,再次執行 Lambda 就會出現錯誤,因為 client 物件沒被重新 connect。 Global 程式不會每次都被執行
Posts
部屬Web application到K8S環境
Deploy first API server on K8S prepare server image docker pu image to registery
deploy pod on K8S 執行create pod指令時出現以下錯誤,代表目前K8S架構裡只有Master沒有Node
0/1 nodes are available: 1 node(s) had taints that the pod didn't tolerate 執行 kubectl get pods -o wide 可以檢查目前pod的運行狀態
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE my-pod 1/1 Running 0 19m 10.244.1.2 ubuntu <none> 驗證server是否已經成功的運行,可以用建立另一個暫時的pod,利用curl指令去取得server的資訊 kubectl run -i --tty alpine --image=alpine --restart=Never -- sh apk add curl curl -v http://10.