https://serverless-stack.com/ logo
#help
Title
# help
s

Shubham Sinha

05/24/2022, 4:34 AM
Is there a tutorial for using dynamo db and golang lambda functions ? Also are we bound to use aws-sdk-go v1 or aws-sdk-go-v2 can be used ? Followed this https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/dynamo-example-read-table-item.html
Copy code
package main

import (
	"fmt"
	"log"
	"os"

	"<http://github.com/aws/aws-lambda-go/events|github.com/aws/aws-lambda-go/events>"
	"<http://github.com/aws/aws-lambda-go/lambda|github.com/aws/aws-lambda-go/lambda>"
	"<http://github.com/aws/aws-sdk-go/aws|github.com/aws/aws-sdk-go/aws>"
	"<http://github.com/aws/aws-sdk-go/aws/session|github.com/aws/aws-sdk-go/aws/session>"
	"<http://github.com/aws/aws-sdk-go/service/dynamodb|github.com/aws/aws-sdk-go/service/dynamodb>"
	"<http://github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute|github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute>"
)

type Project struct {
	Description string `json:"description"`
}

func Handler(request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))

	svc := dynamodb.New(sess)
	tableName := os.Getenv("table")
	result, err := svc.GetItem(&dynamodb.GetItemInput{
		TableName: aws.String(tableName),
		Key: map[string]*dynamodb.AttributeValue{
			"Project": {
				S: aws.String("Ecommerce"),
			},
		},
	})
	if err != nil {
		log.Fatalf("Got error calling GetItem: %s", err)
	}
	project := Project{}
	err = dynamodbattribute.UnmarshalMap(result.Item, &project)
	if err != nil {
		panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
	}
	return events.APIGatewayProxyResponse{
		Body:       project.Description,
		StatusCode: 200,
	}, nil
}

func main() {
	lambda.Start(Handler)
}
Got error calling GetItem: ValidationException: The provided key element does not match the schema status code: 400, request id: 1VH9CC6VVG7O2TVQSRV9PJSVPNVV4KQNSO5AEMVJF66Q9ASUAAJG
k

Klaus

05/24/2022, 7:11 AM
@Shubham Sinha I don't think this is specific to go SDK, but more related to the key you are providing here
Copy code
Key: map[string]*dynamodb.AttributeValue{
			"Project": {
				S: aws.String("Ecommerce"),
			},
		},
for reference `For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.`` https://dynobase.dev/dynamodb-errors/dynamodb-key-element-does-not-match-the-schema/
If you can't find a solution, perhaps show us how you Table stack looks like.
s

Shubham Sinha

05/24/2022, 1:48 PM
@Klaus thanks. This worked:
Copy code
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"<http://github.com/aws/aws-lambda-go/events|github.com/aws/aws-lambda-go/events>"
	"<http://github.com/aws/aws-lambda-go/lambda|github.com/aws/aws-lambda-go/lambda>"
	"<http://github.com/aws/aws-sdk-go/aws|github.com/aws/aws-sdk-go/aws>"
	"<http://github.com/aws/aws-sdk-go/aws/session|github.com/aws/aws-sdk-go/aws/session>"
	"<http://github.com/aws/aws-sdk-go/service/dynamodb|github.com/aws/aws-sdk-go/service/dynamodb>"
	"<http://github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute|github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute>"
)

type Project struct {
	PK string `dynamodbav:"PK" json:"description"`
	SK string `dynamodbav:"SK" json:"date"`
}

func Handler(request events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))

	svc := dynamodb.New(sess)
	tableName := os.Getenv("table")
	result, err := svc.GetItem(&dynamodb.GetItemInput{
		TableName: aws.String(tableName),
		Key: map[string]*dynamodb.AttributeValue{
			"PK": {
				S: aws.String("Ecommerce"),
			},
			"SK": {
				S: aws.String("24-05-2022 00:00:00"),
			},
		},
	})
	if err != nil {
		log.Fatalf("Got error calling GetItem: %s", err)
	}
	project := Project{}
	err = dynamodbattribute.UnmarshalMap(result.Item, &project)
	if err != nil {
		panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
	}
	b, err := json.Marshal(project)
	if err != nil {
		fmt.Println(err)
		panic(fmt.Sprintf("Failed to unmarshal Record, %v", err))
	}
	return events.APIGatewayProxyResponse{
		Body:       string(b),
		StatusCode: 200,
		Headers:    map[string]string{"Content-Type": "application/json"},
	}, nil
}

func main() {
	lambda.Start(Handler)
}
3 Views