AWS Lambda

With AWS Lambda, you can run code without provisioning or managing servers. You pay only for the compute time that you consume—there’s no charge when your code isn’t running. You can run code for virtually any type of application or backend service—all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

Prerequisite Tasks

To use these operators, you must do a few things:

Operators

Create an AWS Lambda function

To create an AWS lambda function you can use LambdaCreateFunctionOperator. This operator can be run in deferrable mode by passing deferrable=True as a parameter. This requires the aiobotocore module to be installed.

tests/system/providers/amazon/aws/example_lambda.py[source]

create_lambda_function = LambdaCreateFunctionOperator(
    task_id="create_lambda_function",
    function_name=lambda_function_name,
    runtime="python3.9",
    role=role_arn,
    handler="lambda_function.test",
    code={
        "ZipFile": create_zip(CODE_CONTENT),
    },
)

Invoke an AWS Lambda function

To invoke an AWS lambda function you can use LambdaInvokeFunctionOperator.

Note

According to Lambda.Client.invoke documentation for synchronous invocation (invocation_type="RequestResponse") with a long timeout, your client might disconnect during synchronous invocation while it waits for a response.

If this happens you will see a ReadTimeoutError exception similar to this:

urllib3.exceptions.ReadTimeoutError: AWSHTTPSConnectionPool(host='lambda.us-east-1.amazonaws.com', port=443): Read timed out. (read timeout=60)

If you encounter this issue, configure Amazon Web Services Connection to allow botocore / boto3 to use long connections with timeout or keep-alive settings in the Connection Extras field

{
  "config_kwargs": {
    "connect_timeout": 900,
    "read_timeout": 900,
    "tcp_keepalive": true
  }
}

In addition, you might need to configure your firewall, proxy, or operating system to allow for long connections with timeout or keep-alive settings.

Note

You cannot describe the asynchronous invocation (invocation_type="Event") of an AWS Lambda function. The only way is configuring destinations for asynchronous invocation and sensing destination.

tests/system/providers/amazon/aws/example_lambda.py[source]

invoke_lambda_function = LambdaInvokeFunctionOperator(
    task_id="invoke_lambda_function",
    function_name=lambda_function_name,
    payload=json.dumps({"SampleEvent": {"SampleData": {"Name": "XYZ", "DoB": "1993-01-01"}}}),
)

Sensors

Wait on an AWS Lambda function deployment state

To check the deployment state of an AWS Lambda function until it reaches the target state or another terminal state you can use LambdaFunctionStateSensor.

tests/system/providers/amazon/aws/example_lambda.py[source]

wait_lambda_function_state = LambdaFunctionStateSensor(
    task_id="wait_lambda_function_state",
    function_name=lambda_function_name,
)

Was this entry helpful?