Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions airflow/providers/amazon/aws/sensors/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if TYPE_CHECKING:
from airflow.utils.context import Context

from airflow.exceptions import AirflowException
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.sensors.base import BaseSensorOperator


Expand Down Expand Up @@ -74,9 +74,11 @@ def poke(self, context: Context) -> bool:
state = self.hook.conn.get_function(**trim_none_values(get_function_args))["Configuration"]["State"]

if state in self.FAILURE_STATES:
raise AirflowException(
"Lambda function state sensor failed because the Lambda is in a failed state"
)
message = "Lambda function state sensor failed because the Lambda is in a failed state"
# TODO: remove this if block when min_airflow_version is set to higher than 2.7.1
if self.soft_fail:
raise AirflowSkipException(message)
raise AirflowException(message)

return state in self.target_states

Expand Down
18 changes: 17 additions & 1 deletion tests/providers/amazon/aws/sensors/test_lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import pytest

from airflow.exceptions import AirflowException
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.providers.amazon.aws.hooks.lambda_function import LambdaHook
from airflow.providers.amazon.aws.sensors.lambda_function import LambdaFunctionStateSensor

Expand Down Expand Up @@ -69,3 +69,19 @@ def test_poke(self, get_function_output, expect_failure, expected):
mock_conn.get_function.assert_called_once_with(
FunctionName=FUNCTION_NAME,
)

@pytest.mark.parametrize(
"soft_fail, expected_exception", ((False, AirflowException), (True, AirflowSkipException))
)
def test_fail_poke(self, soft_fail, expected_exception):
sensor = LambdaFunctionStateSensor(
task_id="test_sensor",
function_name=FUNCTION_NAME,
)
sensor.soft_fail = soft_fail
message = "Lambda function state sensor failed because the Lambda is in a failed state"
with pytest.raises(expected_exception, match=message), mock.patch(
"airflow.providers.amazon.aws.hooks.lambda_function.LambdaHook.conn"
) as conn:
conn.get_function.return_value = {"Configuration": {"State": "Failed"}}
sensor.poke(context={})