简体   繁体   中英

aws lambda: invoke with payload from cli

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:

在此处输入图片说明

Any ideas?

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

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
    

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM