工具 serverless ,是用 node.js 開發的。只要用 npm 安裝即可,npm 這個工具的介紹就不贅述,網路上資料很多。
安裝工具
npm install -g serverless試用 serverless
這個工具有提供 很多範例程式 供人測試,我們這次使用 python / aws dynamoDB 這個例子。
git clone https://github.com/serverless/examples.git (載下範例來)
cd aws-python-rest-api-with-dynamodb (我們選用這個範例)
原本是可以直接用 serverless deploy 這個指令做 deploy,但是我們現在還沒有設好我們 AWS 上的 credentials,所以還需要設定。
設定 AWS credentials
這個影片教你如何在 IAM 中拿到 key ID 以及 secret,並且透過指令
serverless config credentials --provider aws --key xxxx --secret xxxx
將其存成預設執行的設定檔。
也有一些設定 credentials 的其他方法。
正式執行 deploy
serverless deploy
輸出如下:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service serverless-rest-api-with-dynamodb.zip file to S3 (5.37 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
......................................................................................................
Serverless: Stack update finished...
Service Information
service: serverless-rest-api-with-dynamodb
stage: dev
region: us-east-1
stack: serverless-rest-api-with-dynamodb-dev
resources: 34
api keys:
None
endpoints:
POST - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos
GET - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos
GET - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
PUT - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
DELETE - https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos/{id}
functions:
create: serverless-rest-api-with-dynamodb-dev-create
list: serverless-rest-api-with-dynamodb-dev-list
get: serverless-rest-api-with-dynamodb-dev-get
update: serverless-rest-api-with-dynamodb-dev-update
delete: serverless-rest-api-with-dynamodb-dev-delete
layers:
None
部署完畢。
測試一下能否執行
範例裡面,會個別教學怎麼執行測試。我們直接列出執行狀況:
注意,REST 接觸的端口,就是上面部署完後的輸出結果,上面我用紅色標註。
建立 Todo 資料
$curl -X POST https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos --data '{ "text": "Learn Serverless" }'
輸出
{"text": "Learn Serverless", "checked": false, "id": "0828b186-4549-11e9-923d-eaf73cc0b517", "createdAt": 1552451558329, "updatedAt": 1552451558329}
查詢 Todo 資料
$ curl https://0c1qlf7h8a.execute-api.us-east-1.amazonaws.com/dev/todos
輸出
[{"text": "Learn Serverless", "checked": false, "id": "0828b186-4549-11e9-923d-eaf73cc0b517", "createdAt": 1552451558329, "updatedAt": 1552451558329}]
是可以正確執行的。
沒有留言:
張貼留言