Created
August 19, 2016 03:42
Connect local dynamodb using Golang
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
package main | |
import ( | |
"log" | |
"net/http" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
) | |
type appContext struct { | |
sess *session.Session | |
dbSvc *dynamodb.DynamoDB | |
} | |
func init() { | |
log.SetPrefix("appxxx => ") | |
log.SetFlags(log.Lmicroseconds | log.Lshortfile) | |
valid.SetFieldsRequiredByDefault(true) | |
} | |
func main() { | |
sess, err := session.NewSession(&aws.Config{ | |
Region: aws.String("us-west-2"), | |
Endpoint: aws.String("http://localhost:8000")}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
dbSvc := dynamodb.New(sess) | |
result, err := dbSvc.ListTables(&dynamodb.ListTablesInput{}) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
log.Println("Tables:") | |
for _, table := range result.TableNames { | |
log.Println(*table) | |
} | |
router := NewRouter(&appContext{sess: sess, dbSvc: dbSvc}) | |
// Start web server | |
log.Println("Starting www...") | |
log.Fatal(http.ListenAndServe(":9001", router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @Tamal!
When the Docker container isn't running, the code blocks indefinitely at
dbSvc.ListTables
. As a Go beginner, I first had to figure out how to configure a timeout, because I couldn't find anything about timeout configuration in the SDK documentation. In case others are interested in the solution: