pulkit
pulkit

Reputation: 87

How do i pass a string as payload to aws lambda function using aws cli?

I am trying to use the below aws cli command to invoke the lambda function.

aws lambda invoke  --invocation-type RequestResponse  --function-name HelloWorldJava  --payload \"world\" outputfile.txt

But Iam getting below error.

Invalid base64: ""world""

The book I am referring to is using the same method.

Upvotes: 3

Views: 4128

Answers (3)

cryptomamy
cryptomamy

Reputation: 67

Configurations

  • Mac OS
  • VS code

Error

--payload

The JSON that you want to provide to your Lambda function as input.

You can enter the JSON directly. For example, --payload '{ "key": "value" }' . You can also specify a file path. For example, --payload file://payload.json .

source

Correction

you need to clarify with this command:

--cli-binary-format raw-in-base64-out

Method

Here is the correct syntax:

aws lambda invoke \
> --invocation-type RequestResponse \
> --function-name HelloWorldJava \
> --cli-binary-format raw-in-base64-out \
> --payload \"world\" outputfile.txt

Run your code again

Upvotes: 2

Titulum
Titulum

Reputation: 11486

Your payload has to be JSON according to the docs, but I think there is no hard requirement for that. When you want to pass in a string value directly (JSON or otherwise) you also need to be sure that you pass --cli-binary-format raw-in-base64-out.

So you command should be:

aws lambda invoke  --invocation-type RequestResponse  --function-name HelloWorldJava --cli-binary-format raw-in-base64-out --payload \"world\" outputfile.txt

Upvotes: 7

ZabielskiGabriel
ZabielskiGabriel

Reputation: 600

You need to pass payload as a json-string - https://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html#examples

--payload (blob)

The JSON that you want to provide to your Lambda function as input.

You can enter the JSON directly. For example, --payload '{ "key": "value" }' . You can also specify a file path. For example, --payload file://payload.json .

aws lambda invoke \
    --function-name my-function \
    --payload '{ "name": "Bob" }' \
    response.json

Upvotes: 0

Related Questions