0

It seems like a very simple thing but my connection times out every time.

Here is the code in the Lambda function (written in Go)

package main

import (
    "database/sql"
    "log"

    _ "github.com/go-sql-driver/mysql"
    // "github.com/aws/aws-sdk-go/service/rds"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(request events.APIGatewayProxyRequest) {

    db, err := sql.Open("mysql", "username:password(my-database-address:3306)/db_name")

    if err != nil {
        log.Print(err.Error())
    }

    defer db.Close()

    err = db.Ping()

    if err != nil {
        log.Print(err.Error())
    }

}

func main() {
    lambda.Start(Handler)
}

Obviously I've removed the real username, password, address, and db-name but I've verified they are all correct (I can connect to the DB through MySQLWorkbench with the same details).

I'm assuming it has to do with permissions or something? My Lambda function does have permission to access full RDS functionality.

I also tried removing the defer db.Close() thinking maybe it's leaving the connection open indefinitely but that didn't seem to change much.

I had it working without the db.Ping since sql.Open doesn't actually create the connection.

I'm new to AWS so I'm having trouble figuring out how to debug this. Where would I find a logged event showing where the blockage is? Or is there something wrong in the connection I'm making here?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Michael MacDonald
  • 413
  • 2
  • 6
  • 17
  • 5
    This is a network configuration issue, not a permissions issue. You would be getting a permissions error, not a timeout, if it was a permissions thing. Is the Lambda function running in the same VPC as the RDS instance? – Mark B Jan 07 '19 at 18:43
  • @MarkB It wasn't, but I did that now and now it doesn't timeout, but after about a minute it gives me "Calling the invoke API action failed with this message: An error occurred and the request cannot be processed." This is the part I'm definitely not getting. What is the desired set up for a Lambda function and RDS server to work together? – Michael MacDonald Jan 07 '19 at 18:52
  • 1
    You may not want to open connections in lambdas. If enough of those lambdas are running concurrently, you'll hit the maximum number of concurrent connections. – jub0bs Jan 07 '19 at 19:22
  • 2
    Not related to your question, but a helpful note: you should store things like database name, host, port, user, password as environment variables rather than having them as string literals in your code. This way you don't have to modify source code for development/deployment/testing and your password won't be checked into source control (git) so it stays private – Henry Woody Jan 07 '19 at 19:52
  • @jubobs You mean the sql.Open()? How would I connect to the database outside of the lambda? It's an APIGateway Post method that is calling this, so it needs to get data from the DB and return it, can I make a connection somewhere else that is accessible by all Lambdas? – Michael MacDonald Jan 07 '19 at 20:05
  • @HenryWoody if I store them as Env variables are they available to all other Lambdas as well? Because that's also something I was wondering about. Otherwise all my Lambdas will end up having that same connection string in them. Are env vars globally accessible by all Lambdas? – Michael MacDonald Jan 07 '19 at 20:06
  • 1
    @OscarWilde I've only set up Lambdas where the environment variables are local only to that specific Lambda. I believe there is a way to share env vars across Lambdas. So either way works. If you're using the console, there is a section called "Environment Variables" right under "Function Code" – Henry Woody Jan 07 '19 at 20:10
  • When using RDS with lambda you should add the following line as your first line within the lambda function to avoid timeout before connecting to the rds, `context.callbackWaitsForEmptyEventLoop = false;` – SamPiy93 Jan 08 '19 at 08:42
  • 1
    @OscarWilde This blog post argues against using connection-based services like RDBMS in lambdas: https://medium.com/@PaulDJohnston/serverless-best-practices-b3c97d551535 – jub0bs Jan 09 '19 at 16:41

0 Answers0