2

I am running a lambda function written in Go using Serverless and I want to pass a couple of parameters to it when it's invoked.

Here's the struct I created to receive the request:

type RequestStruct struct {
    StartAt int `json:"startAt"`
    EndAt   int `json:"endAt"`
}

And in the handler I'm trying to print out the values:

func Handler(ctx context.Context,request RequestStruct) (Response, error) {
    fmt.Printf("Request: %v",request)

I tried invoking it using the --raw option, so I tried doing this

serverless invoke -f orders --raw -d '{"startAt":1533513600,"endAt":1534118399}'

and I tried wrapping it in double quotes instead

serverless invoke -f orders --raw -d "{startAt:1533513600,endAt:1534118399}"

serverless invoke -f orders --raw -d "{\"startAt\":1533513600,\"endAt\":1534118399}"

I received a marshal error with all three:

{
    "errorMessage": "json: cannot unmarshal string into Go value of type main.RequestStruct",
    "errorType": "UnmarshalTypeError"
}

I'm not sure what I am doing wrong and I can find any examples for that online, there's only this serverless doc about how to do the invoke and this aws doc about how to handle the event in Go

Update I tried invoking the event from the AWS Console and it worked, so odds are the issue is in the serverless invoke command.

6
  • So did you used aws lambda invoke --function-name CLI instead of serverless? Commented Sep 17, 2018 at 0:26
  • @DeepakSingh no, just through the console's UI I went to the lambda function and ran a test with the same json values Commented Sep 17, 2018 at 0:28
  • Okay, Did you tried aws lambda invoke? Refer docs.aws.amazon.com/lambda/latest/dg/…. Commented Sep 17, 2018 at 0:35
  • 1
    I'd rather stick to the serverless framework because it'll require less maintenance as it'll pull the data from the same yml file that's being used to deploy the function Commented Sep 17, 2018 at 0:39
  • 1
    Seems like a bug with serverless invoke command , similar to reported issue link Commented Sep 17, 2018 at 3:01

2 Answers 2

1

I found a way around this by having my JSON in a file rather than in the command itself, this doesn't solve the issue I'm experiencing in the question but it's a way to invoke the function with Json

I added a events/startAndEnd.json file that contains my json data:

{
    "startAt":1533513600,
    "endAt":1534118399
}

And referenced that file in the invoke command: serverless invoke -f orders --path events/startAndEnd.json

Sign up to request clarification or add additional context in comments.

Comments

0

Incase you hit this issue when running the command via npm. I also had a similar error when invoking it with:

    "invoke": "serverless invoke --function myfunction --data \"{ \"Records\": []}\"",

By changing the double quotes to single quotes on the data it then suddenly started working:

    "invoke": "serverless invoke --function myfunction --data '{ \"Records\": []}'",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.