Running an AWS Lambda Command from Windows Shells

I have been using Linux as my primary OS for quite a while and was surprised when the simple aws lambda invoke command didn’t work in Windows 10.

In Linux, I have been using -

aws lambda invoke --function-name PersonToUpper --cli-binary-format raw-in-base64-out --payload '{ "firstName": "alan", "lastName": "adams" }' response.json

It executes, gives a status, and creates the response.json file.

{
    "StatusCode": 200,
    "ExecutedVersion": "$LATEST"
}

Command Prompt

If I try the above in the command prompt I get -

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

Unknown options: lastName:, adams, }', response.json, alan,

A bit of escaping is needed, and note the " opening and closing the Json -

aws lambda invoke --function-name PersonToUpper --cli-binary-format raw-in-base64-out --payload "{ \"firstName\": \"alan\", \"lastName\": \"adams\" }" response.json

When you are serializing to string in the Lambda function (like in the lambda.EmptyFunction template), here is how it should be called -

aws lambda invoke --function-name EmptyFunction  --cli-binary-format raw-in-base64-out --payload "\"hello\"" response.json

Now it works.

PowerShell

If I run the original command that works Linux in PowerShell I get a different error -

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Could not parse payload into json: Unexpected character ('f' (code 102)): was expecting double-quote to start field name
 at [Source: (byte[])"{ firstName: alan, lastName: adams }"; line: 1, column: 4]

The escaping I used in the Command Prompt doesn’t work, the double quote at the start of the Json must be changed to a single quote.

aws lambda invoke --function-name PersonToUpper --cli-binary-format raw-in-base64-out --payload '{ \"firstName\": \"alan\", \"lastName\": \"adams\" }' response.json

When you are serializing to string in the Lambda function (like in the lambda.EmptyFunction template), here is how it should be called -

aws lambda invoke --function-name EmptyFunction  --cli-binary-format raw-in-base64-out --payload '\"hello\"' response.json

WSL Ubuntu and Git Bash

The original command from Linux works fine in both of these.

aws lambda invoke --function-name PersonToUpper --cli-binary-format raw-in-base64-out --payload '{ "firstName": "alan", "lastName": "adams" }' response.json

When you are serializing to string in the Lambda function (like in the lambda.EmptyFunction template), here is how it should be called -

aws lambda invoke --function-name EmptyFunction  -cli-binary-format raw-in-base64-out --payload '"hello"' response.json

dotnet lambda invoke-function

Another option is to use the AWS Extensions for .NET CLI including the Lambda tools.

Install the Lambda tools using -

dotnet tool install -g Amazon.Lambda.Tools

For the Lambda function that takes a JSON payload the payload varies by shell. For bash, and command prompt use -

dotnet lambda invoke-function PersonToUpper -p "{ \"firstName\": \"Alan\", \"lastName\": \"Adams\" }"

For PowerShell use -

dotnet lambda invoke-function PersonToUpper -p '{ \"firstName\": \"Alan\", \"lastName\": \"Adams\" }'

But for the Lambda function that takes only a string, it is the same for all three shells -

dotnet lambda invoke-function EmptyFunction -p "hello"

When the object to serialize to is a string

When invoking the Lambda with the dotnet lambda invoke-function, the escaping is different from above.

If you using the lambda.EmptyFunction template use this -

dotnet lambda invoke-function EmptyFunction --payload ""hello""

And for PowerShell with the lambda.EmptyFunction template use -

dotnet lambda invoke-function EmptyFunction --payload '""hello""' 

comments powered by Disqus

Related