Last active
February 4, 2019 19:58
-
-
Save AhmedSamy/b8968ecc355c186f7f15fbb28f82c40c to your computer and use it in GitHub Desktop.
Lambda connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Handler(ctx context.Context, payload Payload) (json.RawMessage, error) { | |
log.Print(payload) | |
var dbEndpoint string = "mysql-test.c5alm976yi8s.eu-central-1.rds.amazonaws.com"; | |
var awsRegion string = "eu-central-1"; | |
var dbUser string = "graphql_lambda_resolver"; | |
var dbName string = "webapp"; | |
var dbPort string = "3306"; | |
awsCreds:= credentials.NewEnvCredentials() | |
authToken, err := rdsutils.BuildAuthToken(dbEndpoint, awsRegion, dbUser, awsCreds) | |
// Create the MySQL DNS string for the DB connection | |
dnsStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?tls=true&allowCleartextPasswords=1", | |
dbUser, authToken, dbEndpoint,dbPort, dbName, | |
) | |
driver := mysql.MySQLDriver{} | |
_ = driver | |
log.Print(dnsStr); | |
// Use db to perform SQL operations on database | |
db, err := sql.Open("mysql", dnsStr) | |
if err != nil { | |
log.Print("Cannot open db", err) | |
} | |
err = db.Ping() | |
if err != nil { | |
log.Print("Cannot ping db", err) | |
} | |
log.Print("Connection established") | |
// Execute the query | |
rows, err := db.Query(payload.Query) | |
return getJSON(rows) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment