awsgi を使うと良き感じに使える
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Globals: Function: Timeout: 10 Runtime: python3.7 Resources: ApiBackendFunction: Type: AWS::Serverless::Function Properties: CodeUri: api_backend/ Handler: app.lambda_handler Events: HttpGet: Type: Api Properties: Path: '/' Method: GET HttpGetProxy: Type: Api Properties: Path: '/{proxy+}' Method: GET Outputs: HttpGetHelloApi: Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello"
# -*- coding: utf-8 -*- from flask import Flask, jsonify import awsgi app = Flask('sam-chalice') app.config['JSON_AS_ASCII'] = False @app.route('/hello') def index(): return jsonify({'hello': 'world'}) @app.route('/hello/<name>') def hello_name(name): return jsonify({'hello': name}) def lambda_handler(event, context): return awsgi.response(app, event, context)
flask + jinja2 でテンプレートウェブページ生成も行えます
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Globals: Function: Timeout: 10 Runtime: python3.7 Resources: ApiBackendFunction: Type: AWS::Serverless::Function Properties: CodeUri: api_backend/ Handler: app.lambda_handler Events: HttpGet: Type: Api Properties: Path: '/' Method: GET HttpGetProxy: Type: Api Properties: Path: '/{proxy+}' Method: GET Outputs: HttpGetApi: Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
# -*- coding: utf-8 -*- from datetime import datetime, timezone, timedelta from flask import Flask, render_template import awsgi app = Flask('sam-chalice') app.config['JSON_AS_ASCII'] = False @app.route('/') def index(): jst = timezone(timedelta(hours=+9), 'JST') now = datetime.now(tz=jst).strftime('%Y-%m-%d %H:%M:%S') message = f'現在時刻: {now}' return render_template('index.html', message=message) def lambda_handler(event, context): return awsgi.response(app, event, context)
<html> <body> {% if message %} <p>{{message}}</p> {% endif %} </body> </html>
SAM で定義した Lambda 関数内にて Chalice の @route
デコレータを使ってパスに応じた処理を行わせるだけの処理についてのメモ。基本的に確認している限りでは素直に動いてくれる。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Globals: Function: Timeout: 10 Runtime: python3.7 Resources: ApiBackendFunction: Type: AWS::Serverless::Function Properties: CodeUri: api_backend/ Handler: app.resources Events: HttpGetHello: Type: Api Properties: Path: '/hello' Method: GET HttpGetHelloName: Type: Api Properties: Path: '/hello/{name}' Method: GET HttpPostHelloName: Type: Api Properties: Path: '/users' Method: POST Outputs: HttpGetHelloApi: Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/'
# -*- coding: utf-8 -*- from chalice import Chalice resources = Chalice('sam-chalice') @resources.route('/hello') def index(): return {'hello': 'world'} @resources.route('/hello/{name}') def hello_name(name): return {'hello': name} @resources.route('/users', methods=['POST']) def create_user(): user_as_json = resources.current_request.json_body return {'user': user_as_json}
$ curl https://にゃ〜ん.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/ {"hello":"world"} $ curl https://にゃ〜ん.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/keiichi {"hello":"keiichi"} $ curl -X POST https://にゃ〜ん.execute-api.ap-northeast-1.amazonaws.com/Prod/users -H "Content-Type: application/json" --data '"hidekazu"' {"user":"hidekazu"}
ウェブ界隈でエンジニアとして労働活動に励んでいる @gomi_ningen 個人のブログです