-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsapp.go
36 lines (30 loc) · 1014 Bytes
/
awsapp.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
package awsapp
import (
"context"
"fmt"
"time"
"github.com/94DanielBrown/awsapp/pkg/dynamo"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
// InitDynamo creates a dynamodb table with specified name using AWS credentials from env variables.
func InitDynamo(ctx context.Context, tableName string) (*dynamodb.Client, string, error) {
client, err := dynamo.Connect()
if err != nil {
return nil, "", fmt.Errorf("error connecting to dynamodb: %w", err)
}
exists, err := dynamo.Exists(ctx, client, tableName)
if err != nil {
return nil, "", fmt.Errorf("error checking if dynamodb table exists: %w", err)
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
if !exists {
err = dynamo.Create(ctx, client, tableName)
if err != nil {
return nil, "", fmt.Errorf("error creating dynamodb table: %w", err)
}
return client, fmt.Sprintf("table %v created successfully", tableName), nil
} else {
return client, fmt.Sprintf("table %v already exists", tableName), nil
}
}