I am following this post, so far the simple version is working but the section using AWS dynamodb is not working for me.
I have created an AWS dynamo table and inserted data
aws dynamodb create-table --table-name Services --attribute-definitions AttributeName=Name,AttributeType=S --key-schema AttributeName=Name,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
aws dynamodb put-item --table-name Services --item '{"Name": {"S": "Lets Keep Safe"}, "ID": {"N": "3"}, "CreatedAt": {"S": "2020-11-21"}}'
main.go
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
// Service Names
type Service struct {
Name string `json:"name"`
ID int `json:"id"`
CreatedAt string `json:"created_at"`
}
func show(ID string) (*Service, error) {
// Fetch a specific book record from the DynamoDB database. We'll
// make this more dynamic in the next section.
// ser, err := getItem(ID)
ser, err := getItem("2")
if err != nil {
return nil, err
}
return ser, nil
}
func main() {
lambda.Start(show)
}
db.go
package main
import (
"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"
)
// Declare a new DynamoDB instance. Note that this is safe for concurrent
// use.
var db = dynamodb.New(session.New(), aws.NewConfig().WithRegion("eu-west-1"))
func getItem(ID string) (*Service, error) {
// Prepare the input for the query.
input := &dynamodb.GetItemInput{
TableName: aws.String("services"),
Key: map[string]*dynamodb.AttributeValue{
"ID": {
N: aws.String(ID),
},
},
}
// Retrieve the item from DynamoDB. If no matching item is found
// return nil.
result, err := db.GetItem(input)
if err != nil {
return nil, err
}
if result.Item == nil {
return nil, nil
}
// The result.Item object returned has the underlying type
// map[string]*AttributeValue. We can use the UnmarshalMap helper
// to parse this straight into the fields of a struct. Note:
// UnmarshalListOfMaps also exists if you are working with multiple
// items.
ser := new(Service)
err = dynamodbattribute.UnmarshalMap(result.Item, ser)
if err != nil {
return nil, err
}
return ser, nil
}
Invoking the lambda results in
{"errorMessage":"json: cannot unmarshal object into Go value of type string","errorType":"UnmarshalTypeError"}