/
create.go
90 lines (70 loc) · 2.17 KB
/
create.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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"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/dynamodbattribute"
"github.com/google/uuid"
"encoding/json"
"fmt"
"os"
)
type Item struct {
Id string `json:"id,omitempty"`
Title string `json:"title"`
Details string `json:"details"`
}
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// Creating session for client
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB client
svc := dynamodb.New(sess)
// New uuid for item id
itemUuid := uuid.New().String()
fmt.Println("Generated new item uuid:", itemUuid)
// Unmarshal to Item to access object properties
itemString := request.Body
itemStruct := Item{}
json.Unmarshal([]byte(itemString), &itemStruct)
if itemStruct.Title == "" {
return events.APIGatewayProxyResponse{StatusCode: 400}, nil
}
// Create new item of type item
item := Item{
Id: itemUuid,
Title: itemStruct.Title,
Details: itemStruct.Details,
}
// Marshal to dynamobb item
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Error marshalling item: ", err.Error())
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
}
tableName := os.Getenv("DYNAMODB_TABLE")
// Build put item input
fmt.Println("Putting item: %v", av)
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}
// PutItem request
_, err = svc.PutItem(input)
// Checking for errors, return error
if err != nil {
fmt.Println("Got error calling PutItem: ", err.Error())
return events.APIGatewayProxyResponse{StatusCode: 500}, nil
}
// Marshal item to return
itemMarshalled, err := json.Marshal(item)
fmt.Println("Returning item: ", string(itemMarshalled))
//Returning response with AWS Lambda Proxy Response
return events.APIGatewayProxyResponse{Body: string(itemMarshalled), StatusCode: 200}, nil
}
func main() {
lambda.Start(Handler)
}