Error creating API Gateway Integration Response: NotFoundException: Invalid Integration identifier specified

Viewed 13562

Objective

Solution or workaround for the problem.

Problem

The Terraform API Gateway integration with Firehose below works if Firehose is created separately in advance.

resource "aws_api_gateway_integration" "click_put" {
  rest_api_id = data.aws_api_gateway_rest_api.mysfit.id
  resource_id = aws_api_gateway_resource.click.id
  type        = "AWS"
  uri         = "arn:aws:apigateway:${var.REGION}:firehose:action/PutRecord"
  credentials = aws_iam_role.api_click.arn

  http_method = aws_api_gateway_method.click_put.http_method
  integration_http_method = "POST"
  request_parameters = {
    "integration.request.header.Content-Type" = "'application/x-amz-json-1.1'"
  }
  passthrough_behavior = "NEVER"
  request_templates = {
    "application/json" = <<EOF
{
  "DeliveryStreamName": "${local.firehose_name}",
  "Record": {
    "Data": "$util.base64Encode($input.json('$'))"
  }
}
EOF
  }
}
...
resource "aws_api_gateway_integration_response" "click_put" {
  rest_api_id = data.aws_api_gateway_rest_api.mysfit.id
  resource_id = aws_api_gateway_resource.click.id
  http_method = aws_api_gateway_method.click_put.http_method
  status_code = aws_api_gateway_method_response.click_put.status_code
  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = "'*'"
  }
}

However, if they are created in the same root module, it causes the error.

Error creating API Gateway Integration Response: NotFoundException: Invalid Integration identifier specified

  on api_click.tf line 185, in resource "aws_api_gateway_integration_response" "click_put":
 185: resource "aws_api_gateway_integration_response" "click_put" {
2 Answers

Workaround/Solution

Place a dependency on the aws_api_gateway_integration from the resource causing "NotFoundException: Invalid Integration identifier specified".

resource "aws_api_gateway_integration_response" "click_put" {
  rest_api_id = data.aws_api_gateway_rest_api.mysfit.id
  resource_id = aws_api_gateway_resource.click.id
  http_method = aws_api_gateway_method.click_put.http_method
  status_code = aws_api_gateway_method_response.click_put.status_code
  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = "'*'"
  }

  depends_on = [
    aws_api_gateway_integration.click_put
  ]
}

References

There are indications that depends_on aws_api_gateway_integration or placing a wait would be the way.

Probably waiting for the complete completion of the aws_api_gateway_integration resource would be a recommended practice.

resource "aws_api_gateway_resource" "proxy" {
  rest_api_id = aws_api_gateway_rest_api.rest-api.id
  parent_id   = aws_api_gateway_rest_api.rest-api.root_resource_id
  path_part   = "{proxy+}"
}

resource "null_resource" "method-delay" {
  provisioner "local-exec" {
    command = "sleep 5"
  }
  triggers = {
    response = aws_api_gateway_resource.proxy.id
  }
}

resource "aws_api_gateway_method_response" "response" {
  depends_on = [null_resource.method-delay]
  http_method = "ANY"
  resource_id = aws_api_gateway_resource.proxy.id
  rest_api_id = aws_api_gateway_rest_api.rest-api.id
}

you can just add depends_on. Should be something like:

resource "aws_api_gateway_integration" "create-oauth-lambda" {
  rest_api_id             = aws_api_gateway_rest_api.create-oauth-token-api-gw.id
  resource_id             = aws_api_gateway_resource.auth-token-resource.id
  http_method             = aws_api_gateway_method.method.http_method
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = "some URI"

}

resource "aws_api_gateway_method_response" "response-200" {
  depends_on  = [aws_api_gateway_integration.create-oauth-lambda]
  rest_api_id = aws_api_gateway_rest_api.create-oauth-token-api-gw.id
  resource_id = aws_api_gateway_resource.auth-token-resource.id
  http_method = aws_api_gateway_method.method.http_method
  status_code = "200"
  response_models = {
    "application/json" = "Empty"
  }
}
Related