aws Verified current stable Not installed? Cloud Infrastructure

Aws / Run Lambda Function With Payload

Run Lambda Function With Payload

Invoke an AWS Lambda function with a provided payload and save the response to a file.

$
Terminal
aws lambda invoke --function-name <name> --payload <json> <path/to/response.json>

When To Use

During development when multiple test payloads are used to validate function behavior.

Pro Tip

Check IAM permissions for the function; the execution role must have necessary access to resources referenced in the payload.

Command Builder

Tune the command before you copy it

Back to syntax
$
Generated Command
aws lambda invoke --function-name <name> --payload <json> <path/to/response.json>

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
{"StatusCode": 200, "Payload": "{\"result\": \"success\"}"}

Anatomy of Output

Understanding the result

FunctionName: my-function Function Name

The invoked Lambda function's name.

StatusCode: 200 HTTP Status Code

Confirms successful function execution.

Payload: {"output": "Test successful"} Response Payload

Displays the output returned from the Lambda function.

Power User Variants

Optimized versions

aws lambda invoke --function-name my-function --payload '{"key": "value"}' --invocation-type Event

Invoke function asynchronously.

Troubleshooting

Common pitfalls

An error occurred (ResourceNotFoundException) when calling the Invoke operation: Function not found: my-function

Solution: Check for correct Lambda function name and region.

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Unsupported media type in request

Solution: Ensure the content type is application/json for the payload.

An error occurred (ServiceException) when calling the Invoke operation: Lambda function is throttled

Solution: Reduce invocation rate or request more concurrent executions in AWS.

Command Breakdown

What each part is doing

aws
Base Command
The executable that performs this operation. Here it runs Aws before the shell applies any redirect operators.
<name>
name
The value supplied for name.
<json>
json
The value supplied for json.
<path/to/response.json>
path to response.json
The value supplied for path to response.json.
--function-name
Command Option
Tool-specific option used by this command invocation.
--payload
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Replace {{name}} with your Lambda function's name.

  2. Step 2

    Run the command: aws lambda invoke --function-name <function_name> --payload <json_payload> response.json

  3. Step 3

    Check response.json for the output and StatusCode.

Alternative Approaches

Comparable commands in other tools

Alternative cloud infrastructure tools for the same job.