Shubham Sinha
05/24/2022, 4:34 AMpackage 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: 1VH9CC6VVG7O2TVQSRV9PJSVPNVV4KQNSO5AEMVJF66Q9ASUAAJGKlaus
05/24/2022, 7:11 AMKey: 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/Klaus
05/24/2022, 7:12 AMShubham Sinha
05/24/2022, 1:48 PMpackage 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)
}