4
votes

I am trying to connect to a AWS RDS PosgreSQL database from a lambda in Go.

The authToken seems good in the logs.

creds := credentials.NewEnvCredentials()
authToken, err := rdsutils.BuildAuthToken("something.eu-west-3.rds.amazonaws.com", "eu-west-3", "Username", creds)

if err != nil {
    fmt.Println("Cannot create db auth token")
    return response.Status(500), err
}

// Create the DNS string for the DB connection
// // user:password@protocol(endpoint:port)/dbname?<params>
dnsStr := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=verify-full",
    "Username", authToken, "something.eu-west-3.rds.amazonaws.com", "table_name",
)
db, err := sql.Open("postgres", dnsStr)

if err != nil {
    fmt.Println("Cannot open db")
    return response.Status(500), err
}

err = db.Ping()

if err != nil {
    fmt.Println("Cannot ping db")
    return response.Status(500), err
}

fmt.Println("Connection established")

I have an error on the db.Ping(), and in the logs I got

Cannot ping db
dial tcp: address tcp/something.eu-west-3.rds.amazonaws.com: unknown port: OpError
null

I use the pq library, imported as _ "github.com/lib/pq"

I have tried adding the default port 5432 in the url after the host, but with no results.

I'm sure I'm missing a stupid key point, plz halp

1

1 Answers

3
votes

You need to escape path-special variables when your create connection URLs by hand:

dnsStr := fmt.Sprintf("postgres://%s:%s@%s/testdb",
    "rdsadmin", url.PathEscape(authToken), "something.eu-west-3.rds.amazonaws.com")

Also it might be useful to print the connection-ping error:

fmt.Printf("Cannot ping db: %s\n", err)