Last active
May 2, 2022 10:26
-
-
Save wattanakorn495/00c512680b71f0bfe17c197f3804097d to your computer and use it in GitHub Desktop.
Golang with dynamodb example
This file contains 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 ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"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" | |
) | |
var svc *dynamodb.DynamoDB | |
func init() { | |
session := session.Must(session.NewSessionWithOptions(session.Options{ | |
SharedConfigState: session.SharedConfigEnable, | |
})) | |
svc = dynamodb.New(session) | |
} | |
type MyEvent struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
} | |
func GetRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
var event MyEvent | |
err := json.Unmarshal([]byte(request.Body), &event) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
fmt.Println(event) | |
result, err := dynamodbattribute.MarshalMap(event) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
input := &dynamodb.GetItemInput{ | |
Key: result, | |
TableName: aws.String("Connection-685e604"), | |
} | |
output, err := svc.GetItem(input) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
fmt.Println(output.Item) | |
type Output struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
} | |
var output2 Output | |
err = dynamodbattribute.UnmarshalMap(output.Item, &output2) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
fmt.Println("output2: ", output2) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 200, | |
Body: output2.Name, | |
}, nil | |
} | |
func UpdateRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
var event MyEvent | |
err := json.Unmarshal([]byte(request.Body), &event) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
fmt.Println(event) | |
result, err := dynamodbattribute.MarshalMap(event) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
input := &dynamodb.PutItemInput{ | |
TableName: aws.String("Connection-685e604"), | |
Item: result, | |
} | |
_, err = svc.PutItem(input) | |
if err != nil { | |
fmt.Println(err) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 500, | |
}, nil | |
} | |
// fmt.Println(output.Attributes) | |
// type Output struct { | |
// ID string `json:"id"` | |
// Name string `json:"name"` | |
// } | |
// var output2 Output | |
// err = dynamodbattribute.UnmarshalMap(output.Item, &output2) | |
// if err != nil { | |
// fmt.Println(err) | |
// return events.APIGatewayProxyResponse{ | |
// StatusCode: 500, | |
// }, nil | |
// } | |
// fmt.Println("output2: ", output2) | |
return events.APIGatewayProxyResponse{ | |
StatusCode: 200, | |
// Body: output2.Name, | |
}, nil | |
} | |
func main() { | |
lambda.Start(UpdateRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment