REST APIからHTTP APIを経由してLambdaを実行する – 【AWS】

REST APIからHTTP APIを経由してLambdaを実行する – 【AWS】

REST APIのリソースポリシーを使用しつつ、HTTP APIのJWT検証をしたかったので、REST APIからHTTP API経由でLambdaを実行できるように設定します。

HTTP API

HTTP APIを作成します。

REST API

REST APIの「統合タイプ」を「HTTP」にして、作成しておいたHTTP APIのエンドポイントURL、HTTPメソッドを指定します。

「HTTPプロキシ統合の使用」にチェックを入れます。

これで、REST API → HTTP API → Lambdaを実行することが可能です。

テスト

JWTオーソライザーをアタッチして、curlでテストします。叩くのはREST APIのURLです。

$ curl -i -X POST \
  https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/hoge
HTTP/1.1 401 Unauthorized
Date: Fri, 25 Aug 2023 10:59:53 GMT
Content-Type: application/json
Content-Length: 26
Connection: keep-alive
x-amzn-RequestId: d8c3535c-df9f-46c6-afcc-be9cb76a1619
apigw-requestid: KNpykiA6tjMEP7w=
x-amzn-Remapped-Content-Length: 26
x-amzn-Remapped-Connection: keep-alive
x-amz-apigw-id: KNpykFEZNjMF89g=
x-amzn-Remapped-www-authenticate: Bearer
x-amzn-Remapped-Date: Fri, 25 Aug 2023 10:59:53 GMT

{"message":"Unauthorized"}

ヘッダ情報にJWTがないので401エラーになります。

 

JWTオーソライザーを外して、通信がOKかをテストします。

curl -i -X POST https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/hoge
HTTP/1.1 200 OK
Date: Fri, 25 Aug 2023 11:01:18 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 20
Connection: keep-alive
x-amzn-RequestId: ef115265-a18a-4d2a-8d94-c6064c66cdf4
Apigw-Requestid: KNp_xiUttjMEP-w=
x-amzn-Remapped-Content-Length: 20
x-amzn-Remapped-Connection: keep-alive
x-amz-apigw-id: KNp_xAuKNjMFYtQ=
x-amzn-Remapped-Date: Fri, 25 Aug 2023 11:01:18 GMT

"Hello from Lambda!"

OKです。

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました