I'm attempting to write a really simple Go function to insert an entry into a DynamoDB table.
I'm following the tutorial provided on the AWS Documentation site, but for some reason, the function dynamodbattribute.MarshalMap is returning an empty map.
The code compiles and runs, but breaks when attempting to insert the record as it can't find the required keys in the map. A little println action shows that the map is empty, even though the struct it's created from isn't.
Any help is appreciated!
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-lambda-go/lambda"
"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"
)
// Item Struct for Fruit Item
type Item struct {
fruitID int
fruitName string
}
/*HandleRequest handles the lambda request.*/
func HandleRequest(ctx context.Context) (string, error) {
// Initialize a session that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials
// and region from the shared configuration file ~/.aws/config.
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// Create DynamoDB client
svc := dynamodb.New(sess)
item := Item{
fruitID: 2,
fruitName: "Orange",
}
fmt.Println(item)
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Got error marshalling new fruit item: ")
fmt.Println(err.Error())
return "", err
}
fmt.Println("av: ", av)
fmt.Println("ln: ", len(av))
fmt.Println("KEY VALUE PAIRS::")
for key, value := range av {
fmt.Println("Key:", key, "Value:", value)
}
tableName := "fruits"
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}
_, err = svc.PutItem(input)
if err != nil {
fmt.Println("Got error inserting new fruit item: ")
fmt.Println(err.Error())
return "", err
}
return "SUCCESS!", nil
}
func main() {
lambda.Start(HandleRequest)
}
Log output from Lambda
START RequestId: 82f120d9-6bba-40d9-9204-136d491dbb88 Version: $LATEST
{2 Orange}
av: map[]
ln: 0
KEY VALUE PAIRS::
Got error inserting new fruit item:
ValidationException: One or more parameter values were invalid: Missing the key fruitId in the item
status code: 400, request id: QUJG8R8EHPO65O1JFVI98PRQ1VVV4KQNSO5AEMVJF66Q9ASUAAJG
ValidationException: One or more parameter values were invalid: Missing the key fruitId in the item
status code: 400, request id: QUJG8R8EHPO65O1JFVI98PRQ1VVV4KQNSO5AEMVJF66Q9ASUAAJG: requestError
null
END RequestId: 82f120d9-6bba-40d9-9204-136d491dbb88