darragh_oconnor
darragh_oconnor

Reputation: 131

lambda evoke cli command reading lambda payload differently

I have created an AWS lambda function which I'm triggering using a Windows bat script. The windows bat script takes in the windows environment variable USERNAME as part of the payload.

I have this working successfully on my local with the following syntax:

aws lambda invoke --function-name ConnectDesktops --payload "{\"Name\": \"%USERNAME%\" }" response.json

However, I then pushed this bat file to my end users, and they are encountering errors when executing the same command:

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character ('©' (code 169)): Expected space separating root-level values at [Source: (byte[])"5��F�b����"; line: 1, column: 3]

The environment variable is resolving on their desktop, but still I resolved it manually and tried a few alternatives to the payload:

aws lambda invoke --function-name ConnectDesktops --payload "{\"Name\": \"User1\" }" response.json

aws lambda invoke --function-name ConnectDesktops --payload "{"Name": "User1" }" response.json

aws lambda invoke --function-name ConnectDesktops --payload '{"Name": "User1"}' response.json

aws lambda invoke --function-name ConnectDesktops --payload {"Name": "User1"} response.json

All with the same result or possibly something like

Unknown options: response.json, }'

Again, the initial command is actually working on my own desktop and we are both using Windows 10 and the cmd command line tool.

My AWS lambda function is written in Python 3.8 and starts like this

import boto3
from botocore.exceptions import ClientError

client=boto3.client('ec2')

def lambda_handler(event, context):

    name=event['Name']

Any suggestions would be greatly appreciated.

Upvotes: 5

Views: 3916

Answers (1)

darragh_oconnor
darragh_oconnor

Reputation: 131

Ok so I found the answer.

Of course there had to be a difference between my installation and my colleagues and the difference was the version of Amazon CLI we were using.

I was on V1 where the above command works fine but my colleagues were using V2 where you need to add the parameter --cli-binary-format raw-in-base64-out

You can check what version of the CLI you are using by executing the following command: aws --version

Full working CLI V2 command for reference: aws lambda invoke --function-name ConnectDesktops --payload "{\"Name\": \"%USERNAME%\" }" --cli-binary-format raw-in-base64-out response.json

Upvotes: 8

Related Questions