forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createTableOnDemand.go
79 lines (65 loc) · 1.92 KB
/
createTableOnDemand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"fmt"
)
var table = "Music"
var awsRegion = "us-west-2"
func getSession() (*session.Session) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
// Provide SDK Config options, such as Region and Endpoint
Config: aws.Config{
Region: aws.String(awsRegion),
},
}))
return sess
}
func createTable() error {
dynamoDBClient := dynamodb.New(getSession())
attributeDefinitions := []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String("Artist"),
AttributeType: aws.String("S"),
},
{
AttributeName: aws.String("SongTitle"),
AttributeType: aws.String("S"),
},
}
keySchema := []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String("Artist"),
KeyType: aws.String("HASH"), // Partition Key
},
{
AttributeName: aws.String("SongTitle"),
KeyType: aws.String("RANGE"), // Sort Key
},
}
billingMode := aws.String("PAY_PER_REQUEST")
_, err := dynamoDBClient.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: attributeDefinitions,
KeySchema: keySchema,
BillingMode: billingMode,
TableName: &table,
})
if err != nil {
return err
}
err = dynamoDBClient.WaitUntilTableExists(&dynamodb.DescribeTableInput{
TableName: aws.String(table),
});
if err != nil {
fmt.Println("Got error calling CreateTable:")
return err
}
return nil
}
func main() {
fmt.Println("Creating On-Demand Table ...")
createTable()
fmt.Println("Finished ...")
}