Search code examples
amazon-web-servicesaws-lambdaterraformaws-api-gateway

Deploying AWS API Gateway lambda - BadRequestException: Invalid model identifier specified: Empty


Newcomer to the AWS computing using terraform

I have two files for declaring an AWS API Gateway and it fails when trying to deploy using gitlab ci, especially when it is treating the generated module

 Error: Error creating API Gateway Method Response: BadRequestException: Invalid model identifier specified: Empty
│ 
│   with module.corslambdaApiHelloWorld.aws_api_gateway_method_response._,
│   on .terraform/modules/corslambdaApiHelloWorld/main.tf line 63, in resource "aws_api_gateway_method_response" "_":
│   63: resource "aws_api_gateway_method_response" "_" {

Here is the file that generates that module:

// https://domain/{base}/helloworld
resource "aws_api_gateway_resource" "ApiResourceHelloWorld" {
  rest_api_id   = aws_api_gateway_rest_api.Api.id
  parent_id     = aws_api_gateway_rest_api.Api.root_resource_id
  path_part     = "helloworld"
}

module "corslambdaApiHelloWorld" {
  source            = "squidfunk/api-gateway-enable-cors/aws"
  version           = "0.3.3"

  api_id            = aws_api_gateway_rest_api.Api.id
  api_resource_id   = aws_api_gateway_resource.ApiResourceHelloWorld.id

  allow_methods     = ["GET"]
}

And here is the resource generated in the module file when I use terraform init in my local workspace

resource "aws_api_gateway_method_response" "_" {
  rest_api_id = var.api_id
  resource_id = var.api_resource_id
  http_method = aws_api_gateway_method._.http_method
  status_code = 200

  response_parameters = local.method_response_parameters

  response_models = {
    "application/json" = "Empty"
  }

  depends_on = [
    aws_api_gateway_method._,
  ]
}

We have already tried declaring a resource using this method, but from the deployment process, it still generates the response_models with the same value

We have verified similar projects by using terraform init into their terraform codes, and it generates something similar as the modules

But those projects are deployed properly, unlike ours

Notes:

  • using terraform plan in my workspace does not generate any error
  • the lambda function attached to the API was deployed succesfully and is available when browsing the AWS console
  • in the gitlab job console, this output is present:
  # module.corslambdaApiHelloWorld.aws_api_gateway_method_response._ will be created
  + resource "aws_api_gateway_method_response" "_" {
      + http_method         = "OPTIONS"
      + id                  = (known after apply)
      + resource_id         = "m3aimb"
      + response_models     = {
          + "application/json" = "Empty"
        }
      + response_parameters = {
          + "method.response.header.Access-Control-Allow-Headers" = true
          + "method.response.header.Access-Control-Allow-Methods" = true
          + "method.response.header.Access-Control-Allow-Origin"  = true
          + "method.response.header.Access-Control-Max-Age"       = true
        }
      + rest_api_id         = "7ptzpas417"
      + status_code         = "200"
    }

I mainly want to know what is supposed to be generated in that module for the build to pass

Additional note asked from comments:

  • The dockerfile used by gitlab-ci is using terraform_1.0.0_linux_amd64 installed using wget
  • Tried using hashicorp/aws v3.75.0 locally, getting the same error on terraform apply command

Solution

  • We solved it by declaring an "empty model" resource in the api.tf file

    resource "aws_api_gateway_model" "emptyModel" {
      rest_api_id = aws_api_gateway_rest_api.Api.id
      name = "Empty"
      description = "This is a default empty schema mode"
      content_type = "application/json"
    
      schema = "{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"title\": \"Empty Schema\",\n  \"type\": \"object\"\n}"
    }