Snakemake executor plugin: aws-batch-flex

https://img.shields.io/badge/repository-github-blue?color=%23022c22 GitHub - Last commit https://img.shields.io/badge/author-Stephen%20Smith-purple?color=%23064e3b PyPI - Version PyPI - License Snakemake

Warning

This plugin is not maintained and reviewed by the official Snakemake organization.

Runs Snakemake workflows on AWS Batch with off-the-shelf container images: each rule runs in the image its container: directive names, exactly as published (quay.io/biocontainers, Docker Hub, ECR, …). You don’t build images with Snakemake in them, and the images don’t need Python either: on EC2, a Snakemake environment baked into the AMI is mounted into every job; on Fargate, Snakemake is installed when the job starts.

It needs Snakemake 9.5 or newer, and the S3 storage plugin (see below).

Installation

Install this plugin by installing it with pip or mamba directly, e.g.:

pip install snakemake-executor-plugin-aws-batch-flex

Or, if you are using pixi, add the plugin to your pixi.toml. Be careful to put it under the right dependency type based on the plugin’s availability, e.g.:

snakemake-executor-plugin-aws-batch-flex = "*"

Usage

In order to use the plugin, run Snakemake (>=8.6) in the folder where your workflow code and config resides (containing either workflow/Snakefile or Snakefile) with the corresponding value for the executor flag:

snakemake --executor aws-batch-flex --default-resources --jobs N ...

with N being the number of jobs you want to run in parallel and ... being any additional arguments you want to use (see below). The machine on which you run Snakemake must have the executor plugin installed, and, depending on the type of the executor plugin, have access to the target service of the executor plugin (e.g. an HPC middleware like slurm with the sbatch command, or internet access to submit jobs to some cloud provider, e.g. azure).

The flag --default-resources ensures that Snakemake auto-calculates the mem and disk resources for each job, based on the input file size. The values assumed there are conservative and should usually suffice. However, you can always override those defaults by specifying the resources in your Snakemake rules or via the --set-resources flag.

Depending on the executor plugin, you might either rely on a shared local filesystem or use a remote filesystem or storage. For the latter, you have to additionally use a suitable storage plugin (see section storage plugins in the sidebar of this catalog) and eventually check for further recommendations in the sections below.

All arguments can also be persisted via a profile, such that they don’t have to be specified on each invocation. Here, this would mean the following entries inside of the profile

executor: aws-batch-flex
default_resources: []

For specifying other default resources than the built-in ones, see the docs.

Settings

The executor plugin has the following settings (which can be passed via command line, the workflow or environment variables, if provided in the respective columns):

Settings

CLI argument

Description

Default

Choices

Required

Type

--aws-batch-flex-queue VALUE

AWS Batch job queue to submit to. A rule can override this with the resource batch_queue.

None

✓

--aws-batch-flex-region VALUE

AWS region. Defaults to the standard boto3 resolution (AWS_REGION, AWS_PROFILE, ~/.aws/config).

None

✗

--aws-batch-flex-job-role-arn VALUE

IAM role assumed by the job container (needs access to the storage bucket). If omitted, the compute environment’s instance role is used (EC2 only, and only if the instances let containers reach it: jobs_use_instance_role in the Terraform setup).

None

✗

--aws-batch-flex-execution-role-arn VALUE

ECS task execution role. Required for Fargate and for pulling from private registries.

None

✗

--aws-batch-flex-head-role-arn VALUE

Only for smkflex run: IAM role of the head job that runs Snakemake on AWS (Terraform prints it as head_role_arn). Snakemake itself ignores it.

None

✗

--aws-batch-flex-platform VALUE

Compute platform of the main queue: ‘ec2’ (also covers EC2 Spot) or ‘fargate’. Default: detected per queue from its compute environment (EC2 and Fargate queues can be mixed via the batch_queue resource); falls back to ec2 when the queue cannot be inspected. A rule can force it with the batch_platform resource.

None

✗

--aws-batch-flex-default-image VALUE

Image for rules without a container: directive. Defaults to Snakemake’s –container-image.

None

✗

--aws-batch-flex-bootstrap VALUE

How to get Snakemake into job containers. ‘host’ (EC2, recommended; Nextflow-style): mount a Snakemake env pre-installed on the host (see snakemake-batch-flex-host-env), so images need only /bin/sh. ‘auto’: install at job start only if the image lacks the matching version; ‘always’: always install at job start; ‘never’: the image must already contain Snakemake and the storage plugins.

'auto'

✗

--aws-batch-flex-host-env-path VALUE

Path of the host-installed Snakemake env, mounted read-only at the same path in every container (bootstrap=host).

'/opt/snakemake-env'

✗

--aws-batch-flex-host-env-wait-seconds VALUE

bootstrap=host: how long a job waits for the host env to become ready (instances installing it via user-data). Use 0 on a baked AMI.

120

✗

--aws-batch-flex-install-prereqs VALUE

Install-at-start modes (auto/always): if the image has no curl, wget or python3+pip, install curl/ca-certificates/bash with the image’s package manager (apt/apk/dnf/yum). Needs root in the image and access to the distro mirrors.

False

✗

--aws-batch-flex-script-python VALUE

Which python runs Python script: rules. ‘image’: the image’s python if it is 3.10 or newer (so the script sees the image’s packages), else Snakemake’s. ‘host’: always Snakemake’s own python. Per rule: the batch_script_python resource.

'image'

✗

--aws-batch-flex-mem-overhead-mib VALUE

MiB added to each rule’s mem_mb/mem_mib for the Snakemake process that runs inside the job and shares its memory limit. 0 = request exactly mem_mb.

256

✗

--aws-batch-flex-skip-preflight VALUE

Skip the startup checks of the queue, its compute environment and the job role.

False

✗

--aws-batch-flex-conda-path VALUE

Host-installed conda (see snakemake-batch-flex-host-env conda / bake-ami.sh –conda), mounted read-only at the same path. Lets any image run conda: rules, including existing named environments in <path>/envs. EC2 only.

None

✗

--aws-batch-flex-volumes VALUE

Extra host bind mounts, HOST:CONTAINER[:ro] (EC2 only), like Nextflow’s aws.batch.volumes. Example: /scratch:/scratch

None

✗

--aws-batch-flex-extra-packages VALUE

Extra pip requirements installed alongside Snakemake when bootstrapping (e.g. additional Snakemake plugins).

None

✗

--aws-batch-flex-python-version VALUE

Python version for the bootstrap venv. Defaults to the version running this Snakemake.

None

✗

--aws-batch-flex-spot-retries VALUE

How many times AWS Batch itself retries a job whose host was reclaimed (Spot interruption), 0-9; 0 = none. Independent of Snakemake’s –retries. Per rule: the batch_spot_retries resource.

2

✗

--aws-batch-flex-scheduling-priority VALUE

Scheduling priority (0-9999, higher runs first) for every job. Only works on queues with a fair-share scheduling policy; skipped with a warning otherwise. Per rule: the batch_priority resource.

None

✗

--aws-batch-flex-log-group VALUE

CloudWatch log group for job logs. The default needs no setup.

'/aws/batch/job'

✗

--aws-batch-flex-job-definition VALUE

Use this existing job definition (name, name:revision or ARN) instead of registering one per image; per rule: the batch_job_definition resource. It fixes the image, job role, mounts and /dev/shm; vCPUs, memory, timeout and tags still come from the rules. Without the host-env mount, jobs install Snakemake at start.

None

✗

--aws-batch-flex-job-definition-prefix VALUE

Prefix for auto-registered job definitions.

'snakemake'

✗

--aws-batch-flex-assign-public-ip VALUE

Fargate only: ‘ENABLED’ or ‘DISABLED’. DISABLED needs a NAT gateway or VPC endpoints for image pulls and bootstrapping.

'ENABLED'

✗

--aws-batch-flex-ephemeral-storage-gib VALUE

Fargate only: minimum scratch disk in GiB (21-200); a rule’s disk_mb can raise it for that rule.

None

✗

--aws-batch-flex-registry-credentials VALUE

REGISTRY=SECRET_ARN pairs for private registries outside AWS, e.g. docker.io=arn:aws:secretsmanager:…:secret:dockerhub (the secret holds {“username”: …, “password”: …}). Only images from that registry use it. Needs –aws-batch-flex-execution-role-arn with secretsmanager:GetSecretValue on the secret. ECR images need no credentials (the instance/execution role pulls them).

None

✗

--aws-batch-flex-secrets VALUE

NAME=ARN pairs: environment variables jobs get from an SSM parameter (arn:aws:ssm:…:parameter/…) or a Secrets Manager secret. ECS reads them when the job starts, so unlike –envvars the value is never in the job’s settings, the Batch console or CloudTrail. Needs –aws-batch-flex-execution-role-arn allowed to read them.

None

✗

--aws-batch-flex-orphan-jobs VALUE

What to do with jobs still running from an earlier, interrupted run of the same workflow (same directory, Snakefile and storage prefix), which Snakemake would otherwise run a second time: ‘cancel’ (default), ‘warn’ (default with –nolock) or ‘ignore’. Needs batch:ListJobs.

None

✗

--aws-batch-flex-tags VALUE

KEY=VALUE tags applied to every job and to job definitions (cost allocation). Rules can add or override tags for their own jobs with the resource batch_tags=”k1=v1,k2=v2”.

None

✗

--aws-batch-flex-conda-cache VALUE

Reuse conda environments instead of building them in every job: ‘instance’ (jobs on the same EC2 machine share them), an S3 URL such as s3://BUCKET/snakemake/.smkflex/conda-cache (also kept there for later jobs and runs), or ‘none’.

'instance'

✗

--aws-batch-flex-oom-memory-factor VALUE

When a job ran out of memory and Snakemake retries it (–retries, or a rule’s retries:), ask for this many times more memory on each retry. 1 = never.

2.0

✗

--aws-batch-flex-no-usage-summary VALUE

Don’t print the per-rule summary of what the run used (vCPU-hours, memory, CPUs, disk, and the resources to paste into the profile) at the end of the run.

False

✗

--aws-batch-flex-no-auto-tags VALUE

Disable the automatic job tags snakemake-run-id, snakemake-workflow, snakemake-rule and snakemake-job-id (on by default, for per-rule cost reporting). Also turns off the check for jobs left over from an interrupted run.

False

✗

--aws-batch-flex-max-sources-mb VALUE

Every job gets the workflow folder (what git would track, or everything .gitignore doesn’t exclude, and config/; single files over 10 MB are left out). Stop with a message when that is more than this many MiB (as du -sh shows sizes): data belongs in storage.

100

✗

--aws-batch-flex-runnable-warn-minutes VALUE

Warn when a job has been RUNNABLE (waiting for capacity) this long; usually means the request exceeds what the compute environment can provide.

15

✗

Further details

Setup

Install the plugin together with the S3 storage plugin, which it uses for all files but doesn’t depend on (in pixi, both go under [pypi-dependencies]):

pip install snakemake-executor-plugin-aws-batch-flex snakemake-storage-plugin-s3

Besides --executor aws-batch-flex, a run needs a queue and S3 storage, best kept in a profile (see the example below): --aws-batch-flex-queue, --default-storage-provider s3 and --default-storage-prefix s3://BUCKET/PREFIX.

You need a Batch queue whose instances have the Snakemake environment. In the repository, bash scripts/setup.sh --region eu-west-1 builds the AMI (with Snakemake, conda and pandas), creates the queue, compute environment, roles, log group and S3 bucket with Terraform, and installs a global profile called smkflex, so snakemake --profile smkflex works in any folder. The README walks through it step by step, from an empty AWS account to a finished test run.

To try the plugin on an existing queue without building an AMI, set aws-batch-flex-bootstrap: auto: Snakemake is then installed into each container when its job starts (about 10-15 s per job; needs internet access and curl, wget or pip in the image).

Example profile

executor: aws-batch-flex
default-storage-provider: s3
default-storage-prefix: s3://my-bucket/snakemake
aws-batch-flex-queue: my-queue
aws-batch-flex-region: eu-west-1
aws-batch-flex-job-role-arn: arn:aws:iam::123456789012:role/snakemake-job
aws-batch-flex-bootstrap: host
aws-batch-flex-default-image: buildpack-deps:bookworm-scm
jobs: 100

Terraform’s snakemake_profile output prints one filled in for your account.

Rules

rule align:
    input: "data/{sample}.fq"
    output: "results/{sample}.sam"
    container: "docker://quay.io/biocontainers/bwa:0.7.17--h7132678_9"
    threads: 8
    resources: mem_mb=16000, runtime=120, batch_queue="big-queue", batch_tags="stage=align"
    shell: "bwa mem -t {threads} ref.fa {input} > {output}"
  • threads sets vCPUs, mem_mb memory (plus 256 MiB for the Snakemake process in the job), runtime (minutes) the timeout, and gpu GPUs. When Snakemake retries a job that ran out of memory (--retries), the retry gets twice as much.

  • disk_mb sets the job’s disk on Fargate (up to 200 GiB). On EC2, jobs share the instance’s disk and disk_mb isn’t enforced.

  • batch_shm_mb sets the size of /dev/shm (EC2 only); batch_spot_retries and batch_priority set Spot retries and fair-share priority per rule.

  • batch_job_definition (or aws-batch-flex-job-definition for all rules) uses an existing job definition instead of registering one.

  • batch_queue sends a rule to another queue; EC2, Spot and Fargate queues can be mixed in one run.

  • batch_tags adds tags to that rule’s jobs; every job is also tagged with the run, workflow, rule and job ID.

  • Python script: rules run with the image’s own Python (3.10 or newer), so they can use its packages, unless their pandas/numpy/polars params need packages it lacks; batch_script_python="host" always uses Snakemake’s instead.

  • conda: and wrapper: work with --sdm conda (conda from the image, or baked into the AMI). Built envs are reused rather than rebuilt in every job: by default, by the jobs on the same EC2 instance; with aws-batch-flex-conda-cache set to an S3 URL, also from S3 by later jobs and runs.

  • If snakemake is killed, the jobs it left running are cancelled when you restart it.

  • Private images: ECR works through the IAM roles; other registries through Secrets Manager (aws-batch-flex-registry-credentials).

At the end of a run the plugin prints what each rule used: vCPU- and GiB-hours, peak memory, the most vCPUs busy at once and the most disk its jobs took, with set-resources: and set-threads: blocks to paste into the project profile. Costs come from AWS Billing, by the tags on every job (snakemake-run-id, snakemake-rule).

Published pipelines and long runs

Pipelines from the workflow catalog run unchanged: deploy them with snakedeploy, then run smkflex init (gives the project its own prefix in the bucket) and smkflex push data (uploads the inputs). smkflex run runs Snakemake itself as a Batch job, so nothing has to stay running on your machine; smkflex logs, status and cancel follow and stop it.

Limits

  • Host mode needs glibc images with /bin/sh and bash (no Alpine).

  • pipe() doesn’t work; Singularity/Apptainer images can’t be used.

  • All rules in a group must use the same image.

The README covers setup, and the Reference lists every option, what has been tested on AWS, the remaining limits and the IAM permissions needed.