I'm new to Terraform and implementing serverless architecture with API-Gateway,Lambda. When I create the API Gateway from console manually with same setting and linking the same lambda function, it works correctly. But it gives "Internal Server Error" when linking the same lambda function with rest api created via terraform. Is there something missing in my apiGateway.tf file which I've missed.
Lambda function is working correctly.
" apiGateway.tf "
variable "stage_name" {
default = "example"
type = string
}
variable "profile" {
default = "Dev"
}
//#### Creating REST API ####
resource "aws_api_gateway_rest_api" "airbnbAPI" {
name = "airbnbAPI"
endpoint_configuration {
types = ["REGIONAL"]
}
}
//#### Create Resource ####
resource "aws_api_gateway_resource" "listing" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
parent_id = aws_api_gateway_rest_api.airbnbAPI.root_resource_id
path_part = "listing"
}
//#### Create Method GET ####
resource "aws_api_gateway_method" "get" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
resource_id = aws_api_gateway_resource.listing.id
http_method = "GET"
authorization = "NONE"
api_key_required = false
}
resource "aws_api_gateway_integration" "integration-get" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
resource_id = aws_api_gateway_resource.listing.id
http_method = aws_api_gateway_method.get.http_method
integration_http_method = "GET"
type = "AWS_PROXY"
uri = aws_lambda_function.getListLambda.invoke_arn
}
//#### Method Response ####
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
resource_id = aws_api_gateway_resource.listing.id
http_method = aws_api_gateway_method.get.http_method
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
//#### Integration Response ####
resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
resource_id = aws_api_gateway_resource.listing.id
http_method = aws_api_gateway_method.get.http_method
status_code = aws_api_gateway_method_response.response_200.status_code
response_templates = {
"application/json" = ""
}
}
//#### Create Stage ####
resource "aws_api_gateway_stage" "test" {
deployment_id = aws_api_gateway_deployment.deployment1.id
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
stage_name = var.profile
}
//###### Deploy API Gateway ######
resource "aws_api_gateway_deployment" "deployment1" {
rest_api_id = aws_api_gateway_rest_api.airbnbAPI.id
triggers = {
redeployment = sha1(jsonencode(aws_api_gateway_rest_api.airbnbAPI.body))
}
depends_on = [aws_api_gateway_integration.integration-get]
lifecycle {
create_before_destroy = true
}
}
//#### Creating trigger for Lambda for REST API ####
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.getListLambda.function_name
principal = "apigateway.amazonaws.com"
/# The "/*/*" portion grants access from any method on any resource
/# within the API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.airbnbAPI.execution_arn}/*/*"
}
/# Output with invoke URL
output "complete_invoke_url" {value = "${aws_api_gateway_deployment.deployment1.invoke_url}${aws_api_gateway_stage.test.stage_name}/${aws_api_gateway_resource.listing.path_part}"}
Images and screenshots
Creating API Gateway manually from console working correctly
API Gateway Rest API Created via Terraform
Expected Output from RestAPI getting correctly when creating API from console manually