0

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
7
  • 1
    It seems like there wasn't an update to the module in quite a while. Which Terraform version are you using? Commented Jul 15, 2022 at 10:20
  • according to the dockerfile, the deployment process is using terraform v1.0.0, obtained via wget Commented Jul 15, 2022 at 10:40
  • How about provider version? Commented Jul 15, 2022 at 10:50
  • provider version for what exactly? I have a versions.tf file with as required_providers hashicorp/archive, hashicorp/aws and hashicorp/random with required_version = ">= 0.13" Commented Jul 15, 2022 at 11:14
  • 1
    Well, I am not sure that the module can work with the newer version of the AWS provider. Try anything in the 3.x.x range and see if that will work. Version 4 of the provider introduced breaking changes (not sure if any of it is related to API Gateway though). Commented Jul 15, 2022 at 11:35

1 Answer 1

2

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}"
}
Sign up to request clarification or add additional context in comments.

Comments

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.