Mô phỏng AWS Lambda & API Gateway bằng Serverless Offline
5th Oct 2022Khi phát triển ứng dùng bằng AWS Lambda không phải lúc nào chúng ta cũng có thể phát triển trực tiếp trên AWS được. Do đó việc giả lập môi trường AWS để có thể chạy được Lambda và API Gateway là cần thiết. Nó không chỉ giúp chúng ta có thể học mà còn giúp cho quá trình phát triển nhanh hơn. Trong bài viết này tôi sẽ hướng dẫn các bạn giả lập AWS Lambda và API Gateway bằng Serverless Offline
Các công cụ cần thiết
Trước tiên bạn cần cài đặt các tool cần thiết, bạn có thể tham khảo hướng dẫn cài đặt trong các bài viết sau:
- node
- yarn
- python
- serverless
Bạn có thể dùng lệnh sau để cài serverless
hieunv@HieuNV lambda % yarn global add serverless yarn global v1.22.0 [1/4] ? Resolving packages... [2/4] ? Fetching packages... [3/4] ? Linking dependencies... [4/4] ? Building fresh packages... success Installed "[email protected]" with binaries: - serverless - slss - sls ✨ Done in 14.23s.
Tạo một project mới
Tạo project với yarn
hieunv@HieuNV hieunv % mkdir lambda hieunv@HieuNV hieunv % cd lambda hieunv@HieuNV lambda % yarn init yarn init v1.22.0 question name (lambda): question version (1.0.0): question description: question entry point (index.js): question repository url: question author: question license (MIT): question private: success Saved package.json ✨ Done in 3.53s.
Cài đặt serverless-offline
hieunv@HieuNV lambda % yarn add serverless-offline -D Cài đặt serverless-python-requirements để viết lambda handler bằng python hieunv@HieuNV lambda % yarn add serverless-python-requirements -D
Cấu hình serverless.yml
serverless.yml
service: lambda frameworkVersion: '>=1.1.0 <2.0.0' provider: name: aws runtime: python3.7 custom: serverless-offline: port: 4000 plugins: - serverless-offline - serverless-python-requirements
Cấu hình lambda handler đầu tiên trong serverless.yml
Chúng ta tạo một Rest API sử dụng lambda bằng cách thêm đoạn sau vào file serverless.yml
functions: test: handler: src.api.test.lambda_handler events: - http: method: get path: api/test cors: true
Ở đây chúng ta tạo ra một Rest API với phướng thức GET và path /api/test. Các bạn nhìn thấy handler: src.api.test.lambda_handler đúng không. Đây là cấu hình hàm lamda sẽ được gọi bởi API Gateway
Viết code cho lambda handler
src/api/test.py
import json def lambda_handler(event, context): headers = {"Access-Control-Allow-Origin": "*", "Accept": "application/json"} return { "statusCode": 200, "headers": headers, "body": json.dumps({"status": "success", "data": {}}), }
Tạo script để run server
Thêm đoạn sau vào package.json
"scripts": { "start": "sls offline start" },
Giờ thì chạy thôi nào các thanh niên
hieunv@HieuNV lambda % yarn start yarn run v1.22.0 $ sls offline start Serverless: Starting Offline: dev/us-east-1. Serverless: Routes for test: Serverless: GET /api/test Serverless: POST /{apiVersion}/functions/lambda-dev-test/invocations Serverless: Offline [HTTP] listening on http://localhost:4000 Serverless: Enter "rp" to replay the last request
Dùng Postman để call api vừa tạo nhé (Xem hình trên cùng)
Cám ơn các bạn đã theo dõi bài viết. Hy vọng bài viết có thể giúp các bạn tiếp tục học và làm việc cùng với AWS Lambda và API Gateway trong các dự án của mình.
Add new comment