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

Time:12-30

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

package main

import (
    "fmt"

    "github.com/aws/aws-lambda-go/lambda"
    "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"
)

type Item struct {
    Email    string `json:"email"`
    Password string `json:"password"`
    Rname    string `json:"rname"`
}

func Put() error {

    // Create a session - London Region
    session, err := session.NewSession(&aws.Config{
        Region: aws.String("eu-west-2")},
    )
    if err != nil {
        fmt.Println(err)
    }

    svc := dynamodb.New(session)

    // Create instance of Item Struct
    item := Item{
        Email:    "[email protected]",
        Password: "12345678",
        Rname:    "abcde",
    }

    // Marshall Item
    av, err := dynamodbattribute.MarshalMap(item)

    if err != nil {
        fmt.Println("Got error marshalling map:")
        fmt.Println(err)
    }

    // Create Item
    input := &dynamodb.PutItemInput{
        Item:      av,
        TableName: aws.String("accountsTable"),
    }

    _, err = svc.PutItem(input)

    if err != nil {
        fmt.Println("Got error calling PutItem:")
        fmt.Println(err)
    }
    return err
}

func main() {
    lambda.Start(Put())
}

However getting the error:

{
  "errorMessage": "handler is nil",
  "errorType": "errorString"
}

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

Building with:

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.

CodePudding user response:

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

Please update your main function with

  • Related