Skip to content

A SQL Driver for connecting to AWS RDS instances via IAM roles

License

Notifications You must be signed in to change notification settings

labd/go-rds-iam

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RDS IAM SQL Driver for Go

Usage

Make sure you have set the env var AWS_REGION to the region you want to connect to.

Example

package main

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/xo/dburl"

	_ "github.com/labd/go-rds-iam/rdsiam"
)

// Register the custom schema with dburl
func init() {
	dburl.Register(dburl.Scheme{
		Driver:    "postgresql-rdsiam",
		Generator: dburl.GenPostgres,
		Transport: dburl.TransportUnix,
		Opaque:    false,
		Aliases:   []string{"postgresql-rdsiam"},
		Override:  "",
	})
}

func main() {
	db, err := dburl.Open("postgresql-rdsiam://iam-role@rds-server/my-database")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	ctx := context.Background()
	rows, err := db.QueryContext(ctx, `SELECT 'foobar' AS one`)
	if err != nil {
		panic(err)
	}

	for rows.Next() {
		var value string
		rows.Scan(&value)
		fmt.Println("Selected:", value)
	}
}

// alternative skips registering the url with dburl
func alternative() {
	dsn, err := dburl.Parse("postgres://iam-role@rds-server/my-database")
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("postgresql-rdsiam", dsn.DSN)
	if err != nil {
		panic(err)
	}
	defer db.Close()
}

About

A SQL Driver for connecting to AWS RDS instances via IAM roles

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages