[[AWSメモ]] > * ChaliceでAPI Gateway&Lambda開発 [#hb0ecdab] #setlinebreak(on); #contents -- 関連 --- [[Chaliceで外部ライブラリを利用する]] --- [[Flaskの基礎]] --- [[Nginx+uwsgi+Flaskをdockerで動かす]] --- [[AWS API Gateway&LambdaをFlaskに移行しやすいように作る]] -- 参考 --- https://github.com/aws/chalice --- http://chalice.readthedocs.io/en/latest/api.html ** インストール [#ab8caee5] #html(<div style="padding-left:10px">) #myterm2(){{ pip install chalice }} #html(</div>) ** プロジェクト作成 [#n03e012e] #html(<div style="padding-left:10px">) #myterm2(){{ chalice new-project helloworld && cd helloworld }} #html(</div>) ** サンプルコード [#rc85a248] #html(<div style="padding-left:10px">) Flask でアプリケーション作成をした事があれば、スムーズに利用する事ができると思う。 ※デコレータ等は、ほぼ Flask のインターフェースと同じ。 app.py #mycode2(){{ from chalice import Chalice, Response app = Chalice(app_name='helloworld') @app.route('/sample1', methods=['GET', 'POST', 'PUT', 'DELETE']) def sample1(): request = app.current_request headers = request.headers try: #if hasattr(request, 'json_body'): body = request.json_body except: body = None ## debug ## if headers: for k in headers: print ( k + " = " + headers[k] ) if body: for key in body: print ( key + " = " + body[key] ) ########### response = { "method": request.method, "json_body": body } return { 'response': response } @app.route('/sample2/{key}', methods=['GET', 'POST', 'PUT', 'DELETE']) def sample2(key): request = app.current_request headers = request.headers try: #if hasattr(request, 'json_body'): body = request.json_body except: body = None ## debug ## if key: print ( "key is " + key ) if headers: for k in headers: print ( k + " = " + headers[k] ) #body = json.loads(request.json_body) if body: for key in body: print ( key + " = " + body[key] ) ########### response = { "method": request.method, "json_body": body } @app.route('/error1') def error1(): raise BadRequestError("This is a bad request") @app.route('/error2') def error2(): error = {'code': 'ERROR2', 'message': "Error message!" } return Response(body=error, status_code=400, headers={'Content-Type': 'text/plain'}) }} #html(</div>) ** 動作確認 [#jd60f398] #html(<div style="padding-left:10px">) *** ローカル起動 [#j4829ccd] #html(<div style="padding-left:10px">) #myterm2(){{ chalice local }} #html(</div>) *** 確認リクエスト送信 [#a233041d] #html(<div style="padding-left:10px">) #myterm2(){{ curl -XPOST -H 'Content-type: application/json' --data '{"param1":"ABC"}' http://localhost:8000/sample2/123 }} #html(</div>) #html(</div>) ** デプロイ [#h3023dcf] #html(<div style="padding-left:10px">) #myterm2(){{ chalice deploy }} #html(</div>) ** 削除 [#b6b2e1cc] #html(<div style="padding-left:10px">) #myterm2(){{ chalice delete --stage dev }} #html(</div>) ** 設定ファイル [#ld995391] #html(<div style="padding-left:10px">) 諸々の設定を コンフィグファイル( ./chalice/config.json )で行う事ができる http://chalice.readthedocs.io/en/latest/topics/configfile.html #mycode2(){{ { "version": "2.0", "app_name": "app", "stages": { "dev": { "autogen_policy": true, "api_gateway_stage": "dev" }, "beta": { "autogen_policy": false, "iam_policy_file": "beta-app-policy.json" }, "prod": { "manage_iam_role": false, "iam_role_arn": "arn:aws:iam::...:role/prod-role" } } } }} #html(</div>) ** 制限 [#o3133fab] #html(<div style="padding-left:10px">) *** API Gateway は「Lambdaプロキシ統合の使用」が ON の状態で作成される [#pd2b5d2c] #html(<div style="padding-left:10px">) 回避策はあるかもしれないが。未調査。 API Gateway に引っ付く Lambda しか作成しない場合は特に問題ないが、 そうでない場合は、リクエスト、レスポンスの扱いに注意が必要。 ※Lambdaプロキシ統合の使用を前提に呼び出す必要がある。 ※というか、現時点(2018/1)ではChalice では API Gateway に引っ付かない Lambda の作成はできないらしい。(後述) #html(</div>) *** Lambda関数のみをデプロイする事はできない [#e63f9747] #html(<div style="padding-left:10px">) 下記URLによると、少なくとも1つの @app.route を含める必要があり、 API Gateway 無しで Lambda をデプロイする事はできないらしい。 http://chalice.readthedocs.io/en/latest/topics/purelambda.html #html(<div style="padding:10px; border:1px solid #000;display:inline-block">) Limitations: You must provide at least 1 @app.route decorator. It is not possible to deploy only lambda functions without an API Gateway API. #html(</div>) #html(</div>) #html(</div>)