AWS API 网关方法请求路径参数不起作用 [英] AWS API Gateway Method request path parameter not working

查看:26
本文介绍了AWS API 网关方法请求路径参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置 API 网关以将请求路由到后端 GO 微服务上的特定路由.我正在使用 GET 请求方法和 VPC_LINKNLB 的集成,该方法路由到我在 Fargate 中运行的后端微服务代码>.这是一个用 GO 编写的简单 REST api.在服务端,在我的处理程序中,我为我的阶段路由放置了一个捕获所有响应,然后为 /nmapscan 路由放置了一个Hello World"响应,这就是我想要点击的.但是,当我尝试使用调用 url 访问我的后端服务时,尽管请求路径在我的请求响应输出中显示正确,但我仍不断收到捕获所有响应.我是 API Gateway 的新手,我觉得我错过了一些简单的东西.此外,当我在本地运行容器时,我附加了一个 shell 并运行 curl localhost:8000/v1/nmapscan 并得到正确的Hello World!"回复.以下是响应、我的配置以及来自流日志的响应:

响应:

user$ curl -v -X GET https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/v1/nmapscan/>获取/v1/nmapscan/HTTP/2>主机:xxxxxxxxxxx.execute-api.us-east-1.amazonaws.com>用户代理:curl/7.54.0>接受: */*>* 连接状态改变(MAX_CONCURRENT_STREAMS 更新)!<HTTP/2 200<内容类型:应用程序/json<内容长度:27<日期:2019 年 9 月 25 日,星期三 20:24:16 GMT<x-amzn-requestid: df90f051-dbdd-405f-a708-73668ad955f1<x-amz-apigw-id:XXXXXXXXXXX=<x-amzn-trace-id:根=1-5d8bccf0-44ebf9c5af13c90e1636de42<x-cache:来自 cloudfront 的小姐<通过:1.1 b7ddb18a56b4bad68ca78b085e9ca451.cloudfront.net (CloudFront)<x-amz-cf-pop:EWR52-C2<x-amz-cf-id:lvT1CGlv2fboFJ5AxE917Jr61Nwb4fQOwbranZ3s_vz0EJULhcwudQ==<* 与主机 xxxxxxxxx.execute-api.us-east-1.amazonaws.com 的连接 #0 完好无损这是捕获所有路径!

如您所见,这将返回捕获所有响应.它应该返回Hello World!".

配置:

资源aws_api_gateway_rest_api"GOAPI"{名称 = "开始"description = "用于 GO API 的 REST API"}资源aws_api_gateway_resource"测试"{rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"parent_id = "${aws_api_gateway_rest_api.GOAPI.root_resource_id}"path_part = "nmapscan"}资源aws_api_gateway_method"测试方法"{rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"resource_id = "${aws_api_gateway_resource.test.id}"http_method = "获取"授权 = "NONE"请求参数 = {"method.request.path.nmapscan" = true}}资源aws_api_gateway_integration"integrationtest"{connection_type = "VPC_LINK"connection_id = "${aws_api_gateway_vpc_link.test.id}"类型 = "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"//}}资源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 = {应用程序/json"=空"}}资源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 = {应用程序/json"="}}资源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"}

流日志:

(fdaa14d3-08df-4847-ba63-a9644a65d265) 转换前的方法请求体:(fdaa14d3-08df-4847-ba63-a9644a65d265) 端点请求 URI:http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000(fdaa14d3-08df-4847-ba63-a9644a65d265) 端点请求标头:{x-amzn-apigateway-api-id=cqq6k2xrw3, Accept=application/json, User-Agent=AmazonAPIGateway_cqq6k2xrw3, Host-xxxbzzyelus-east-1.amazonaws.com, X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14}(fdaa14d3-08df-4847-ba63-a9644a65d265) 转换后的端点请求主体:(fdaa14d3-08df-4847-ba63-a9644a65d265) 发送请求到 http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000(fdaa14d3-08df-4847-ba63-a9644a65d265) 收到回复.状态:200,集成延迟:15 毫秒(fdaa14d3-08df-4847-ba63-a9644a65d265) 端点响应标头:{Content-Type=application/json, Date=Thu, 26 Sep 2019 03:22:22 GMT, Content-Length=27}(fdaa14d3-08df-4847-ba63-a9644a65d265) 转换前的端点响应主体:这是全能路径!(fdaa14d3-08df-4847-ba63-a9644a65d265) 转换后的方法响应体:这是捕获所有路径!(fdaa14d3-08df-4847-ba63-a9644a65d265) 方法响应头:{X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14, Content-Type=application/json}(fdaa14d3-08df-4847-ba63-a9644a65d265) 成功完成执行(fdaa14d3-08df-4847-ba63-a9644a65d265) 方法完成,状态:200

解决方案

您的帖子看起来很像 这个

如果您确信这不是该回复中提到的内容,那么我会检查您的集成请求中的路由.

您的 curl 请求的 URI 是/v1/nmapscan/,这意味着 API Gateway 将查看 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,因为它在集成请求中由于某种原因没有正确映射.

说了这么多,当请求被发送到 NLB 时,查看 URI 是什么的最简单方法是启用 API 网关执行日志,并在这些日志上启用完整的请求/响应数据.它会给你类似的东西:

<前>(32e76dc1-2e80-11e9-b849-1d4cbf2f1403) 转换后的端点请求正文:{"resource":"/testproxy","path":"/testproxy","httpMethod":"GET","headers":[TRUNCATED](32e76dc1-2e80-11e9-b849-1d4cbf2f1403) 向 [TRUNCATED] 发送请求

为简洁起见,我进行了截断,但您可以在那里看到路径定义.查看它以缩小错误的来源将很有用.

I am trying to configure API Gateway to route requests to a specific route on my backend GO microservice. I am using a GET request method with a VPC_LINK integration with an NLB, which routes to my backend microservice running in Fargate. This is a simple REST api written in GO. On the service side, in my handler, I put a catch all response for my stage route and then a "Hello World" response for the /nmapscan route, which is what I am trying to hit. However, when I try to hit my backend service with the invocation url, I keep getting the catch all response, despite the request path appearing correct in my request response output. I am new to API Gateway and I have a feeling I am missing something simple. Also, when I ran the container locally, I attached a shell and ran curl localhost:8000/v1/nmapscan and got the correct "Hello World!" response. Below is the response, my configuration, as well as the response from the flow logs:

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!

As you can see, this is returning the catch all response. It should return "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

解决方案

Your post looks suspiciously like this

If you are convinced it's not the things mentioned in that response, then I would check the routing in your integration request.

The URI of your curl request is /v1/nmapscan/ which means API Gateway will look at STAGE v1, resource /nmapscan. Once that occurs, API GW sends the request to the VPC Link based on the uri configured in the Integration Request. I'm not super familiar with Terraform, but it looks like you are sending it to:

http://aws_lb.myapis.dns_name:8000

I also see that you have "request parameters" defined in terraform(but maybe commented out?):


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

I would check that after deployment, the Integration Request in the API GW Console shows that this is being routed to the proper route. Without knowing for sure, I'm assuming your application path is actually http://aws_lb.myapis.dns_name/nmapscan:8000 and that's not being passed correctly to the NLB because it isn't properly mapping for some reason in the Integration Request.

All of that being said, the easiest way to see what the URI is when the request is being sent to the NLB is to enable API Gateway execution logs, and enable full request/response data on those logs. It will give you something similar to this:


    (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]

I truncated for brevity but you can see the path definition there. It will be useful to look at that to narrow down where the error is originating.

这篇关于AWS API 网关方法请求路径参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆