403 - ERR_BAD_REQUEST:当 Lambda 调用 ApiGateway 端点时请求失败,状态代码为 403

问题描述 投票:0回答:1

我正在尝试从我们的 VPC 的 Lambda 函数调用 Apigateway 上的 RestApi(不是默认的,默认的工作正常)。它抛出 403 错误,我在 CloudWatch 日志或 Apigateway 日志中找不到任何详细信息。我认为我无法从 Lambda 访问 Apigateway。我尝试使用 Apigateway 资源策略来允许我的 VPC 请求,但没有任何改变。

我尝试了以下事情:

  • Endpoint 在 Postman 和 cURL 上工作
  • 没有ApiKey
  • 没有authX、token、bearer
  • 我可以调用任何外部端点
  • 我可以从默认 VPC 调用 Apigateway Endpoint
  • 我们使用 Nat Instance 而不是 NatGateway
  • 问题:从 Lambda 到 Apigateway 端点的请求(get,post)

Lambda/Node.js 代码片段:

const { default: axios } = require('axios')

exports.handler = async (event) => {
  const res = await axios.get(
    'https://XXXX.execute-api.REGION.amazonaws.com/ABC/hello-world/healthcheck',
     {"headers": { "x-api-key": "18yXXXXXXXXXaSI0usGYYYYYY" }})
  console.log(JSON.stringify(res,null,2))
  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
  }

  return response
}

错误响应

    {
   "message":"Request failed with status code 403",
   "name":"AxiosError",
   "config":{
      "transitional":{
         "silentJSONParsing":true,
         "forcedJSONParsing":true,
         "clarifyTimeoutError":false
      },
      "transformRequest":[
         null
      ],
      "transformResponse":[
         null
      ],
      "timeout":0,
      "xsrfCookieName":"XSRF-TOKEN",
      "xsrfHeaderName":"X-XSRF-TOKEN",
      "maxContentLength":-1,
      "maxBodyLength":-1,
      "env":{
         
      },
      "headers":{
         "Accept":"application/json, text/plain, */*",
         "x-api-key":"18yXXXXXXXXXaSI0usGYYYYYY",
         "User-Agent":"axios/0.27.2"
      },
      "method":"get",
      "url":"https://XXXX.execute-api.REGION.amazonaws.com/ABC/hello-world/healthcheck"
   },
   "code":"ERR_BAD_REQUEST",
   "status":403
}
node.js rest aws-lambda aws-api-gateway serverless
1个回答
0
投票

你能解决这个问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.