Python Serverless Microframework for AWS(Preview)のchaliceを使ってみる

どうも、セクションナイン吉田真吾@yoshidashingo)です。

Python Serverless Microframework for AWSという、Serverless Frameworkに似たWeb APIのフレームワークがPreviewリリースされたのでチュートリアルどおり試してみます。

Preview the Python Serverless Microframework for AWS | AWS Developer Tools Blog

※ GitHubはこちら

github.com

1. インストール

$ sudo pip install chalice

2. 新規プロジェクト作成

$ chalice new-project demo
$ cd demo

3. デフォルトの定義のままデプロイして確認する

  • 定義ファイル app.py を確認する
$ vi app.py
  • デフォルトで以下のように定義されている
from chalice import Chalice

app = Chalice(app_name='demo')


@app.route('/')
def index():
    return {'hello': 'world'}

  • デプロイする
$ chalice deploy
Initial creation of lambda function.
Updating IAM policy.
Creating deployment package.
Lambda deploy done.
Initiating first time deployment...
Deploying to: dev
https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
  • エンドポイントにアクセスしてみる
$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
{"hello": "world"}
  • 存在しないパスにアクセスしてもエラー
$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/AWS/hello
{"message":"Missing Authentication Token"}

4. 定義ファイルにAPI機能を追加してデプロイする

  • 定義ファイル app.py を編集する
$ vi app.py
  • 以下のように定義にAPI機能を追加する
from chalice import Chalice

app = Chalice(app_name='demo')


@app.route('/')
def index():
    return {'hello': 'world'}

@app.route('/{name}/hello')
def hello(name):
    return {'hello': name}

  • デプロイする
$ chalice deploy
Updating IAM policy.
Updating lambda function...
Regen deployment package...
Sending changes to lambda.
Lambda deploy done.
API Gateway rest API already found.
Deleting root resource id
Done deleting existing resources.
Deploying to: dev
https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
  • エンドポイントにアクセスして機能追加されたことを確認する
$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
{"hello": "world"}

$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/AWS/hello
{"hello": "AWS"}

$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/serverless/hello
{"hello": "serverless"}

5. ログ出力して、確認してみる

  • 定義ファイル app.py を編集する
$ vi app.py
  • 以下のようにprint文を追加してログを出力するようにする
from chalice import Chalice

app = Chalice(app_name='demo')


@app.route('/')
def index():
    return {'hello': 'world'}

@app.route('/{name}/hello')
def hello(name):
    print('I want to log {}'.format(name))
    return {'hello': name}

  • デプロイする
$ chalice deploy
Updating IAM policy.
Updating lambda function...
Regen deployment package...
Sending changes to lambda.
Lambda deploy done.
API Gateway rest API already found.
Deleting root resource id
Done deleting existing resources.
Deploying to: dev
https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/
  • ログ出力を追加したAPIにアクセスする
$ curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/logme/hello
{"hello": "logme"}
  • ログを確認する
$ chalice logs --num-entries 2
2016-07-12 06:45:12.099000 b929c8 START RequestId: beb5562b-47b0-11e6-97f7-2345ee85dba4 Version: $LATEST
2016-07-12 06:45:12.100000 b929c8 I want to log logme
2016-07-12 06:45:12.101000 b929c8 END RequestId: beb5562b-47b0-11e6-97f7-2345ee85dba4
2016-07-12 06:45:12.101000 b929c8 REPORT RequestId: beb5562b-47b0-11e6-97f7-2345ee85dba4   Duration: 0.40 ms Billed Duration: 100 ms    Memory Size: 128 MB    Max Memory Used: 15 MB

6. まとめ

Serverless Frameworkであればステージの指定やプロファイルの切替、エンドポイントやリソースの削除などもできますが、こちらのPython版はPreview版なので、まだ機能的にはさほど揃ってないようです。