読者です 読者をやめる 読者になる 読者になる

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 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:23:33.273000 538cc0 START RequestId: b7b86d04-47ad-11e6-8b8f-91040acecd69 Version: $LATEST
2016-07-12 06:23:33.273000 538cc0 END RequestId: b7b86d04-47ad-11e6-8b8f-91040acecd69

※あれ、なんかログ出力されてない...(別途確認)

6. まとめ

Serverless FrameworkのPython版ですね。