API GatewayからLambdaに渡ってくるプロキシ統合イベントのペイロードの形式、V2だとどんなのか知りたかったけど、ぱっと探して見つかった公式ドキュメントのサンプルが微妙だったので自分で確認した
REST API (無印) はこちら↓
リソース /{param1}/{param2}/hello
のメソッド ANY
でLambda関数を呼び出せるようにし、ステージ v1
を作成し、以下の curl
コマンドで叩いたときのイベントの中身
curl -X POST -H 'Content-Type: application/json' -H 'X-Foo: bar' -d '{"aaa":123}' 'https://9999999999.execute-api.ap-northeast-1.amazonaws.com/v1/xxx/yyy/hello?hoge=hg&piyo=py'
{
"version": "2.0",
"routeKey": "ANY /{param1}/{param2}/hello",
"rawPath": "/v1/xxx/yyy/hello",
"rawQueryString": "hoge=hg&piyo=py",
"headers": {
"accept": "*/*",
"content-length": "11",
"content-type": "application/json",
"host": "9999999999.execute-api.ap-northeast-1.amazonaws.com",
"user-agent": "curl/7.68.0",
"x-amzn-trace-id": "Root=1-60d75b9e-0d9a8b2473795833227c3af2",
"x-foo": "bar",
"x-forwarded-for": "1.1.1.1",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
},
"queryStringParameters": {
"hoge": "hg",
"piyo": "py"
},
"requestContext": {
"accountId": "000000000000",
"apiId": "9999999999",
"domainName": "9999999999.execute-api.ap-northeast-1.amazonaws.com",
"domainPrefix": "9999999999",
"http": {
"method": "POST",
"path": "/v1/xxx/yyy/hello",
"protocol": "HTTP/1.1",
"sourceIp": "1.1.1.1",
"userAgent": "curl/7.68.0"
},
"requestId": "BitA2hMuNjMEPFQ=",
"routeKey": "ANY /{param1}/{param2}/hello",
"stage": "v1",
"time": "26/Jun/2021:16:53:50 +0000",
"timeEpoch": 1624726430873
},
"pathParameters": {
"param1": "xxx",
"param2": "yyy"
},
"stageVariables": {
"kokokara": "hajimaru",
"my": "stage"
},
"body": "{\"aaa\":123}",
"isBase64Encoded": false
}
- APIのパス (ステージ名を含む) は
rawPath
- HTTPメソッドは
requestContext.http.method
(※routeKey
の先頭のメソッド名はAPI Gatewayのリソースのメソッド名であり、実行されたメソッド名ではないのでこっちを見る。) - リクエスト元IPアドレスは
requestContext.http.sourceIp
- リクエストヘッダは
headers
- リクエストボディは
body
- クエリパラメータは
queryStringParameters
- パスパラメータは
pathParameters
- API Gatewayのリソースのパスは
routeKey
を分解して取り出す - API Gatewayのステージ名は
requestContext.stage
- API Gatewayのステージ変数は
stageVariables