28

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?

5
  • 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. Commented Apr 2, 2020 at 1:17
  • I've edited post with more details of sending payload on command line. Commented Apr 2, 2020 at 6:28
  • What shell is this? And can you give the output of echo $LC_CTYPE? Commented Apr 2, 2020 at 8:08
  • I've performed $ echo $LC_CTYPE -> en_US.UTF-8 Commented Apr 2, 2020 at 8:54
  • How was this JSON generated? Is this JSON copied from somewhere, from different Operating System? Commented Apr 2, 2020 at 9:56

4 Answers 4

39

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

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

1 Comment

Article from AWS explaining what file and fileb are: aws.amazon.com/blogs/developer/…
19

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

1 Comment

With awscli v2, the payload needs to base64-encoded. Example here.
15

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
    

Comments

0

Also, when payload is with multiple JSON fields, I had after the comma (,) a space and failed to be processed. BAD:

--payload {\"param1\":\"abc\", \"param2\":\"xyz\"}

GOOD:

--payload {\"param1\":\"abc\",\"param2\":\"xyz\"}

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.