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:

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[source]

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,
)

Durable execution

By default BatchOperator saves the Batch job id to the task state store right after submitting the job. If the worker crashes and the task is retried, the operator checks that job first:

  • still active (SUBMITTED through RUNNING): reconnects and keeps waiting on it

  • already SUCCEEDED: returns the job id without submitting again

  • FAILED, or no longer returned by AWS Batch: submits a new job

This applies to the synchronous path only; a deferrable run is unchanged.

Reconnecting only helps when the worker dies without a chance to clean up, such as an OOM kill or a lost node. On SIGTERM (for example a pod eviction) or on execution_timeout, on_kill terminates the Batch job, so the retry submits a new one.

Clearing a task can keep the stored job id. On Airflow 3.3 every clear keeps it. On 3.4 only clearing task instances through the UI or REST API discards it. When the id is kept and that job already succeeded, the cleared task returns without submitting a new job. From Airflow 3.5, every clear discards it by default. See Resumable Tasks for the clear paths, and set durable=False if every clear must submit a new job.

Set durable=False to submit a new job on every try. Durable execution needs Airflow 3.3 or later; on earlier versions the operator always submits a new job.

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[source]

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[source]

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[source]

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[source]

wait_for_job_queue_valid = BatchJobQueueSensor(
    task_id="wait_for_job_queue_valid",
    job_queue=batch_job_queue_name,
)

Reference

Was this entry helpful?