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 of Airflow®
Generic Parameters¶
- aws_conn_id
Reference to Amazon Web Services Connection ID. If this parameter is set to
None
then the default boto3 behaviour is used without a connection lookup. Otherwise use the credentials stored in the Connection. Default:aws_default
- region_name
AWS Region Name. If this parameter is set to
None
or omitted then region_name from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None
- verify
Whether or not to verify SSL certificates.
False
- Do not validate SSL certificates.path/to/cert/bundle.pem - A filename of the CA cert bundle to use. You can specify this argument if you want to use a different CA cert bundle than the one used by botocore.
If this parameter is set to
None
or is omitted then verify from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None
- botocore_config
The provided dictionary is used to construct a botocore.config.Config. This configuration can be used to configure Avoid Throttling exceptions, timeouts, etc.
{ "signature_version": "unsigned", "s3": { "us_east_1_regional_endpoint": True, }, "retries": { "mode": "standard", "max_attempts": 10, }, "connect_timeout": 300, "read_timeout": 300, "tcp_keepalive": True, }
If this parameter is set to
None
or omitted then config_kwargs from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None
Note
Specifying an empty dictionary,
{}
, will overwrite the connection configuration for botocore.config.Config
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/amazon/aws/example_lambda.py
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, you need to provide botocore.config.Config to use long connections with timeout or keep-alive settings.
By providing botocore_config as an operator parameter
{
"connect_timeout": 900,
"read_timeout": 900,
"tcp_keepalive": True,
}
Or specify config_kwargs in associated AWS Connection Extra Parameter
{
"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/amazon/aws/example_lambda.py
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/amazon/aws/example_lambda.py
wait_lambda_function_state = LambdaFunctionStateSensor(
task_id="wait_lambda_function_state",
function_name=lambda_function_name,
)