3

I am trying to create rest API using terraform however I am getting below error which I am not able to resolve.

Error: Error creating API Gateway Integration Response: BadRequestException: Invalid mapping expression specified: Validation Result: warnings : [], errors : [No method response exists for method.]
│ 
│   with aws_api_gateway_integration_response.create_report_integration_options_response,
│   on test_api_post.tf line 119, in resource "aws_api_gateway_integration_response" "create_report_integration_options_response":
│  119: resource "aws_api_gateway_integration_response" "create_report_integration_options_response" {

Below is the terraform configuration

provider "aws" {
  region = "us-east-2"
}


terraform {
  required_version = "> 0.14.0"
  required_providers {
    aws = "~> 3.0"
  }
}

resource "aws_api_gateway_rest_api" "first_api" {
  name = "test-api"
  tags = {
    By = "Terraform"
  }
}

resource "aws_api_gateway_resource" "create_report" {
  parent_id   = aws_api_gateway_rest_api.first_api.root_resource_id
  path_part   = "create_report"
  rest_api_id = aws_api_gateway_rest_api.first_api.id
}

resource "aws_api_gateway_method" "create_report_method" {

  rest_api_id   = aws_api_gateway_rest_api.first_api.id
  http_method   = "POST"
  resource_id   = aws_api_gateway_resource.create_report.id
  authorization = "NONE"
}


resource "aws_api_gateway_integration" "create_report_integration" {
  http_method = aws_api_gateway_method.create_report_method.http_method
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode({
      statusCode = 200
    })
  }
}

resource "aws_api_gateway_integration_response" "create_report_integration_response" {
  http_method = "POST"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_templates = {
    "application/json" = jsonencode({

      "message_str" : "report requested, check your phone shortly"

    })
  }

  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
    "method.response.header.Access-Control-Allow-Methods" : "'POST'"
    "method.response.header.Access-Control-Allow-Origin" : "'*'",
  }
  depends_on = [aws_api_gateway_integration.create_report_integration]
}

resource "aws_api_gateway_method_response" "create_report_method_response" {
  http_method = "POST"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : false,
    "method.response.header.Access-Control-Allow-Origin" : false,
    "method.response.header.Access-Control-Allow-Methods" : false
  }
  depends_on = [aws_api_gateway_method.create_report_method, aws_api_gateway_integration.create_report_integration]
}


// options method
resource "aws_api_gateway_method" "create_report_options_method" {
  http_method   = "OPTIONS"
  resource_id   = aws_api_gateway_resource.create_report.id
  rest_api_id   = aws_api_gateway_rest_api.first_api.id
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "create_report_options_integration" {
  http_method = aws_api_gateway_method.create_report_options_method.http_method
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  type        = "MOCK"
  request_templates = {
    "application/json" = jsonencode({
      statusCode = 200
    })
  }
}

resource "aws_api_gateway_method_response" "create_report_method_options_response" {
  http_method = "OPTIONS"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : true,
    "method.response.header.Access-Control-Allow-Origin" : true,
    "method.response.header.Access-Control-Allow-Methods" : true,
    "method.response.header.Access-Control-Allow-Credentials" : true
  }
  response_models = {
    "application/json" = "Empty"
  }

  depends_on = [aws_api_gateway_method.create_report_options_method, aws_api_gateway_integration.create_report_options_integration]
}

resource "aws_api_gateway_integration_response" "create_report_integration_options_response" {
  http_method = "OPTIONS"
  resource_id = aws_api_gateway_resource.create_report.id
  rest_api_id = aws_api_gateway_rest_api.first_api.id
  status_code = 200
  response_templates = {

  }
  response_parameters = {
    "method.response.header.Access-Control-Allow-Headers" : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
    "method.response.header.Access-Control-Allow-Methods" : "'POST,OPTIONS'",
    "method.response.header.Access-Control-Allow-Origin" : "'*'",
    "method.response.header.Access-Control-Allow-Credentials" : "'true'"
  }
  depends_on = [aws_api_gateway_method.create_report_options_method, aws_api_gateway_integration.create_report_options_integration]
}

Could someone please help me out in identifying what I am doing wrong as its been 2 days and I am still not able to figure out what exactly the issue is . Thank you

1 Answer 1

3

This error happens because terraform hasn't finished creating the aws_api_gateway_method_response resources before it starts attempting to create the related aws_api_gateway_integration_responses.

To fix this issue:

  • add the aws_api_gateway_method_response.create_report_method_response into the aws_api_gateway_integration_response.create_report_integration_response's depends on list
  • add the aws_api_gateway_method_response.create_report_method_options_response into the aws_api_gateway_integration_response.create_report_integration_options_response's depends on list.
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.