Amazon Lambda function errors in PowerShell - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Lambda function errors in PowerShell

When your code raises an error, Lambda generates a JSON representation of the error. This error document appears in the invocation log and, for synchronous invocations, in the output.

This page describes how to view Lambda function invocation errors for the PowerShell runtime using the Lambda console and the Amazon CLI.


Consider the following PowerShell script example statement:

throw 'The Account is not found'

When you invoke this Lambda function, it throws a terminating error, and Amazon Lambda returns the following error message:

{ "errorMessage": "The Account is not found", "errorType": "RuntimeException" }

Note the errorType is RuntimeException, which is the default exception thrown by PowerShell. You can use custom error types by throwing the error like this:

throw @{'Exception'='AccountNotFound';'Message'='The Account is not found'}

The error message is serialized with errorType set to AccountNotFound:

{ "errorMessage": "The Account is not found", "errorType": "AccountNotFound" }

If you don't need an error message, you can throw a string in the format of an error code. The error code format requires that the string starts with a character and only contain letters and digits afterwards, with no spaces or symbols.

For example, if your Lambda function contains the following:

throw 'AccountNotFound'

The error is serialized like this:

{ "errorMessage": "AccountNotFound", "errorType": "AccountNotFound" }

How it works

When you invoke a Lambda function, Lambda receives the invocation request and validates the permissions in your execution role, verifies that the event document is a valid JSON document, and checks parameter values.

If the request passes validation, Lambda sends the request to a function instance. The Lambda runtime environment converts the event document into an object, and passes it to your function handler.

If Lambda encounters an error, it returns an exception type, message, and HTTP status code that indicates the cause of the error. The client or service that invoked the Lambda function can handle the error programmatically, or pass it along to an end user. The correct error handling behavior depends on the type of application, the audience, and the source of the error.

The following list describes the range of status codes you can receive from Lambda.


A 2xx series error with a X-Amz-Function-Error header in the response indicates a Lambda runtime or function error. A 2xx series status code indicates that Lambda accepted the request, but instead of an error code, Lambda indicates the error by including the X-Amz-Function-Error header in the response.


A 4xx series error indicates an error that the invoking client or service can fix by modifying the request, requesting permission, or by retrying the request. 4xx series errors other than 429 generally indicate an error with the request.


A 5xx series error indicates an issue with Lambda, or an issue with the function's configuration or resources. 5xx series errors can indicate a temporary condition that can be resolved without any action by the user. These issues can't be addressed by the invoking client or service, but a Lambda function's owner may be able to fix the issue.

For a complete list of invocation errors, see InvokeFunction errors.

Using the Lambda console

You can invoke your function on the Lambda console by configuring a test event and viewing the output. The output is captured in the function's execution logs and, when active tracing is enabled, in Amazon X-Ray.

To invoke a function on the Lambda console
  1. Open the Functions page of the Lambda console.

  2. Choose the function to test, and choose Test.

  3. Under Test event, select New event.

  4. Select a Template.

  5. For Name, enter a name for the test. In the text entry box, enter the JSON test event.

  6. Choose Save changes.

  7. Choose Test.

The Lambda console invokes your function synchronously and displays the result. To see the response, logs, and other information, expand the Details section.

Using the Amazon Command Line Interface (Amazon CLI)

The Amazon CLI is an open-source tool that enables you to interact with Amazon services using commands in your command line shell. To complete the steps in this section, you must have the following:

When you invoke a Lambda function in the Amazon CLI, the Amazon CLI splits the response into two documents. The Amazon CLI response is displayed in your command prompt. If an error has occurred, the response contains a FunctionError field. The invocation response or error returned by the function is written to an output file. For example, output.json or output.txt.

The following invoke command example demonstrates how to invoke a function and write the invocation response to an output.txt file.

aws lambda invoke \ --function-name my-function \ --cli-binary-format raw-in-base64-out \ --payload '{"key1": "value1", "key2": "value2", "key3": "value3"}' output.txt

The cli-binary-format option is required if you're using Amazon CLI version 2. To make this the default setting, run aws configure set cli-binary-format raw-in-base64-out. For more information, see Amazon CLI supported global command line options.

You should see the Amazon CLI response in your command prompt:

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

You should see the function invocation response in the output.txt file. In the same command prompt, you can also view the output in your command prompt using:

cat output.txt

You should see the invocation response in your command prompt.

Lambda also records up to 256 KB of the error object in the function's logs. For more information, see Amazon Lambda function logging in PowerShell.

Error handling in other Amazon services

When another Amazon service invokes your function, the service chooses the invocation type and retry behavior. Amazon services can invoke your function on a schedule, in response to a lifecycle event on a resource, or to serve a request from a user. Some services invoke functions asynchronously and let Lambda handle errors, while others retry or pass errors back to the user.

For example, API Gateway treats all invocation and function errors as internal errors. If the Lambda API rejects the invocation request, API Gateway returns a 500 error code. If the function runs but returns an error, or returns a response in the wrong format, API Gateway returns a 502 error code. To customize the error response, you must catch errors in your code and format a response in the required format.

We recommend using Amazon X-Ray to determine the source of an error and its cause. X-Ray allows you to find out which component encountered an error, and see details about the errors. The following example shows a function error that resulted in a 502 response from API Gateway.

          Trace map for a function error with API Gateway.

For more information, see Using Amazon Lambda with Amazon X-Ray.

What's next?