LoginSignup
0
0

Terraformでlambdaプロキシ統合のAPIを作成する際の注意点

Posted at

はじめに

Terraform初心者のため、間違いなどあればコメントで指摘していただけると助かります。

結論

API Gatewayとlambdaを組み合わせたAPIを作成するときは、GET APiの場合でもaws_api_gateway_integrationのintegration_http_methodをPOSTとしよう。

api.tf
resource "aws_api_gateway_integration" "get_section" {
rest_api_id = aws_api_gateway_rest_api.main.id
resource_id = aws_api_gateway_resource.get_section.id
http_method = aws_api_gateway_method.get_section.http_method
#Lambdaプロキシ統合を使用する場合は、GETメソッド作成時もaws_api_gateway_integration中のintegration_http_methodをPOSTにする必要がある。
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.get_section.invoke_arn
}

発生の流れ

Terraformを使用してAWSインフラを構築している最中、GET APIを作成しているタイミングで発生しました。
自分はTerraformに慣れていなかったため。マネジメントコンソールで設定項目を確認してから、Terraformのコードを記述するという手順を踏んでいました。
その際API Gatewayのintegration設定項目において、GET APIを作成するからGETだろうと安直に設定した箇所が上記になります。
マネジメントコンソールにおいて設定する場合は、メソッド作成時にlambda統合を有効にするというチェックをつけるだけで、裏側でうまいこと設定してくれる部分みたいですね。

原因

lambdaプロキシ統合を利用する場合、メソッドリクエストが統合リクエストを呼び、統合リクエストがlambdaを呼び出すという手順を踏みます。この際、メソッドリクエストのHTTPメソッドがなんであろうと統合リクエストでPOSTでないとlambdaまでリクエストが届きません。

こちらにも書かれていましたね。

integration_http_method - (Optional) Integration HTTP method (GET, POST, PUT, DELETE, HEAD, OPTIONs, ANY, PATCH) specifying how API Gateway will interact with the back end. Required if type is AWS, AWS_PROXY, HTTP or HTTP_PROXY. Not all methods are compatible with all AWS integrations. e.g., Lambda function can only be invoked via POST.

終わりに

始めて触る技術は公式で一次情報を参照する癖をつけたいです。

0
0
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
0
0