简体   繁体   English

如何在 VPC 中正确地将 AWS Lambda 连接到 RDS?

[英]How to properly connect AWS Lambda to RDS in VPC?

I am trying to build a graphQL API with Serverless Framework on AWS-Lambda using Apollo-Server-Lambda etc. I need to use a not publicly available PostgreSQL RDS instance.我正在尝试使用Apollo-Server-Lambda等在AWS-Lambda 上使用无服务器框架构建 graphQL API。我需要使用未公开可用的 PostgreSQL RDS 实例。

I can get lambdas up and running and sending back requests when not in VPC.当不在 VPC 中时,我可以启动并运行 lambdas 并发送回请求。 I can get a Postgres RDS Database up and running and connected to PgAdmin (when in publicly available mode).我可以启动并运行 Postgres RDS 数据库并连接到 PgAdmin(在公开可用模式下)。

The problem is once I make the RDS Postgres 'non public' and try to get all these pieces talking together I fail.问题是,一旦我将 RDS Postgres 设为“非公开”并尝试将所有这些内容放在一起讨论,我就会失败。

I have tried multitude of different approaches.我尝试了多种不同的方法。

This is regularly portrayed as magic.这经常被描绘成魔术。 It is wonderfully written https://gist.github.com/reggi/dc5f2620b7b4f515e68e46255ac042a7得很精彩https://gist.github.com/reggi/dc5f2620b7b4f515e68e46255ac042a7

I could not get access to Postgres with my lambdas using this.我无法使用我的 lambdas 访问 Postgres。 So my first question.所以我的第一个问题。

Do I need a NAT gateway for incoming (ingress) api calls to lambdas in a VPC?我是否需要 NAT 网关来对 VPC 中的 lambda 进行传入(入口)api 调用? My current understanding is that maybe I only need a NAT gateway for my lambdas to make outgoing calls to other api's out of aws or things like S3.我目前的理解是,也许我只需要一个 NAT 网关来让我的 lambdas 使用 aws 或 S3 之类的东西向外调用其他 api。 Is this correct?这是正确的吗?

Next up.接下来。 I have made a security group for my lambdas and have added this security group to the inbound list for the security group that was created for RDS.我为我的 lambdas 创建了一个安全组,并将这个安全组添加到为 RDS 创建的安全组的入站列表中。 My understanding is this is how the lambdas should gain access to RDS.我的理解是这就是 lambda 应该如何访问 RDS。 I have not had such luck.我没有这样的运气。 Maybe this is related to public or non public subnets?也许这与公共或非公共子网有关? Maybe this is related to my understanding of the necessity of a NAT?也许这与我对 NAT 必要性的理解有关?

Basically the only visibility I have been able to get is Lambdas timing out after 20 or 30 seconds depending on my limit when they are trying to connect to postgres in private.基本上,我能够获得的唯一可见性是 Lambda 在 20 或 30 秒后超时,具体取决于他们尝试私下连接到 postgres 时的限制。 Cloudwatch logs reveal nothing else. Cloudwatch 日志没有显示其他内容。

Lastly, for now 😂, what is the best way to connect my dev machine to Postgres once it is 'not public'?最后,现在😂,一旦“不公开”,将我的开发机器连接到 Postgres 的最佳方法是什么? I have my machines IP listed for inbound TCP/IP to port 5432 to postgres in the RDS security group but that does not seem to give me the access I was hoping for.我将我的机器 IP 列为入站 TCP/IP 到端口 5432 到 RDS 安全组中的 postgres,但这似乎没有给我我希望的访问权限。 Do I really need a VPN connected to VPC?我真的需要连接到 VPC 的 VPN 吗? Whats the simplest way?最简单的方法是什么?

I have done this tutorial with basic alterations for Postgres https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html我已经完成了对 Postgres https://docs.aws.amazon.com/lambda/latest/dg/vpc-rds.html 的基本更改的本教程

I have read and considered answers from this question & more Allow AWS Lambda to access RDS Database我已经阅读并考虑了这个问题的答案以及更多Allow AWS Lambda to access RDS Database

I have had many success-full deployments with serverless framework with many variations on serverless.yml config to try these options or else I would show a specific one I thought was failing but this is more broadly that I cant seem to grasp exactly how all these VPC, security groups, routing tables etc are supposed to interact.我已经使用无服务器框架进行了许多成功的完整部署,在 serverless.yml 配置上有许多变化来尝试这些选项,否则我会展示一个我认为失败的特定选项,但这更广泛地说,我似乎无法准确掌握所有这些VPC、安全组、路由表等应该交互。

Any help greatly appreciated!非常感谢任何帮助!

Obviously, Lambda needs to be setup to run inside the same VPC, but I'm assuming you already got that.显然,Lambda 需要设置为在同一个 VPC 内运行,但我假设您已经知道了。

You need to:你需要:

  1. Create a security group (SG) and associate it with the Lambda function.创建安全组 (SG) 并将其与 Lambda 函数关联。
  2. Now, open the SG associated with the RDS instance (not the one you created above).现在,打开与 RDS 实例关联的 SG(不是您在上面创建的那个)。
  3. Inside the RDS SG, go to "Inbound" tab and click "Edit"在 RDS SG 内,转到“入站”选项卡并单击“编辑”
  4. Select "PostgreSQL" in the Type column.在类型列中选择“PostgreSQL”。 In the Source column, select "Custom" in the select dropdown and enter the Lambda SG ID in the input text (if you start typing "sg-", it will show you all your SGs).在源列中,在选择下拉列表中选择“自定义”并在输入文本中输入 Lambda SG ID(如果您开始输入“sg-”,它将显示您所有的 SG)。

Does it work?它有效吗?

Make sure your Lambda function is in the VPC, and the security group allows connections from IP addresses within the subnet of the VPC.确保您的 Lambda 函数在 VPC 中,并且安全组允许来自 VPC 子网内 IP 地址的连接。 The amount of available IP addresses is going to affect how many lambda functions can be run concurrently.可用 IP 地址的数量将影响可以同时运行的 lambda 函数的数量。 Also make sure that the Lambda function's role has the ability to describe the VPC (the AWSLambdaVPCAccessExecutionRole policy should do the job for you).还要确保 Lambda 函数的角色能够描述 VPC(AWSLambdaVPCAccessExecutionRole 策略应该为您完成这项工作)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM