I'm performing a simple table Scan to get all items from my DynamoDB table, products. Here is the whole Lambda:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
type Product struct {
productUUID string
name string
description string
brand string
price string
category string
dateAdded string
}
func handleRequest() (string, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), func(o *config.LoadOptions) error {
o.Region = "us-east-2"
return nil
})
if err != nil {
panic(err)
}
svc := dynamodb.NewFromConfig(cfg)
out, err := svc.Scan(context.TODO(), &dynamodb.ScanInput{
TableName: aws.String("products"),
})
if err != nil {
panic(err)
}
for _, item := range out.Items {
fmt.Println(item)
}
return "Items", nil // Temporary debugging return (always 200). I'd like to return a json object here
}
func main() {
lambda.Start(handleRequest)
}
When I get the data back, I can print out each item in the following form:
map[brand:0xc0002f38a8 category:0xc0002f3848 dateAdded:0xc0002f3830 name:0xc0002f3800 price:0xc0002f37a0 productUUID:0xc0002f3818]
How can I convert these Items into the struct I defined above, and then return the json response that is expected from lambda (basically an HTTP response code and the Items in json form)?