Invoke - 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).

Invoke

Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. By default, Lambda invokes your function synchronously (i.e. theInvocationType is RequestResponse). To invoke a function asynchronously, set InvocationType to Event. Lambda passes the ClientContext object to your function for synchronous invocations only.

For synchronous invocation, details about the function response, including errors, are included in the response body and headers. For either invocation type, you can find more information in the execution log and trace.

When an error occurs, your function may be invoked multiple times. Retry behavior varies by error type, client, event source, and invocation type. For example, if you invoke a function asynchronously and it returns an error, Lambda executes the function up to two more times. For more information, see Error handling and automatic retries in Lambda.

For asynchronous invocation, Lambda adds events to a queue before sending them to your function. If your function does not have enough capacity to keep up with the queue, events may be lost. Occasionally, your function may receive the same event multiple times, even if no error occurs. To retain events that were not processed, configure your function with a dead-letter queue.

The status code in the API response doesn't reflect function errors. Error codes are reserved for errors that prevent your function from executing, such as permissions errors, quota errors, or issues with your function's code and configuration. For example, Lambda returns TooManyRequestsException if running the function would cause you to exceed a concurrency limit at either the account level (ConcurrentInvocationLimitExceeded) or function level (ReservedFunctionConcurrentInvocationLimitExceeded).

For functions with a long timeout, your client might disconnect during synchronous invocation while it waits for a response. Configure your HTTP client, SDK, firewall, proxy, or operating system to allow for long connections with timeout or keep-alive settings.

This operation requires permission for the lambda:InvokeFunction action. For details on how to set up permissions for cross-account invocations, see Granting function access to other accounts.

Request Syntax

POST /2015-03-31/functions/FunctionName/invocations?Qualifier=Qualifier HTTP/1.1 X-Amz-Invocation-Type: InvocationType X-Amz-Log-Type: LogType X-Amz-Client-Context: ClientContext Payload

URI Request Parameters

The request uses the following URI parameters.

ClientContext

Up to 3,583 bytes of base64-encoded data about the invoking client to pass to the function in the context object. Lambda passes the ClientContext object to your function for synchronous invocations only.

FunctionName

The name or ARN of the Lambda function, version, or alias.

Name formats
  • Function namemy-function (name-only), my-function:v1 (with alias).

  • Function ARNarn:aws:lambda:us-west-2:123456789012:function:my-function.

  • Partial ARN123456789012:function:my-function.

You can append a version number or alias to any of the formats. The length constraint applies only to the full ARN. If you specify only the function name, it is limited to 64 characters in length.

Length Constraints: Minimum length of 1. Maximum length of 170.

Pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}(-gov)?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-_\.]+)(:(\$LATEST|[a-zA-Z0-9-_]+))?

Required: Yes

InvocationType

Choose from the following options.

  • RequestResponse (default) – Invoke the function synchronously. Keep the connection open until the function returns a response or times out. The API response includes the function response and additional data.

  • Event – Invoke the function asynchronously. Send events that fail multiple times to the function's dead-letter queue (if one is configured). The API response only includes a status code.

  • DryRun – Validate parameter values and verify that the user or role has permission to invoke the function.

Valid Values: Event | RequestResponse | DryRun

LogType

Set to Tail to include the execution log in the response. Applies to synchronously invoked functions only.

Valid Values: None | Tail

Qualifier

Specify a version or alias to invoke a published version of the function.

Length Constraints: Minimum length of 1. Maximum length of 128.

Pattern: (|[a-zA-Z0-9$_-]+)

Request Body

The request accepts the following binary data.

Payload

The JSON that you want to provide to your Lambda function as input.

You can enter the JSON directly. For example, --payload '{ "key": "value" }'. You can also specify a file path. For example, --payload file://payload.json.

Response Syntax

HTTP/1.1 StatusCode X-Amz-Function-Error: FunctionError X-Amz-Log-Result: LogResult X-Amz-Executed-Version: ExecutedVersion Payload

Response Elements

If the action is successful, the service sends back the following HTTP response.

StatusCode

The HTTP status code is in the 200 range for a successful request. For the RequestResponse invocation type, this status code is 200. For the Event invocation type, this status code is 202. For the DryRun invocation type, the status code is 204.

The response returns the following HTTP headers.

ExecutedVersion

The version of the function that executed. When you invoke a function with an alias, this indicates which version the alias resolved to.

Length Constraints: Minimum length of 1. Maximum length of 1024.

Pattern: (\$LATEST|[0-9]+)

FunctionError

If present, indicates that an error occurred during function execution. Details about the error are included in the response payload.

LogResult

The last 4 KB of the execution log, which is base64-encoded.

The response returns the following as the HTTP body.

Payload

The response from the function, or an error object.

Errors

For information about the errors that are common to all actions, see Common Errors.

EC2AccessDeniedException

Need additional permissions to configure VPC settings.

HTTP Status Code: 502

EC2ThrottledException

Amazon EC2 throttled Amazon Lambda during Lambda function initialization using the execution role provided for the function.

HTTP Status Code: 502

EC2UnexpectedException

Amazon Lambda received an unexpected Amazon EC2 client exception while setting up for the Lambda function.

HTTP Status Code: 502

EFSIOException

An error occurred when reading from or writing to a connected file system.

HTTP Status Code: 410

EFSMountConnectivityException

The Lambda function couldn't make a network connection to the configured file system.

HTTP Status Code: 408

EFSMountFailureException

The Lambda function couldn't mount the configured file system due to a permission or configuration issue.

HTTP Status Code: 403

EFSMountTimeoutException

The Lambda function made a network connection to the configured file system, but the mount operation timed out.

HTTP Status Code: 408

ENILimitReachedException

Amazon Lambda couldn't create an elastic network interface in the VPC, specified as part of Lambda function configuration, because the limit for network interfaces has been reached. For more information, see Lambda quotas.

HTTP Status Code: 502

InvalidParameterValueException

One of the parameters in the request is not valid.

HTTP Status Code: 400

InvalidRequestContentException

The request body could not be parsed as JSON.

HTTP Status Code: 400

InvalidRuntimeException

The runtime or runtime version specified is not supported.

HTTP Status Code: 502

InvalidSecurityGroupIDException

The security group ID provided in the Lambda function VPC configuration is not valid.

HTTP Status Code: 502

InvalidSubnetIDException

The subnet ID provided in the Lambda function VPC configuration is not valid.

HTTP Status Code: 502

InvalidZipFileException

Amazon Lambda could not unzip the deployment package.

HTTP Status Code: 502

KMSAccessDeniedException

Lambda couldn't decrypt the environment variables because Amazon KMS access was denied. Check the Lambda function's KMS permissions.

HTTP Status Code: 502

KMSDisabledException

Lambda couldn't decrypt the environment variables because the Amazon KMS key used is disabled. Check the Lambda function's KMS key settings.

HTTP Status Code: 502

KMSInvalidStateException

Lambda couldn't decrypt the environment variables because the state of the Amazon KMS key used is not valid for Decrypt. Check the function's KMS key settings.

HTTP Status Code: 502

KMSNotFoundException

Lambda couldn't decrypt the environment variables because the Amazon KMS key was not found. Check the function's KMS key settings.

HTTP Status Code: 502

RecursiveInvocationException

Lambda has detected your function being invoked in a recursive loop with other Amazon resources and stopped your function's invocation.

HTTP Status Code: 400

RequestTooLargeException

The request payload exceeded the Invoke request body JSON input quota. For more information, see Lambda quotas.

HTTP Status Code: 413

ResourceConflictException

The resource already exists, or another operation is in progress.

HTTP Status Code: 409

ResourceNotFoundException

The resource specified in the request does not exist.

HTTP Status Code: 404

ResourceNotReadyException

The function is inactive and its VPC connection is no longer available. Wait for the VPC connection to reestablish and try again.

HTTP Status Code: 502

ServiceException

The Amazon Lambda service encountered an internal error.

HTTP Status Code: 500

SnapStartException

The afterRestore() runtime hook encountered an error. For more information, check the Amazon CloudWatch logs.

HTTP Status Code: 400

SnapStartNotReadyException

Lambda is initializing your function. You can invoke the function when the function state becomes Active.

HTTP Status Code: 409

SnapStartTimeoutException

Lambda couldn't restore the snapshot within the timeout limit.

HTTP Status Code: 408

SubnetIPAddressLimitReachedException

Amazon Lambda couldn't set up VPC access for the Lambda function because one or more configured subnets has no available IP addresses.

HTTP Status Code: 502

TooManyRequestsException

The request throughput limit was exceeded. For more information, see Lambda quotas.

HTTP Status Code: 429

UnsupportedMediaTypeException

The content type of the Invoke request body is not JSON.

HTTP Status Code: 415

See Also

For more information about using this API in one of the language-specific Amazon SDKs, see the following: