如何修复 AWS Lambda 处理程序和 DynamoDB Put 请求的错误?

huangapple go评论105阅读模式
英文:

How to fix error with AWS Lambda handler, DynamoDB Put req?

问题

尝试创建一个与我的DynamoDB交互的Lambda函数。
这个特定的Lambda函数是用来将一个项目写入DB的:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-lambda-go/lambda"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/aws/session"
  7. "github.com/aws/aws-sdk-go/service/dynamodb"
  8. "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  9. )
  10. type Item struct {
  11. Email string `json:"email"`
  12. Password string `json:"password"`
  13. Rname string `json:"rname"`
  14. }
  15. func Put() error {
  16. // 创建一个会话 - 伦敦地区
  17. session, err := session.NewSession(&aws.Config{
  18. Region: aws.String("eu-west-2")},
  19. )
  20. if err != nil {
  21. fmt.Println(err)
  22. }
  23. svc := dynamodb.New(session)
  24. // 创建Item结构的实例
  25. item := Item{
  26. Email: "123@mail.com",
  27. Password: "12345678",
  28. Rname: "abcde",
  29. }
  30. // 将Item进行编组
  31. av, err := dynamodbattribute.MarshalMap(item)
  32. if err != nil {
  33. fmt.Println("Got error marshalling map:")
  34. fmt.Println(err)
  35. }
  36. // 创建Item
  37. input := &dynamodb.PutItemInput{
  38. Item: av,
  39. TableName: aws.String("accountsTable"),
  40. }
  41. _, err = svc.PutItem(input)
  42. if err != nil {
  43. fmt.Println("Got error calling PutItem:")
  44. fmt.Println(err)
  45. }
  46. return err
  47. }
  48. func main() {
  49. lambda.Start(Put())
  50. }

然而,出现了以下错误:

  1. {
  2. "errorMessage": "handler is nil",
  3. "errorType": "errorString"
  4. }

我已经在运行时设置中将处理程序更改为main,所以不认为这是问题所在。

使用以下命令构建:

  1. GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a main.go

并通过控制台将可执行文件的zip文件放入AWS(没有IAC)。

非常感谢您提供帮助以解决此错误!谢谢。

英文:

Trying to create a Lambda to interact with my DynamoDB.
This specific Lambda is to put/write an item to the DB:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-lambda-go/lambda"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/aws/session"
  7. "github.com/aws/aws-sdk-go/service/dynamodb"
  8. "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  9. )
  10. type Item struct {
  11. Email string `json:"email"`
  12. Password string `json:"password"`
  13. Rname string `json:"rname"`
  14. }
  15. func Put() error {
  16. // Create a session - London Region
  17. session, err := session.NewSession(&aws.Config{
  18. Region: aws.String("eu-west-2")},
  19. )
  20. if err != nil {
  21. fmt.Println(err)
  22. }
  23. svc := dynamodb.New(session)
  24. // Create instance of Item Struct
  25. item := Item{
  26. Email: "123@mail.com",
  27. Password: "12345678",
  28. Rname: "abcde",
  29. }
  30. // Marshall Item
  31. av, err := dynamodbattribute.MarshalMap(item)
  32. if err != nil {
  33. fmt.Println("Got error marshalling map:")
  34. fmt.Println(err)
  35. }
  36. // Create Item
  37. input := &dynamodb.PutItemInput{
  38. Item: av,
  39. TableName: aws.String("accountsTable"),
  40. }
  41. _, err = svc.PutItem(input)
  42. if err != nil {
  43. fmt.Println("Got error calling PutItem:")
  44. fmt.Println(err)
  45. }
  46. return err
  47. }
  48. func main() {
  49. lambda.Start(Put())
  50. }

However getting the error:

  1. {
  2. "errorMessage": "handler is nil",
  3. "errorType": "errorString"
  4. }

I have changed the handler in run time settings to main too so don't think that would be the issue.

Building with:

  1. GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a main.go

and putting the zip of the executable into AWS via console (no IAC's)

Any help would be greatly appreciated to resolve this error! Thanks.

答案1

得分: 1

你需要传递函数句柄而不是函数结果给lambda.Start

请使用以下代码更新你的主函数:

  1. func main() {
  2. lambda.Start(Put)
  3. }
英文:

You need to pass function handle not function result to lambda.Start

Please update your main function with👇

  1. func main() {
  2. lambda.Start(Put)
  3. }

huangapple
  • 本文由 发表于 2022年12月30日 03:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/74955288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定