简体   繁体   English

从VPC中的Lambda访问API网关-请求超时

[英]Access to API Gateway from Lambda in VPC - Request times out

Objective 目的

  • I am using the „new“ API Gateway Websocket service of AWS 我正在使用AWS的“新” API网关Websocket服务
  • I want to connect using a Websocket client to a Lambda function 我想使用Websocket客户端连接到Lambda函数
  • I want to respond to the client using the ApiGatewayManager 我想使用ApiGatewayManager响应客户端
  • Lambda function needs to be in a VPC with my RDS for better security (RDS is not public accessible, except for specified IPs in the security group) Lambda功能需要与我的RDS一起放在VPC中,以提高安全性(RDS不可公共访问,但安全组中的指定IP除外)
  • I am using Node.js 8 in combination with Typescript 我将Node.js 8Typescript结合使用

Approach 途径

I created a regional AWS API Gateway for Websockets and added a Lambda function for $connect , $disconnect and one for the action subscribeChannel . 我创建了一个区域AWS API网关的WebSockets,并增加了一个lambda函数$connect$disconnect以及一个用于操作subscribeChannel

I am able to connect and send messages to connected clients. 我能够连接并向连接的客户端发送消息。

I also created a VPC with 3 private subnets, all of them are located in eu-central-1 each one in a different Availability Zone (AZ). 我还创建了一个具有3个专用子网的VPC,所有这些子网均位于eu-central-1每个子网均位于不同的可用区(AZ)中。

Lambda Functions loose their access to the public internet when you add them to a VPC, therefore one approach is adding another public subnet and adding a NAT Gateway to it. 当您将Lambda函数添加到VPC时,Lambda函数会释放其对公共Internet的访问权限,因此一种方法是添加另一个公共子网并向其中添加NAT网关。

Now I am changing the routing table of the private subnet to delegate 0.0.0.0/0 to the NAT and in the routing table of the public subnet it routes 0.0.0.0/0 to a Internet Gateway. 现在,我正在更改专用子网的路由表,以将0.0.0.0/0委派给NAT,在公用子网的路由表中,它将0.0.0.0/0路由到Internet网关。

This seems to work to get access to the public internet, eg I am able to request https://google.com but the ApiGatewayManagement times out as if cannot resolve the AWS service. 这似乎可以访问公共互联网,例如,我可以请求https://google.com但ApiGatewayManagement超时,好像无法解析AWS服务。

Then I looked into VPC Endpoints, as they are designed to make public AWS Services available in private subnets, without routing through the internet. 然后,我研究了VPC端点,因为它们旨在使公用AWS服务可在专用子网中使用,而无需通过Internet进行路由。 I am able to set it up and receive the private DNS urls. 我能够进行设置并接收私有DNS网址。 But here I am stuck, I do not know how to use it in my setup/code. 但是在这里我被困住了,我不知道如何在我的设置/代码中使用它。

I am managing the whole project using the serverless framework and Cloud Formation resources. 我正在使用无服务器框架和云形成资源来管理整个项目。

Issue 问题

If the action handler for subscribeChannel is associated with the VPC then the request to XXXXXXXX.execute-api.eu-central-1.amazonaws.com/develop times out, as it cannot reach the public internet. 如果操作处理程序subscribeChannel与VPC然后关联请求XXXXXXXX.execute-api.eu-central-1.amazonaws.com/develop超时,因为它不能达到公共互联网。

Is a NAT Gateway the correct approach or do I need to use a VPC Endpoint for execute-api? NAT网关是正确的方法还是我需要将VPC端点用于execute-api? How do I correctly configure the VPC to use this private DNS? 如何正确配置VPC以使用此私有DNS?

Code

async function channelHandler(event, context) {
    return new aws_sdk_1.ApiGatewayManagementApi({
        apiVersion: "2018-11-29",
        endpoint: event.requestContext.domainName + "/" + event.requestContext.stage,
    })
    .postToConnection({
        ConnectionId: event.requestContext.connectionId,
        Data: "Hello, world!",
    }).promise()
    .then(() => {
        return {
            statusCode: 200,
            body: "Sent message!",
        };
    })
    .catch((error) => {
        return {
            statusCode: 500,
            body: JSON.stringify(error),
        };
    });
}

Used resources so far: 到目前为止已使用的资源:

I did recreated the full project from my configuration and "magically" started working. 我确实从我的配置中重新创建了整个项目,并且“神奇地”开始工作。 Therefore I can only make an assumption for what was going on: 因此,我只能假设发生了什么:

Every Availability Zone has a private and a public subnet. 每个可用区都有一个专用和公用子网。 According to the AWS Documentation for NAT Gateways it is necessary to create a NAT Gateway in each of them, but I only had one NAT Gateway in Zone A configured. 根据适用于NAT网关AWS文档,有必要在每个网关中创建一个NAT网关,但是我在A区中仅配置了一个NAT网关。

I changed the configuration of my Lambda functions to be placed in only one AZ (I do not want to have full redundancy until the project is in production) and now the NAT Gateway is in the same one. 我将Lambda函数的配置更改为仅放置在一个可用区中(在项目投入生产之前,我不希望具有完全的冗余),现在NAT网关在同一可用区中。

Simple graph: 简单图:

Client --[AWS]--> Lambda --[Private Subnet]--> NAT --[Public Subnet]--> API Gateway 

I am still wondering if it is possible to reach API Gateway using a VPC Endpoint instead of the API Gateway, but for now I will stay with this configuration. 我仍然想知道是否可以使用VPC端点而不是API网关访问API网关,但是现在我将继续使用此配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VPC下从Lambda访问Api网关 - Access Api Gateway from Lambda under VPC 如何从API网关将请求类型传递给lambda? - How to pass a request type to lambda from API gateway? 从API Gateway和Lambda获取Cognito ID - Getting Cognito ID from API Gateway, and Lambda 从 API-Gateway 检索 lambda 中的数据 - Retrieving data in lambda from API-Gateway 使用 API Gateway 继续在 Lambda 请求上获得 502。 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Keep getting 502 on a Lambda request with API Gateway. No 'Access-Control-Allow-Origin' header is present on the requested resource 结合使用节点请求模块和AWS Lambda和API网关 - Using the Node Request module with AWS Lambda and API Gateway API Gateway CORS 204 error with CORS enabled in API Gateway and 'Access-Control-Allow-Origin: *' in lambda header - API Gateway CORS 204 error with CORS enabled in API Gateway and 'Access-Control-Allow-Origin: *' in lambda header AWS Lambda和API网关-500来自浏览器,但不是来自curl - AWS Lambda & API Gateway - 500 from browser but not from curl “错误请求”通过React Native的签名请求访问API网关 - “Bad request” accessing API Gateway with signed request from React Native 尝试在api网关客户端中从前端添加超时 - Trying to add time out from frontend in api gateway client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM