不工作AWS API网关方法请求路径参数(AWS API Gateway Method reques

2019-10-29 06:25发布

我想配置API网关将请求路由到我的后端GO微服务一个特定的路线。 我使用的是GET与请求方法VPC_LINK与集成NLB ,哪些路由到我的后端的microService运行Fargate 。 这是一个简单的REST写在API GO 。 在服务方面,在我的处理程序,我放了抓我的舞台路线,然后为一个“Hello World”响应所有响应/nmapscan路线,这正是我试图打。 然而,当我试着打我的后端服务与调用的URL,我不断收到包罗万象的响应,尽管出现正确在我的请求响应输出的请求路径。 我是新来的API网关和我有一种感觉,我失去了一些东西简单。 此外,当我跑了本地的容器,我连着壳就跑curl localhost:8000/v1/nmapscan ,并得到了正确的“Hello World!” 响应。 下面是反应,我的配置,以及从流量日志的回应:

RESPONSE:

user$ curl -v -X GET https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/v1/nmapscan/
> GET /v1/nmapscan/ HTTP/2
> Host: xxxxxxxxxx.execute-api.us-east-1.amazonaws.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200 
< content-type: application/json
< content-length: 27
< date: Wed, 25 Sep 2019 20:24:16 GMT
< x-amzn-requestid: df90f051-dbdd-405f-a708-73668ad955f1
< x-amz-apigw-id: XXXXXXXXXXX=
< x-amzn-trace-id: Root=1-5d8bccf0-44ebf9c5af13c90e1636de42
< x-cache: Miss from cloudfront
< via: 1.1 b7ddb18a56b4bad68ca78b085e9ca451.cloudfront.net (CloudFront)
< x-amz-cf-pop: EWR52-C2
< x-amz-cf-id: lvT1CGlv2fboFJ5AxE917Jr61Nwb4fQOwbranZ3s_vz0EJULhcwudQ==
< 
* Connection #0 to host xxxxxxxxx.execute-api.us-east-1.amazonaws.com left intact
This is the catch all path!

正如你所看到的,这是返回包罗万象的响应。 它应该返回的 “Hello World!”。

Configuration:

resource "aws_api_gateway_rest_api" "GOAPI" {
  name        = "GO"
  description = "REST API for GO APIs"
}

resource "aws_api_gateway_resource" "test" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  parent_id   = "${aws_api_gateway_rest_api.GOAPI.root_resource_id}"
  path_part   = "nmapscan"
}

resource "aws_api_gateway_method" "testmethod" {
  rest_api_id   = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id   = "${aws_api_gateway_resource.test.id}"
  http_method   = "GET"
  authorization = "NONE"
  request_parameters = {
    "method.request.path.nmapscan" = true
  }
}

resource "aws_api_gateway_integration" "integrationtest" {
  connection_type = "VPC_LINK"
  connection_id   = "${aws_api_gateway_vpc_link.test.id}"
  type = "HTTP"
  integration_http_method = "GET"
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"
  uri = "${format("http://%s:8000", aws_lb.myapis.dns_name)}"

//  request_parameters = {
//    "integration.request.path.nmapscan" = "method.request.path.nmapscan"
//  }
}


resource "aws_api_gateway_method_response" "test-200" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"
  status_code = "200"

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

resource "aws_api_gateway_integration_response" "testintegrationresponse" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"

  status_code = "${aws_api_gateway_method_response.test-200.status_code}"

  response_templates = {
    "application/json" = ""
  }
}

resource "aws_api_gateway_deployment" "testdeploy" {
  depends_on = ["aws_api_gateway_integration.integrationtest"]

  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  stage_name = "v1"
}

Flow Logs:

(fdaa14d3-08df-4847-ba63-a9644a65d265) Method request body before transformations:
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request URI: http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request headers: {x-amzn-apigateway-api-id=cqq6k2xrw3, Accept=application/json, User-Agent=AmazonAPIGateway_cqq6k2xrw3, Host=xxx-yyy-zzzzzz.elb.us-east-1.amazonaws.com, X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request body after transformations:
(fdaa14d3-08df-4847-ba63-a9644a65d265) Sending request to http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000
(fdaa14d3-08df-4847-ba63-a9644a65d265) Received response. Status: 200, Integration latency: 15 ms
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint response headers: {Content-Type=application/json, Date=Thu, 26 Sep 2019 03:22:22 GMT, Content-Length=27}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint response body before transformations: This is the catch all path!
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method response body after transformations: This is the catch all path!
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method response headers: {X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14, Content-Type=application/json}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Successfully completed execution
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method completed with status: 200

Answer 1:

您的文章看起来很像这个

如果你确信这不是在回应中提到的事情,那么我会检查您的合并请求路由。

您的卷曲请求的URI是/ V1 / nmapscan /这意味着API网关将着眼于STAGE V1,资源/ nmapscan。 一旦发生这种情况,API GW发送基于在所述结合请求配置的URI请求到VPC链接。 我不是超级熟悉Terraform,但它看起来像你把它发送到:

HTTP://aws_lb.myapis.dns_name:8000

我也看到你在terraform定义的“请求参数”(但也许注释掉?):


    //  request_parameters = {
    //    "integration.request.path.nmapscan" = "method.request.path.nmapscan"
    //  }

我会检查部署后,该API GW控制台的合并请求表明,这是被路由到正确的路线。 不知道是肯定的,我假设你的应用程序的路径实际上是HTTP://aws_lb.myapis.dns_name/nmapscan:8000和这不是正确传递到NLB,因为它没有正确出于某种原因在合并请求映射。

所有这一切都被说,看到URI是什么时,该请求被发送到NLB最简单的方法是让API网关执行日志,并启用这些日志完整的请求/响应数据。 它会给你一些与此类似:


    (32e76dc1-2e80-11e9-b849-1d4cbf2f1403) Endpoint request body after transformations: {"resource":"/testproxy","path":"/testproxy","httpMethod":"GET","headers":[TRUNCATED]
    (32e76dc1-2e80-11e9-b849-1d4cbf2f1403) Sending request to [TRUNCATED]

我截断简洁但你可以看到路径定义在那里。 这将是看那个来缩小错误的始发有用。



文章来源: AWS API Gateway Method request path parameter not working