Last active
March 17, 2020 21:50
-
-
Save rts-rob/1f64a4c238228204beb51d315bdf5d5c to your computer and use it in GitHub Desktop.
Setting DynamoDB timeout to 100ms
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 ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"os" | |
"time" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" | |
) | |
const ( | |
DynamoDBTable = "DYNAMODB_TABLE" | |
DynamoDBPrefix = "DYNAMODB_PREFIX" | |
) | |
type dependencies struct { | |
ddb dynamodbiface.DynamoDBAPI | |
table string | |
prefix string | |
} | |
func (d *dependencies) handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
input := dynamodb.GetItemInput{ | |
Key: map[string]*dynamodb.AttributeValue{ | |
"id": { | |
S: aws.String(d.prefix + request.PathParameters[PathParameter]), | |
}, | |
}, | |
TableName: aws.String(d.table), | |
} | |
item, err := d.ddb.GetItem(&input) | |
if err != nil { | |
return events.APIGatewayProxyResponse{}, err | |
} | |
body, err := json.Marshal(item.Item) | |
if err != nil { | |
return events.APIGatewayProxyResponse{}, err | |
} | |
return events.APIGatewayProxyResponse{ | |
Body: string(body), | |
StatusCode: 200, | |
}, nil | |
} | |
func main() { | |
sess := session.Must(session.NewSession()) | |
ddb := dynamodb.New(sess, &aws.Config{ | |
HTTPClient: &http.Client{ | |
Transport: &http.Transport{ | |
DialContext: (&net.Dialer{ | |
Timeout: 100 * time.Millisecond, | |
}).DialContext, | |
}, | |
}, | |
}) | |
d := dependencies{ | |
ddb: ddb, | |
table: os.Getenv(DynamoDBTable), | |
prefix: os.Getenv(DynamoDBPrefix), | |
} | |
fmt.Printf("Started up CustomerReader for table %s and prefix %s\n", d.table, d.prefix) | |
lambda.Start(d.handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment