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:
Create necessary resources using AWS Console or AWS CLI.
Install API libraries via pip.
pip install 'apache-airflow[amazon]'Detailed information is available Installation
Operators¶
Create an AWS Lambda function¶
To create an AWS lambda function you can use
LambdaCreateFunctionOperator
.
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
.
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 Amazon Lambda function state¶
To check the state of an Amazon Lambda function until it reaches the target state or another terminal
state you can use LambdaFunctionStateSensor
.
wait_lambda_function_state = LambdaFunctionStateSensor(
task_id="wait_lambda_function_state",
function_name=lambda_function_name,
)