14
votes

I'm trying to invoke my lambda using aws cli:

$ aws lambda invoke \
    --function-name soc-update-dynamodb-java \
    --invocation-type Event \
    --payload file://invoke-payload.json \
   response.json

However, I'm getting this message:

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Invalid UTF-8 middle byte 0x28 at [Source: (byte[])"E�(�����U�슉���ޞԨ��k.....

payload.json content is a s3 event-like json:

{
"Records": [
  {
    "eventVersion": "2.0",
    "eventSource": "aws:s3",
    "awsRegion": "eu-central-1",
    "eventTime": "1970-01-01T00:00:00.000Z",
    "eventName": "ObjectCreated:Put",
    "userIdentity": {
      "principalId": "EXAMPLE"
    },
    "requestParameters": {
      "sourceIPAddress": "127.0.0.1"
    },
    "responseElements": {
      "x-amz-request-id": "EXAMPLE123456789",
      "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
    },
    "s3": {
      "s3SchemaVersion": "1.0",
      "configurationId": "testConfigRule",
      "bucket": {
        "name": "soc-connect",
        "ownerIdentity": {
          "principalId": "EXAMPLE"
        },
        "arn": "arn:aws:s3:::example"
      },
      "object": {
        "key": "example.key",
        "size": 1024,
        "eTag": "0123456789abcdef0123456789abcdef",
        "sequencer": "0A1B2C3D4E5F678901"
      }
    }
  }
]
}

I'm able to execute it from aws web console using this event, but I have problem trying to invoke it using aws cli.

I've get encoding from invoke-payload.json:

$ file -i invoke-payload.json 
invoke-payload.json: text/plain; charset=us-ascii

As you can see, encoding is us-ascii.

EDIT

I've also tried to send payload embedded on command. As you can see on image, I'm getting the same message:

enter image description here

Any ideas?

3
It works on my end. But they I just copied and pasted the json from SO. Thus I think your original file must have some strange characters somewhere.Marcin
I've edited post with more details of sending payload on command line.Jordi
What shell is this? And can you give the output of echo $LC_CTYPE?franklinsijo
I've performed $ echo $LC_CTYPE -> en_US.UTF-8Jordi
How was this JSON generated? Is this JSON copied from somewhere, from different Operating System?franklinsijo

3 Answers

16
votes

use the fileb:// ("file binary") syntax for the payload parameter so you don't have to run it through base64

... --payload fileb://invoke-payload.json

6
votes

For AWS CLI version 2 add --cli-binary-format flag to make sure the payload interpreted correctly.

$ aws lambda invoke \
    --function-name soc-update-dynamodb-java \
    --invocation-type Event \
    --payload file://invoke-payload.json \
    --cli-binary-format raw-in-base64-out \
    response.json
    
1
votes

invoke Lambda with simple JSON from the CLI

Here's how to test your Lambda by giving it a short JSON payload, and displaying the result in the terminal:

aws lambda invoke \
--payload '{"beer": "tasty"}' --function-name myfunc \
--cli-binary-format raw-in-base64-out \
/dev/stdout

(Building on antklim's answer)