@Stability(value=Experimental)
See: Description
| Enum | Description |
|---|---|
| AllocationStrategy |
(experimental) Properties for how to prepare compute resources that are provisioned for a compute environment.
|
| ComputeResourceType |
(experimental) Property to specify if the compute environment uses On-Demand or SpotFleet compute resources.
|
| LogDriver |
(experimental) The log driver to use for the container.
|
---
All classes with the
Cfnprefix in this module (CFN Resources) are always stable and safe to use.
The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
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:
For more information on AWS Batch visit the AWS Docs for Batch.
At the core of AWS Batch is the compute environment. All batch jobs are processed within a compute environment, which uses resource like OnDemand or Spot EC2 instances.
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:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object defaultVpc = new Vpc(this, "VPC");
// default is managed
Object awsManagedEnvironment = ComputeEnvironment.Builder.create(stack, "AWS-Managed-Compute-Env")
.computeResources(Map.of(
"vpc", vpc))
.build();
Object customerManagedEnvironment = ComputeEnvironment.Builder.create(stack, "Customer-Managed-Compute-Env")
.managed(false)
.build();
It is possible to have AWS Batch submit spotfleet requests for obtaining compute resources. Below is an example of how this can be done:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object vpc = new Vpc(this, "VPC");
Object spotEnvironment = ComputeEnvironment.Builder.create(stack, "MySpotEnvironment")
.computeResources(Map.of(
"type", batch.ComputeResourceType.getSPOT(),
"bidPercentage", 75, // Bids for resources at 75% of the on-demand price
"vpc", vpc))
.build();
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.
Simply define your Launch Template:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object myLaunchTemplate = CfnLaunchTemplate.Builder.create(this, "LaunchTemplate")
.launchTemplateName("extra-storage-template")
.launchTemplateData(Map.of(
"blockDeviceMappings", asList(Map.of(
"deviceName", "/dev/xvdcz",
"ebs", Map.of(
"encrypted", true,
"volumeSize", 100,
"volumeType", "gp2")))))
.build();
and use it:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(Map.of(
"launchTemplate", Map.of(
"launchTemplateName", (String)myLaunchTemplate.getLaunchTemplateName()),
"vpc", vpc))
.computeEnvironmentName("MyStorageCapableComputeEnvironment")
.build();
To import an existing batch compute environment, call ComputeEnvironment.fromComputeEnvironmentArn().
Below is an example:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Object computeEnv = batch.ComputeEnvironment.fromComputeEnvironmentArn(this, "imported-compute-env", "arn:aws:batch:us-east-1:555555555555:compute-environment/My-Compute-Env");
Occasionally, you will need to deviate from the default processing AMI.
ECS Optimized Amazon Linux 2 example:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(Map.of(
"image", EcsOptimizedAmi.Builder.create()
.generation(ec2.AmazonLinuxGeneration.getAMAZON_LINUX_2())
.build(),
"vpc", vpc))
.build();
Custom based AMI example:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object myComputeEnv = ComputeEnvironment.Builder.create(this, "ComputeEnv")
.computeResources(Map.of(
"image", ec2.MachineImage.genericLinux(Map.of(
"[aws-region]", "[ami-ID]")),
"vpc", vpc))
.build();
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.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object jobQueue = JobQueue.Builder.create(stack, "JobQueue")
.computeEnvironments(asList(Map.of(
// 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();
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:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object highPrioQueue = JobQueue.Builder.create(stack, "JobQueue")
.computeEnvironments(sharedComputeEnvs)
.priority(2)
.build();
Object lowPrioQueue = JobQueue.Builder.create(stack, "JobQueue")
.computeEnvironments(sharedComputeEnvs)
.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.
To import an existing batch job queue, call JobQueue.fromJobQueueArn().
Below is an example:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Object jobQueue = batch.JobQueue.fromJobQueueArn(this, "imported-job-queue", "arn:aws:batch:us-east-1:555555555555:job-queue/High-Prio-Queue");
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:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
Object repo = ecr.Repository.fromRepositoryName(stack, "batch-job-repo", "todo-list");
JobDefinition.Builder.create(stack, "batch-job-def-from-ecr")
.container(Map.of(
"image", new EcrImage(repo, "latest")))
.build();
Below is an example of how you can create a Batch Job Definition from a local Docker application.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
JobDefinition.Builder.create(stack, "batch-job-def-from-local")
.container(Map.of(
// todo-list is a directory containing a Dockerfile to build the application
"image", ecs.ContainerImage.fromAsset("../todo-list")))
.build();
You can provide custom log driver and its configuration for the container.
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
JobDefinition.Builder.create(stack, "job-def")
.container(Map.of(
"image", ecs.EcrImage.fromRegistry("docker/whalesay"),
"logConfiguration", Map.of(
"logDriver", batch.LogDriver.getAWSLOGS(),
"options", Map.of("awslogs-region", "us-east-1"),
"secretOptions", asList(batch.ExposedSecret.fromParametersStore("xyz", ssm.StringParameter.fromStringParameterName(stack, "parameter", "xyz"))))))
.build();
To import an existing batch job definition from its ARN, call JobDefinition.fromJobDefinitionArn().
Below is an example:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 Object job = batch.JobDefinition.fromJobDefinitionArn(this, "imported-job-definition", "arn:aws:batch:us-east-1:555555555555:job-definition/my-job-definition");
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:
// Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826 // Without revision Object job = batch.JobDefinition.fromJobDefinitionName(this, "imported-job-definition", "my-job-definition"); // With revision Object job = batch.JobDefinition.fromJobDefinitionName(this, "imported-job-definition", "my-job-definition:3");
Copyright © 2021. All rights reserved.