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®
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
.
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,
overrides=JOB_OVERRIDES,
)
Create an AWS Batch compute environment¶
To create a new AWS Batch compute environment you can
use BatchCreateComputeEnvironmentOperator
.
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
.
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
.
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
.
wait_for_job_queue_valid = BatchJobQueueSensor(
task_id="wait_for_job_queue_valid",
job_queue=batch_job_queue_name,
)