うさぎ駆動開発

UWP, Xamarin.Macを中心によしなしごとを書いていきます。

AWS API Gateway Lambda proxy integration を使う

API Gatewayに,20日に新機能がリリースされました。

Amazon API Gateway に API 設定を簡素化する 3 つの新機能を追加

今まではAPI Gateway側の設定をいろいろ通ってからLambdaファンクションに到達していましたが,認証後はリクエストをするっとLambdaに流せると。 まあ便利。ということでさっそく使ったのですが,何をやっても502エラーになる。

レスポンスはこのように返しましょう。

class ApiGatewayResponse
{
    constructor(){
        this.statusCode = 200;
        this.headers = {};
        this.body = "";
    }
}

exports.handler = (event, context, callback) => {
    const res = new ApiGatewayResponse();

    // 任意のレスポンスヘッダ
    res.headers["x-custom-header"] = "foo";

    // body は string で。Objectは不可
    res.body = JSON.stringify(event);

    // context.succeed は deprecated
    callback(null, res);
};