Last updated: Feb 26, 2024
Reading time·5 min

To invoke a Lambda function synchronously, use the invoke command, passing
it the function name, the payload and the output filename as parameters.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Smith"}' response.json

If you're on Windows, escape the double quotes of the --payload parameter,
e.g. '{\"name\": \"John Smith\"}'.
We passed the following parameters to the invoke command:
| Name | Description |
|---|---|
| function-name | the name of the Lambda function |
| cli-binary-format | by default, AWS CLI v2 takes base64 input, but we're passing a raw JSON string |
| payload | the event the function gets invoked with |
| response.json | an arbitrary file name where the function's response should be stored |
The --payload parameter is the event the Lambda function gets invoked with.
response.json is a file on the local filesystem where we redirect the function's response.Here is the code for the Node.js Lambda function I'll invoke with the next command.
exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify(`Hello ${event.name}!`), }; return response; };
By running the command, I get the following output (stored in a response.json
file).

Sometimes the payload is too complex to be directly passed using the CLI.
To invoke a Lambda function, passing it a file as a payload:
event your Lambda function expects.--payload parameter.First, create a file called event.json with the following JSON in it.
{ "name": "John Doe" }
Open your terminal in the directory where you stored the event.json file.
Then invoke the Lambda function, passing it the event.json file as the
--payload.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json

--parameters to an AWS CLI command, prefix the path with file:// for human-readable files, or with fileb:// for binary (non human-readable) files.Take a look at the response.json file to make sure the response matches your
expectations.

statusCode the AWS CLI responds with is not the status code from the function's response. The statusCode property of the CLI response relates to permission, limit or configuration errors.For instance, if I update the code of my Lambda function to respond with a 400
status code.
exports.handler = async (event) => { const response = { statusCode: 400, body: JSON.stringify(`Hello ${event.name}!`), }; return response; };
The statusCode in the CLI response would still be 200.

However, the response.json file stores the Lambda function's response with
status code of 400.

To invoke a specific version or an alias of a Lambda function using the AWS
CLI, pass the --qualifier parameter to the invoke command.
I've deployed version 2 of the Lambda function. The new version consists of
the following code:
exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify(`Goodbye ${event.name}!`), }; return response; };
Let's invoke version 2 of the Lambda function, passing it the --qualifier
parameter:
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json --qualifier 2 response.json

The response shows that version 2 of the function ran.
When a Lambda function is invoked asynchronously, the CLI doesn't wait for the function's response.
The request is sent to Lambda which queues the events and sends them to the function.
We will invoke a Lambda function that's written in Node.js and contains the following code.
exports.handler = async (event) => { const response = { statusCode: 200, body: JSON.stringify(`Favorite fruit: ${event.fruit}`), }; return response; };
To asynchronously invoke a Lambda function with the AWS CLI, set the
--invocation-type parameter to Event.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --invocation-type Event --payload '{"fruit": "tomato"}' response.json

If you are on Windows, you must escape the double quotes in the JSON string,
e.g. '{\"fruit\": \"tomato\"}'.
We set the --invocation-type parameter to Event to mark the invocation as
asynchronous.
The response.json file where the Lambda function's response is stored is empty
because the CLI returns instantly and doesn't wait for the function to respond.
We also set the --cli-binary-format parameter to raw-in-base64-out to be
able to pass a raw JSON string as --payload.
All the other scenarios are the same as invoking a Lambda function synchronously:
json in a file on your
local file system and prefix the --payload parameter with file://Here is the event.json file that I'll use for the next command.
{ "fruit": "avocado" }
Open your terminal in the directory where the event.json file is stored and
run the invoke command:
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --invocation-type Event --payload file://event.json response.json

--qualifier parameter to specify the version of the function you want to
invokeaws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --invocation-type Event --qualifier 2 --payload file://event.json response.json

I've also written a detailed guide on how to create a lambda function using the AWS CLI.
You can learn more about the related topics by checking out the following tutorials: