/
create_dynamodb_table.go
63 lines (48 loc) · 1.34 KB
/
create_dynamodb_table.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
package infrastructure
import (
"context"
"errors"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
const DynamoDBYoloConfigTableName = "yolo-configuration-dynamodb-table"
var (
ErrYoloConfigTableAlreadyExists = errors.New("ErrYoloConfigTableAlreadyExists")
)
func CreateDynamoDBTableForYoloConfig(
dynamoDBClient *dynamodb.Client,
) error {
_, err := dynamoDBClient.CreateTable(
context.TODO(),
&dynamodb.CreateTableInput{
AttributeDefinitions: []types.AttributeDefinition{
{
AttributeName: aws.String("ID"),
AttributeType: types.ScalarAttributeTypeS,
},
},
KeySchema: []types.KeySchemaElement{
{
AttributeName: aws.String("ID"),
KeyType: types.KeyTypeHash,
},
},
BillingMode: types.BillingModePayPerRequest,
TableName: aws.String(DynamoDBYoloConfigTableName),
},
)
if err != nil {
var tableExistsError *types.ResourceInUseException
if errors.As(err, &tableExistsError) {
return ErrYoloConfigTableAlreadyExists
}
return err
}
existsWaiter := dynamodb.NewTableExistsWaiter(dynamoDBClient)
maxWaitTime := 5 * time.Minute
return existsWaiter.Wait(context.TODO(), &dynamodb.DescribeTableInput{
TableName: aws.String(DynamoDBYoloConfigTableName),
}, maxWaitTime)
}