LoginSignup
40
22

More than 5 years have passed since last update.

API Gatewayの「Lambda プロキシ統合の使用」オプション

Posted at

久々にAPI Gatewayの管理画面を開いたら、統合リクエストのページに以下のようなオプションが出ていいたので早速試して見ました。というのも、これまでAPI Gatewayを利用してLambdaを呼び出す際に、HTTPリクエストに含まれている情報を連携するために、わざわざMapping Templateを作成して、HTTPヘッダーなどをeventオブジェクトに設定していたのですが、これを使えば、HTTPヘッダーの情報がそのままLambdaに伝わるのでは!?と感じたためです。

2016-10-17.png

まずはチェックをはずした状態でAPI Gatewayのテストから以下のJSONをPOSTしてみます。

{ Message: 'Hello World' }

ログに出力されたLambdaのeventの中身は以下の通り。

{ Message: 'Hello World' }

続いて、「Lambda プロキシ統合の使用」オプションを設定した場合。
まずもってテスト画面にクエリ文字列とヘッダーを設定する欄が登場。

2016-10-17 (1).png

今回も同じく

{ Message: 'Hello World' }

をPOSTしてみたところ、Lambdaのeventには以下の値が設定されていました。

{
    resource: '/mook',
    path: '/mook',
    httpMethod: 'POST',
    headers: { Accept: ' application/json' },
    queryStringParameters: { param1: 'value1' },
    pathParameters: null,
    stageVariables: null,
    requestContext: {
        accountId: 'xxxxxxxxxxxx',
        resourceId: 'xxxxxx',
        stage: 'test-invoke-stage',
        requestId: 'test-invoke-request',
        identity: {
            cognitoIdentityPoolId: null,
            accountId: 'xxxxxxxxxxxx',
            cognitoIdentityId: null,
            caller: 'xxxxxxxxxxxx',
            apiKey: 'test-invoke-api-key',
            sourceIp: 'test-invoke-source-ip',
            cognitoAuthenticationType: null,
            cognitoAuthenticationProvider: null,
            userArn: 'arn:aws:iam::xxxxxxxxxxxx:root',
            userAgent: 'Apache-HttpClient/4.5.x (Java/1.8.0_102)',
            user: 'xxxxxxxxxxxx'
        },
        resourcePath: '/mook',
        httpMethod: 'POST',
        apiId: 'xxxxxxxxxx'
    },
    body: '{"Message": "Hello World"}'
}

とまあ、API Gatewayがこれまで丸めていたHTTPヘッダーの情報をかなりきちんと得ることができました。HTTPヘッダーに基づく処理をする場合には非常に役立ちますね。ただ、注意すべきは、event.bodyがJSONではなく、テキスト文字列になっているので、JSON.parseしてやらないとエラーになりますね。

40
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
22