6

If I manually add an Integration Request of type Lambda function, an Api Gateway trigger is automatically added to the lambda function.

If I do it via Terraform, everything looks correct but when I go look at the Lambda function it has no trigger.

If I then manually update the Integration Request (change to Mock and back to Lambda Function) the trigger is added to the Lambda function? Everything works after that.

What am I missing?

resource "aws_api_gateway_integration" "integration" {
  count = var.lambda_definition.apigateway ? 1 : 0
  rest_api_id = "${data.terraform_remote_state.apigateway.outputs.apigateway_id}"
  resource_id = aws_api_gateway_resource.api_proxy_resource[count.index].id
  http_method = "${aws_api_gateway_method.method[count.index].http_method}"
  integration_http_method = "ANY"
  type                    = "AWS_PROXY"
  uri                     = aws_lambda_function.lambda.invoke_arn
}
1
  • I'm using aws_apigatewayv2_api resource and I found out that the issue is simply that I didn't have the target argument on it. target = aws_lambda_function.lambda.arn is the LIFE SAVER! Source here. Commented Nov 16, 2021 at 11:48

2 Answers 2

12

Since you've not mentioned whether you specified proper permissions for your function, my guess is that you are missing aws_lambda_permission. This will explicitly give permissions for the api to invoke your function.

The resource would be (example only):

resource "aws_lambda_permission" "allow_api" {
  statement_id  = "AllowAPIgatewayInvokation"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.lambda.invoke_arn
  principal     = "apigateway.amazonaws.com"
}

When you do it manually in console, the AWS setups all these permissions in the background.

Sign up to request clarification or add additional context in comments.

1 Comment

Please use .function_name instead of .invoke_arn as the latter could fail the naming restrictions ("function_name" cannot be longer than 140 characters; "function_name" doesn't comply with [regex] restrictions).
8

Make sure that integration_http_method is set to POST and not to ANY as in your sample:

  integration_http_method = "POST"

See AWS Docs - midway - red box that says '! Important':

For Lambda integrations, you must use the HTTP method of POST for the integration request, according to the specification of the Lambda service action for function invocations. The IAM role of apigAwsProxyRole must have policies allowing the apigateway service to invoke Lambda functions. For more information about IAM permissions, see API Gateway permissions model for invoking an API.

1 Comment

I was facing the same problem. I had said integration_http_method to GET. The integration method for lambda always needs to be POST. The individual HTTP method under aws_api_gateway_method.http_method can be GET, PUT, POST etc based on the REST API method. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.