简体   繁体   English

数据库连接挂在 AWS Lambda 上

[英]Database connection hanging on AWS Lambda

I am trying to do basic db connection in AWS Lambda with Go, and for some reason, it got stuck at db.prepare() and no log is presented in cloudwatch.我正在尝试使用 Go 在 AWS Lambda 中进行基本的数据库连接,由于某种原因,它卡在了 db.prepare() 并且 cloudwatch 中没有显示任何日志。

func Handler(request Request) (Response, error) {

    db, err := sql.Open("mysql", dbUsername+":"+dbPassword+"@tcp("+dbURL+":"+dbPort+")/"+dbName)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    stmt, err := db.Prepare("SELECT id, password FROM package_passwords WHERE password = ?")
    return Response{
        Message: "rows",
        Ok:      false,
    }, nil
}

weird thing is that the code above stuck at least more than 5 second in AWS Lambda while it works fine in plain go run/ go build.奇怪的是,上面的代码在 AWS Lambda 中停留了至少超过 5 秒,而它在普通的 go run/go build 中运行良好。

It's probably the AWS security groups可能是 AWS 安全组

The lambda tries to connect but the security groups time it out as they block it forever lambda 尝试连接,但安全组超时,因为他们永远阻止它

Attach the AWSLambdaVPCAccessExecutionRole policy to your lambda and ensure the Lambda is in a VPC.将 AWSLambdaVPCAccessExecutionRole 策略附加到您的 lambda 并确保 Lambda 在 VPC 中。 Check the database security groups allow access from the VPC检查数据库安全组是否允许从 VPC 访问

There are some more pointers in this question Allow AWS Lambda to access RDS Database这个问题Allow AWS Lambda to access RDS Database中还有一些提示

If it is a RDS operation, I think it is getting timeout.如果是 RDS 操作,我认为它正在超时。 The default timeout of a lambda function is 6 seconds and therefore it is getting timeout before the operation finishes with RDS. lambda 函数的默认超时为 6 秒,因此在使用 RDS 完成操作之前它会超时。 To avoid this you do not have to increase the timeout.为避免这种情况,您不必增加超时。 Instead of that you can make "callBackWaitsForEmptyEventLoop" false as your first line of code in lambda function.相反,您可以将“callBackWaitsForEmptyEventLoop”设为 false 作为 lambda 函数中的第一行代码。

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

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