SAMのURLパラメタの分析にflaskを使う
template.ymlにて
SampleFlaskFunction: Type: AWS::Serverless::Function Properties: CodeUri: src/ Handler: flask_test.lambda_handler Runtime: python3.7 Events: HttpGet: Type: Api Properties: Path: '/flask' Method: GET HttpGetProxy: Type: Api Properties: Path: '/flask/{proxy+}' Method: GET
のような設定をしている時に
lambda側でパラメタを取得したい場合は
from flask import Flask, jsonify import awsgi app = Flask(__name__) @app.route('/flask/<name>') def output_name(name): return jsonify({'input': name}) def lambda_handler(event, context): return awsgi.response(app, event, context)
このようにすると取得できる
なおアクセスできるURLは
http://127.0.0.1:3000/flask/name
SAM側で振り分けたflaskというディレクトリもlambdaでも設定する必要がある。(アクセスしたパスがそのまま渡ってきているイメージか)
@app.route('/flask/<name>/test/<test>/test2/<test2>') def output_name(name,test,test2): return jsonify({'value': name + test + test2}
このように複数の値の取得も可能