Lambda Boto3 Docs 1.16.0 documentation

May 2024 ยท 4 minute read

Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event .

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 Retry Behavior .

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, limit errors , or issues with your function's code and configuration. For example, Lambda returns TooManyRequestsException if executing 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 be disconnected 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.

See also: AWS API Documentation

Request Syntax

response = client.invoke( FunctionName='string', InvocationType='Event'|'RequestResponse'|'DryRun', LogType='None'|'Tail', ClientContext='string', Payload=b'bytes'|file, Qualifier='string' ) 
ParametersReturn type

dict

Returns

Response Syntax

{ 'StatusCode': 123, 'FunctionError': 'string', 'LogResult': 'string', 'Payload': StreamingBody(), 'ExecutedVersion': 'string' } 

Response Structure

Exceptions

Examples

The following example invokes version 1 of a function named my-function with an empty event payload.

response = client.invoke( FunctionName='my-function', Payload='{}', Qualifier='1', ) print(response) 

Expected Output:

{ 'Payload': '200 SUCCESS', 'StatusCode': 200, 'ResponseMetadata': { '...': '...', }, } 

The following example invokes version 1 of a function named my-function asynchronously.

response = client.invoke( FunctionName='my-function', InvocationType='Event', Payload='{}', Qualifier='1', ) print(response) 

Expected Output:

{ 'Payload': '', 'StatusCode': 202, 'ResponseMetadata': { '...': '...', }, } 

ncG1vNJzZmian6m8dHrAppizp56WxLR6wqikaK5hZLGwr9SmnKeskam2sLqOmqeiZ2Fjfnd6j2ipnp6Vp7Kvr8Roqp6qpp6wpr%2BOpZimmpSWe6nAzKU%3D