AWS Batch¶
AWS Batch enables you to run batch computing workloads on the AWS Cloud. Batch computing is a common way for developers, scientists, and engineers to access large amounts of compute resources. AWS Batch removes the undifferentiated heavy lifting of configuring and managing the required infrastructure.
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.
Example, for more detail about parameters please have a look botocore.config.Config¶{ "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¶
Submit a new AWS Batch job¶
To submit a new AWS Batch job and monitor it until it reaches a terminal state you can
use BatchOperator
.
tests/system/amazon/aws/example_batch.py
submit_batch_job = BatchOperator(
task_id="submit_batch_job",
job_name=batch_job_name,
job_queue=batch_job_queue_name,
job_definition=batch_job_definition_name,
container_overrides=JOB_OVERRIDES,
)
Create an AWS Batch compute environment¶
To create a new AWS Batch compute environment you can
use BatchCreateComputeEnvironmentOperator
.
tests/system/amazon/aws/example_batch.py
create_compute_environment = BatchCreateComputeEnvironmentOperator(
task_id="create_compute_environment",
compute_environment_name=batch_job_compute_environment_name,
environment_type="MANAGED",
state="ENABLED",
compute_resources={
"type": "FARGATE",
"maxvCpus": 10,
"securityGroupIds": security_groups,
"subnets": subnets,
},
)
Sensors¶
Wait on an AWS Batch job state¶
To wait on the state of an AWS Batch Job until it reaches a terminal state you can
use BatchSensor
.
tests/system/amazon/aws/example_batch.py
wait_for_batch_job = BatchSensor(
task_id="wait_for_batch_job",
job_id=submit_batch_job.output,
)
In order to monitor the state of the AWS Batch Job asynchronously, use
BatchSensor
with the
parameter deferrable
set to True.
Since this will release the Airflow worker slot , it will lead to efficient utilization of available resources on your Airflow deployment. This will also need the triggerer component to be available in your Airflow deployment.
Wait on an AWS Batch compute environment status¶
To wait on the status of an AWS Batch compute environment until it reaches a terminal status you can
use BatchComputeEnvironmentSensor
.
tests/system/amazon/aws/example_batch.py
wait_for_compute_environment_valid = BatchComputeEnvironmentSensor(
task_id="wait_for_compute_environment_valid",
compute_environment=batch_job_compute_environment_name,
)
Wait on an AWS Batch job queue status¶
To wait on the status of an AWS Batch job queue until it reaches a terminal status you can
use BatchJobQueueSensor
.
tests/system/amazon/aws/example_batch.py
wait_for_job_queue_valid = BatchJobQueueSensor(
task_id="wait_for_job_queue_valid",
job_queue=batch_job_queue_name,
)