Package software.amazon.awscdk.services.batch
AWS Batch Construct Library
---
AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.
For more information on how to migrate, see the Migrating to AWS CDK v2 guide.
This module is part of the AWS Cloud Development Kit project.
AWS Batch is a batch processing tool for efficiently running hundreds of thousands computing jobs in AWS. Batch can dynamically provision different types of compute resources based on the resource requirements of submitted jobs.
AWS Batch simplifies the planning, scheduling, and executions of your batch workloads across a full range of compute services like Amazon EC2 and Spot Resources.
Batch achieves this by utilizing queue processing of batch job requests. To successfully submit a job for execution, you need the following resources:
- Job Definition - Group various job properties (container image, resource requirements, env variables...) into a single definition. These definitions are used at job submission time.
- Compute Environment - the execution runtime of submitted batch jobs
- Job Queue - the queue where batch jobs can be submitted to via AWS SDK/CLI
For more information on AWS Batch visit the AWS Docs for Batch.
Compute Environment
At the core of AWS Batch is the compute environment. All batch jobs are processed within a compute environment, which uses resource like OnDemand/Spot EC2 instances or Fargate.
In MANAGED mode, AWS will handle the provisioning of compute resources to accommodate the demand. Otherwise, in UNMANAGED mode, you will need to manage the provisioning of those resources.
Below is an example of each available type of compute environment:
Vpc vpc;
// default is managed
ComputeEnvironment awsManagedEnvironment = ComputeEnvironment.Builder.create(this, "AWS-Managed-Compute-Env")
.computeResources(ComputeResources.builder()
.vpc(vpc)
.build())
.build();
ComputeEnvironment customerManagedEnvironment = ComputeEnvironment.Builder.create(this, "Customer-Managed-Compute-Env")
.managed(false)
.build();
Spot-Based Compute Environment
It is possible to have AWS Batch submit spotfleet requests for obtaining compute resources. Below is an example of how this can be done:
Vpc vpc = new Vpc(this, "VPC");
ComputeEnvironment spotEnvironment = ComputeEnvironment.Builder.create(this, "MySpotEnvironment")
.computeResources(ComputeResources.builder()
.type(ComputeResourceType.SPOT)
.bidPercentage(75) // Bids for resources at 75% of the on-demand price
.vpc(vpc)
.build())
.build();
Fargate Compute Environment
It is possible to have AWS Batch submit jobs to be run on Fargate compute resources. Below is an example of how this can be done:
Vpc vpc = new Vpc(this, "VPC");
ComputeEnvironment fargateSpotEnvironment = ComputeEnvironment.Builder.create(this, "MyFargateEnvironment")
.computeResources(ComputeResources.builder()
.type(ComputeResourceType.FARGATE_SPOT)
.vpc(vpc)
.build())
.build();
Understanding Progressive Allocation Strategies
AWS Batch uses an allocation strategy to determine what compute resource will efficiently handle incoming job requests. By default, BEST_FIT will pick an available compute instance based on vCPU requirements. If none exist, the job will wait until resources become available. However, with this strategy, you may have jobs waiting in the queue unnecessarily despite having more powerful instances available. Below is an example of how that situation might look like:
Compute Environment: 1. m5.xlarge => 4 vCPU 2. m5.2xlarge => 8 vCPU
Job Queue: --------- | A | B | --------- Job Requirements: A => 4 vCPU - ALLOCATED TO m5.xlarge B => 2 vCPU - WAITING
In this situation, Batch will allocate Job A to compute resource #1 because it is the most cost efficient resource that matches the vCPU requirement. However, with this BEST_FIT strategy, Job B will not be allocated to our other available compute resource even though it is strong enough to handle it. Instead, it will wait until the first job is finished processing or wait a similar m5.xlarge resource to be provisioned.
The alternative would be to use the BEST_FIT_PROGRESSIVE strategy in order for the remaining job to be handled in larger containers regardless of vCPU requirement and costs.
Launch template support
Simply define your Launch Template:
// This example is only available in TypeScript
const myLaunchTemplate = new ec2.CfnLaunchTemplate(this, 'LaunchTemplate', {
launchTemplateName: 'extra-storage-template',
launchTemplateData: {
blockDeviceMappings: [
{
deviceName: '/dev/xvdcz',
ebs: {
encrypted: true,
volumeSize: 100,
volumeType: 'gp2',
},
},
],
},
});
and use it:
Vpc vpc;
CfnLaunchTemplate myLaunchTemplate;
ComputeEnvironment myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(ComputeResources.builder()
.launchTemplate(LaunchTemplateSpecification.builder()
.launchTemplateName((String)myLaunchTemplate.getLaunchTemplateName())
.build())
.vpc(vpc)
.build())
.computeEnvironmentName("MyStorageCapableComputeEnvironment")
.build();
Importing an existing Compute Environment
To import an existing batch compute environment, call ComputeEnvironment.fromComputeEnvironmentArn().
Below is an example:
IComputeEnvironment computeEnv = ComputeEnvironment.fromComputeEnvironmentArn(this, "imported-compute-env", "arn:aws:batch:us-east-1:555555555555:compute-environment/My-Compute-Env");
Change the baseline AMI of the compute resources
Occasionally, you will need to deviate from the default processing AMI.
ECS Optimized Amazon Linux 2 example:
Vpc vpc;
ComputeEnvironment myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(ComputeResources.builder()
.image(EcsOptimizedAmi.Builder.create()
.generation(AmazonLinuxGeneration.AMAZON_LINUX_2)
.build())
.vpc(vpc)
.build())
.build();
Custom based AMI example:
Vpc vpc;
ComputeEnvironment myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(ComputeResources.builder()
.image(MachineImage.genericLinux(Map.of(
"[aws-region]", "[ami-ID]")))
.vpc(vpc)
.build())
.build();
Job Queue
Jobs are always submitted to a specific queue. This means that you have to create a queue before you can start submitting jobs. Each queue is mapped to at least one (and no more than three) compute environment. When the job is scheduled for execution, AWS Batch will select the compute environment based on ordinal priority and available capacity in each environment.
ComputeEnvironment computeEnvironment;
JobQueue jobQueue = JobQueue.Builder.create(this, "JobQueue")
.computeEnvironments(List.of(JobQueueComputeEnvironment.builder()
// Defines a collection of compute resources to handle assigned batch jobs
.computeEnvironment(computeEnvironment)
// Order determines the allocation order for jobs (i.e. Lower means higher preference for job assignment)
.order(1)
.build()))
.build();
Priorty-Based Queue Example
Sometimes you might have jobs that are more important than others, and when submitted, should take precedence over the existing jobs. To achieve this, you can create a priority based execution strategy, by assigning each queue its own priority:
ComputeEnvironment sharedComputeEnvs;
JobQueue highPrioQueue = JobQueue.Builder.create(this, "JobQueue")
.computeEnvironments(List.of(JobQueueComputeEnvironment.builder()
.computeEnvironment(sharedComputeEnvs)
.order(1)
.build()))
.priority(2)
.build();
JobQueue lowPrioQueue = JobQueue.Builder.create(this, "JobQueue")
.computeEnvironments(List.of(JobQueueComputeEnvironment.builder()
.computeEnvironment(sharedComputeEnvs)
.order(1)
.build()))
.priority(1)
.build();
By making sure to use the same compute environments between both job queues, we will give precedence to the highPrioQueue for the assigning of jobs to available compute environments.
Importing an existing Job Queue
To import an existing batch job queue, call JobQueue.fromJobQueueArn().
Below is an example:
IJobQueue jobQueue = JobQueue.fromJobQueueArn(this, "imported-job-queue", "arn:aws:batch:us-east-1:555555555555:job-queue/High-Prio-Queue");
Job Definition
A Batch Job definition helps AWS Batch understand important details about how to run your application in the scope of a Batch Job. This involves key information like resource requirements, what containers to run, how the compute environment should be prepared, and more. Below is a simple example of how to create a job definition:
import software.amazon.awscdk.services.ecr.*;
IRepository repo = Repository.fromRepositoryName(this, "batch-job-repo", "todo-list");
JobDefinition.Builder.create(this, "batch-job-def-from-ecr")
.container(JobDefinitionContainer.builder()
.image(new EcrImage(repo, "latest"))
.build())
.build();
Using a local Docker project
Below is an example of how you can create a Batch Job Definition from a local Docker application.
JobDefinition.Builder.create(this, "batch-job-def-from-local")
.container(JobDefinitionContainer.builder()
// todo-list is a directory containing a Dockerfile to build the application
.image(ContainerImage.fromAsset("../todo-list"))
.build())
.build();
Providing custom log configuration
You can provide custom log driver and its configuration for the container.
import software.amazon.awscdk.services.ssm.*;
JobDefinition.Builder.create(this, "job-def")
.container(JobDefinitionContainer.builder()
.image(EcrImage.fromRegistry("docker/whalesay"))
.logConfiguration(LogConfiguration.builder()
.logDriver(LogDriver.AWSLOGS)
.options(Map.of("awslogs-region", "us-east-1"))
.secretOptions(List.of(ExposedSecret.fromParametersStore("xyz", StringParameter.fromStringParameterName(this, "parameter", "xyz"))))
.build())
.build())
.build();
Importing an existing Job Definition
From ARN
To import an existing batch job definition from its ARN, call JobDefinition.fromJobDefinitionArn().
Below is an example:
IJobDefinition job = JobDefinition.fromJobDefinitionArn(this, "imported-job-definition", "arn:aws:batch:us-east-1:555555555555:job-definition/my-job-definition");
From Name
To import an existing batch job definition from its name, call JobDefinition.fromJobDefinitionName().
If name is specified without a revision then the latest active revision is used.
Below is an example:
Deprecated: AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2. For more information on how to migrate, see https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html// Without revision IJobDefinition job1 = JobDefinition.fromJobDefinitionName(this, "imported-job-definition", "my-job-definition"); // With revision IJobDefinition job2 = JobDefinition.fromJobDefinitionName(this, "imported-job-definition", "my-job-definition:3");
-
ClassDescription(experimental) Properties for how to prepare compute resources that are provisioned for a compute environment.A CloudFormation
AWS::Batch::ComputeEnvironment.A fluent builder forCfnComputeEnvironment.Details about the compute resources managed by the compute environment.A builder forCfnComputeEnvironment.ComputeResourcesPropertyAn implementation forCfnComputeEnvironment.ComputeResourcesPropertyProvides information used to select Amazon Machine Images (AMIs) for instances in the compute environment.A builder forCfnComputeEnvironment.Ec2ConfigurationObjectPropertyAn implementation forCfnComputeEnvironment.Ec2ConfigurationObjectPropertyConfiguration for the Amazon EKS cluster that supports the AWS Batch compute environment.A builder forCfnComputeEnvironment.EksConfigurationPropertyAn implementation forCfnComputeEnvironment.EksConfigurationPropertyAn object that represents a launch template that's associated with a compute resource.An implementation forCfnComputeEnvironment.LaunchTemplateSpecificationPropertySpecifies the infrastructure update policy for the compute environment.A builder forCfnComputeEnvironment.UpdatePolicyPropertyAn implementation forCfnComputeEnvironment.UpdatePolicyPropertyProperties for defining aCfnComputeEnvironment.A builder forCfnComputeEnvironmentPropsAn implementation forCfnComputeEnvironmentPropsA CloudFormationAWS::Batch::JobDefinition.The authorization configuration details for the Amazon EFS file system.A builder forCfnJobDefinition.AuthorizationConfigPropertyAn implementation forCfnJobDefinition.AuthorizationConfigPropertyA fluent builder forCfnJobDefinition.Container properties are used for Amazon ECS based job definitions.A builder forCfnJobDefinition.ContainerPropertiesPropertyAn implementation forCfnJobDefinition.ContainerPropertiesPropertyAn object that represents a container instance host device.A builder forCfnJobDefinition.DevicePropertyAn implementation forCfnJobDefinition.DevicePropertyThis is used when you're using an Amazon Elastic File System file system for job storage.A builder forCfnJobDefinition.EfsVolumeConfigurationPropertyAn implementation forCfnJobDefinition.EfsVolumeConfigurationPropertyAn environment variable.A builder forCfnJobDefinition.EksContainerEnvironmentVariablePropertyAn implementation forCfnJobDefinition.EksContainerEnvironmentVariablePropertyEKS container properties are used in job definitions for Amazon EKS based job definitions to describe the properties for a container node in the pod that's launched as part of a job.A builder forCfnJobDefinition.EksContainerPropertyAn implementation forCfnJobDefinition.EksContainerPropertyThe volume mounts for a container for an Amazon EKS job.A builder forCfnJobDefinition.EksContainerVolumeMountPropertyAn implementation forCfnJobDefinition.EksContainerVolumeMountPropertyAn object that contains the properties for the Kubernetes resources of a job.A builder forCfnJobDefinition.EksPropertiesPropertyAn implementation forCfnJobDefinition.EksPropertiesPropertyExample:A builder forCfnJobDefinition.EksSecretPropertyAn implementation forCfnJobDefinition.EksSecretPropertySpecifies an Amazon EKS volume for a job definition.A builder forCfnJobDefinition.EksVolumePropertyAn implementation forCfnJobDefinition.EksVolumePropertyExample:A builder forCfnJobDefinition.EmptyDirPropertyAn implementation forCfnJobDefinition.EmptyDirPropertyThe Environment property type specifies environment variables to use in a job definition.A builder forCfnJobDefinition.EnvironmentPropertyAn implementation forCfnJobDefinition.EnvironmentPropertyExample:A builder forCfnJobDefinition.EphemeralStoragePropertyAn implementation forCfnJobDefinition.EphemeralStoragePropertySpecifies an array of up to 5 conditions to be met, and an action to take (RETRYorEXIT) if all conditions are met.A builder forCfnJobDefinition.EvaluateOnExitPropertyAn implementation forCfnJobDefinition.EvaluateOnExitPropertyThe platform configuration for jobs that are running on Fargate resources.A builder forCfnJobDefinition.FargatePlatformConfigurationPropertyAn implementation forCfnJobDefinition.FargatePlatformConfigurationPropertyExample:A builder forCfnJobDefinition.HostPathPropertyAn implementation forCfnJobDefinition.HostPathPropertyLinux-specific modifications that are applied to the container, such as details for device mappings.A builder forCfnJobDefinition.LinuxParametersPropertyAn implementation forCfnJobDefinition.LinuxParametersPropertyLog configuration options to send to a custom log driver for the container.A builder forCfnJobDefinition.LogConfigurationPropertyAn implementation forCfnJobDefinition.LogConfigurationPropertyExample:A builder forCfnJobDefinition.MetadataPropertyAn implementation forCfnJobDefinition.MetadataPropertyDetails for a Docker volume mount point that's used in a job's container properties.A builder forCfnJobDefinition.MountPointsPropertyAn implementation forCfnJobDefinition.MountPointsPropertyThe network configuration for jobs that are running on Fargate resources.A builder forCfnJobDefinition.NetworkConfigurationPropertyAn implementation forCfnJobDefinition.NetworkConfigurationPropertyAn object that represents the node properties of a multi-node parallel job.A builder forCfnJobDefinition.NodePropertiesPropertyAn implementation forCfnJobDefinition.NodePropertiesPropertyAn object that represents the properties of the node range for a multi-node parallel job.A builder forCfnJobDefinition.NodeRangePropertyPropertyAn implementation forCfnJobDefinition.NodeRangePropertyPropertyThe properties for the pod.A builder forCfnJobDefinition.PodPropertiesPropertyAn implementation forCfnJobDefinition.PodPropertiesPropertyThe type and amount of a resource to assign to a container.A builder forCfnJobDefinition.ResourceRequirementPropertyAn implementation forCfnJobDefinition.ResourceRequirementPropertyExample:A builder forCfnJobDefinition.ResourcesPropertyAn implementation forCfnJobDefinition.ResourcesPropertyThe retry strategy that's associated with a job.A builder forCfnJobDefinition.RetryStrategyPropertyAn implementation forCfnJobDefinition.RetryStrategyPropertyAn object that represents the secret to expose to your container.A builder forCfnJobDefinition.SecretPropertyAn implementation forCfnJobDefinition.SecretPropertyExample:A builder forCfnJobDefinition.SecurityContextPropertyAn implementation forCfnJobDefinition.SecurityContextPropertyAn object that represents a job timeout configuration.A builder forCfnJobDefinition.TimeoutPropertyAn implementation forCfnJobDefinition.TimeoutPropertyThe container path, mount options, and size of thetmpfsmount.A builder forCfnJobDefinition.TmpfsPropertyAn implementation forCfnJobDefinition.TmpfsPropertyTheulimitsettings to pass to the container.A builder forCfnJobDefinition.UlimitPropertyAn implementation forCfnJobDefinition.UlimitPropertyDetermine whether your data volume persists on the host container instance and where it's stored.A builder forCfnJobDefinition.VolumesHostPropertyAn implementation forCfnJobDefinition.VolumesHostPropertyA list of volumes that are associated with the job.A builder forCfnJobDefinition.VolumesPropertyAn implementation forCfnJobDefinition.VolumesPropertyProperties for defining aCfnJobDefinition.A builder forCfnJobDefinitionPropsAn implementation forCfnJobDefinitionPropsA CloudFormationAWS::Batch::JobQueue.A fluent builder forCfnJobQueue.The order that compute environments are tried in for job placement within a queue.A builder forCfnJobQueue.ComputeEnvironmentOrderPropertyAn implementation forCfnJobQueue.ComputeEnvironmentOrderPropertyProperties for defining aCfnJobQueue.A builder forCfnJobQueuePropsAn implementation forCfnJobQueuePropsA CloudFormationAWS::Batch::SchedulingPolicy.A fluent builder forCfnSchedulingPolicy.The fair share policy for a scheduling policy.A builder forCfnSchedulingPolicy.FairsharePolicyPropertyAn implementation forCfnSchedulingPolicy.FairsharePolicyPropertySpecifies the weights for the fair share identifiers for the fair share policy.A builder forCfnSchedulingPolicy.ShareAttributesPropertyAn implementation forCfnSchedulingPolicy.ShareAttributesPropertyProperties for defining aCfnSchedulingPolicy.A builder forCfnSchedulingPolicyPropsAn implementation forCfnSchedulingPolicyProps(experimental) Batch Compute Environment.(experimental) A fluent builder forComputeEnvironment.(experimental) Properties for creating a new Compute Environment.A builder forComputeEnvironmentPropsAn implementation forComputeEnvironmentProps(experimental) Properties for defining the structure of the batch compute cluster.A builder forComputeResourcesAn implementation forComputeResources(experimental) Property to specify if the compute environment uses On-Demand, SpotFleet, Fargate, or Fargate Spot compute resources.(experimental) Exposed secret for log configuration.(experimental) Properties of a compute environment.Internal default implementation forIComputeEnvironment.A proxy class which represents a concrete javascript instance of this type.(experimental) An interface representing a job definition - either a new one, created with the CDK, *using theJobDefinitionclass, or existing ones, referenced using theJobDefinition.fromJobDefinitionArnmethod.Internal default implementation forIJobDefinition.A proxy class which represents a concrete javascript instance of this type.(experimental) Properties of a Job Queue.Internal default implementation forIJobQueue.A proxy class which represents a concrete javascript instance of this type.(experimental) Properties for specifying multi-node properties for compute resources.Internal default implementation forIMultiNodeProps.A proxy class which represents a concrete javascript instance of this type.(experimental) Properties for a multi-node batch job.Internal default implementation forINodeRangeProps.A proxy class which represents a concrete javascript instance of this type.(experimental) Batch Job Definition.(experimental) A fluent builder forJobDefinition.(experimental) Properties of a job definition container.A builder forJobDefinitionContainerAn implementation forJobDefinitionContainer(experimental) Construction properties of theJobDefinitionconstruct.A builder forJobDefinitionPropsAn implementation forJobDefinitionProps(experimental) Batch Job Queue.(experimental) A fluent builder forJobQueue.(experimental) Properties for mapping a compute environment to a job queue.A builder forJobQueueComputeEnvironmentAn implementation forJobQueueComputeEnvironment(experimental) Properties of a batch job queue.A builder forJobQueuePropsAn implementation forJobQueueProps(experimental) Launch template property specification.A builder forLaunchTemplateSpecificationAn implementation forLaunchTemplateSpecification(experimental) Log configuration options to send to a custom log driver for the container.A builder forLogConfigurationAn implementation forLogConfiguration(experimental) The log driver to use for the container.(experimental) Platform capabilities.