Back to Prefect

lambda_function

docs/integrations/prefect-aws/api-ref/prefect_aws-lambda_function.mdx

3.6.30.dev34.8 KB
Original Source

prefect_aws.lambda_function

Integrations with AWS Lambda.

Examples:

Run a lambda function with a payload

```python
LambdaFunction(
    function_name="test-function",
    aws_credentials=aws_credentials,
).invoke(payload={"foo": "bar"})
```

Specify a version of a lambda function

```python
LambdaFunction(
    function_name="test-function",
    qualifier="1",
    aws_credentials=aws_credentials,
).invoke()
```

Invoke a lambda function asynchronously

```python
LambdaFunction(
    function_name="test-function",
    aws_credentials=aws_credentials,
).invoke(invocation_type="Event")
```

Invoke a lambda function and return the last 4 KB of logs

```python
LambdaFunction(
    function_name="test-function",
    aws_credentials=aws_credentials,
).invoke(tail=True)
```

Invoke a lambda function with a client context

```python
LambdaFunction(
    function_name="test-function",
    aws_credentials=aws_credentials,
).invoke(client_context={"bar": "foo"})
```

Classes

LambdaFunction <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/integrations/prefect-aws/prefect_aws/lambda_function.py#L65" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

Invoke a Lambda function. This block is part of the prefect-aws collection. Install prefect-aws with pip install prefect-aws to use this block.

Attributes:

  • function_name: The name, ARN, or partial ARN of the Lambda function to run. This must be the name of a function that is already deployed to AWS Lambda.
  • qualifier: The version or alias of the Lambda function to use when invoked. If not specified, the latest (unqualified) version of the Lambda function will be used.
  • aws_credentials: The AWS credentials to use to connect to AWS Lambda with a default factory of AwsCredentials.

Methods:

ainvoke <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/integrations/prefect-aws/prefect_aws/lambda_function.py#L117" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
ainvoke(self, payload: Optional[dict] = None, invocation_type: Literal['RequestResponse', 'Event', 'DryRun'] = 'RequestResponse', tail: bool = False, client_context: Optional[dict] = None) -> dict

Asynchronously invoke the Lambda function with the given payload.

Args:

  • payload: The payload to send to the Lambda function.
  • invocation_type: The invocation type of the Lambda function. This can be one of "RequestResponse", "Event", or "DryRun". Uses "RequestResponse" by default.
  • tail: If True, the response will include the base64-encoded last 4 KB of log data produced by the Lambda function.
  • client_context: The client context to send to the Lambda function. Limited to 3583 bytes.

Returns:

  • The response from the Lambda function.

Examples:

python
from prefect import flow
from prefect_aws.lambda_function import LambdaFunction
from prefect_aws.credentials import AwsCredentials

@flow
async def example_flow():
    credentials = AwsCredentials()
    lambda_function = LambdaFunction(
        function_name="test_lambda_function",
        aws_credentials=credentials,
    )
    response = await lambda_function.ainvoke(
        payload={"foo": "bar"},
        invocation_type="RequestResponse",
    )
    return response["Payload"].read()

invoke <sup><a href="https://github.com/PrefectHQ/prefect/blob/main/src/integrations/prefect-aws/prefect_aws/lambda_function.py#L186" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>

python
invoke(self, payload: Optional[dict] = None, invocation_type: Literal['RequestResponse', 'Event', 'DryRun'] = 'RequestResponse', tail: bool = False, client_context: Optional[dict] = None) -> dict

Invoke the Lambda function with the given payload.

Args:

  • payload: The payload to send to the Lambda function.
  • invocation_type: The invocation type of the Lambda function. This can be one of "RequestResponse", "Event", or "DryRun". Uses "RequestResponse" by default.
  • tail: If True, the response will include the base64-encoded last 4 KB of log data produced by the Lambda function.
  • client_context: The client context to send to the Lambda function. Limited to 3583 bytes.

Returns:

  • The response from the Lambda function.

Examples:

python
from prefect_aws.lambda_function import LambdaFunction
from prefect_aws.credentials import AwsCredentials

credentials = AwsCredentials()
lambda_function = LambdaFunction(
    function_name="test_lambda_function",
    aws_credentials=credentials,
)
response = lambda_function.invoke(
    payload={"foo": "bar"},
    invocation_type="RequestResponse",
)
response["Payload"].read()