package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

AWS Batch Construct Library

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:

  1. Job Definition - Group various job properties (container image, resource requirements, env variables...) into a single definition. These definitions are used at job submission time.
  2. Compute Environment - the execution runtime of submitted batch jobs
  3. 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:

var vpc vpc


// default is managed
awsManagedEnvironment := batch.NewComputeEnvironment(this, jsii.String("AWS-Managed-Compute-Env"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		Vpc: *Vpc,
	},
})

customerManagedEnvironment := batch.NewComputeEnvironment(this, jsii.String("Customer-Managed-Compute-Env"), &ComputeEnvironmentProps{
	Managed: jsii.Boolean(false),
})

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 := ec2.NewVpc(this, jsii.String("VPC"))

spotEnvironment := batch.NewComputeEnvironment(this, jsii.String("MySpotEnvironment"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		Type: batch.ComputeResourceType_SPOT,
		BidPercentage: jsii.Number(75),
		 // Bids for resources at 75% of the on-demand price
		Vpc: *Vpc,
	},
})

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 := ec2.NewVpc(this, jsii.String("VPC"))

fargateSpotEnvironment := batch.NewComputeEnvironment(this, jsii.String("MyFargateEnvironment"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		Type: batch.ComputeResourceType_FARGATE_SPOT,
		Vpc: *Vpc,
	},
})

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:

var vpc vpc
var myLaunchTemplate cfnLaunchTemplate


myComputeEnv := batch.NewComputeEnvironment(this, jsii.String("ComputeEnv"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		LaunchTemplate: &LaunchTemplateSpecification{
			LaunchTemplateName: string(myLaunchTemplate.LaunchTemplateName),
		},
		Vpc: *Vpc,
	},
	ComputeEnvironmentName: jsii.String("MyStorageCapableComputeEnvironment"),
})

Importing an existing Compute Environment

To import an existing batch compute environment, call ComputeEnvironment.fromComputeEnvironmentArn().

Below is an example:

computeEnv := batch.ComputeEnvironment_FromComputeEnvironmentArn(this, jsii.String("imported-compute-env"), jsii.String("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:

var vpc vpc

myComputeEnv := batch.NewComputeEnvironment(this, jsii.String("ComputeEnv"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		Image: ecs.NewEcsOptimizedAmi(&EcsOptimizedAmiProps{
			Generation: ec2.AmazonLinuxGeneration_AMAZON_LINUX_2,
		}),
		Vpc: *Vpc,
	},
})

Custom based AMI example:

var vpc vpc

myComputeEnv := batch.NewComputeEnvironment(this, jsii.String("ComputeEnv"), &ComputeEnvironmentProps{
	ComputeResources: &ComputeResources{
		Image: ec2.MachineImage_GenericLinux(map[string]*string{
			"[aws-region]": jsii.String("[ami-ID]"),
		}),
		Vpc: *Vpc,
	},
})

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.

var computeEnvironment computeEnvironment

jobQueue := batch.NewJobQueue(this, jsii.String("JobQueue"), &JobQueueProps{
	ComputeEnvironments: []jobQueueComputeEnvironment{
		&jobQueueComputeEnvironment{
			// 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: jsii.Number(1),
		},
	},
})

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:

var sharedComputeEnvs computeEnvironment

highPrioQueue := batch.NewJobQueue(this, jsii.String("JobQueue"), &JobQueueProps{
	ComputeEnvironments: []jobQueueComputeEnvironment{
		&jobQueueComputeEnvironment{
			ComputeEnvironment: sharedComputeEnvs,
			Order: jsii.Number(1),
		},
	},
	Priority: jsii.Number(2),
})

lowPrioQueue := batch.NewJobQueue(this, jsii.String("JobQueue"), &JobQueueProps{
	ComputeEnvironments: []*jobQueueComputeEnvironment{
		&jobQueueComputeEnvironment{
			ComputeEnvironment: sharedComputeEnvs,
			Order: jsii.Number(1),
		},
	},
	Priority: jsii.Number(1),
})

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:

jobQueue := batch.JobQueue_FromJobQueueArn(this, jsii.String("imported-job-queue"), jsii.String("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 ecr "github.com/aws/aws-cdk-go/awscdk"


repo := ecr.Repository_FromRepositoryName(this, jsii.String("batch-job-repo"), jsii.String("todo-list"))

batch.NewJobDefinition(this, jsii.String("batch-job-def-from-ecr"), &JobDefinitionProps{
	Container: &JobDefinitionContainer{
		Image: ecs.NewEcrImage(repo, jsii.String("latest")),
	},
})

Using a local Docker project

Below is an example of how you can create a Batch Job Definition from a local Docker application.

batch.NewJobDefinition(this, jsii.String("batch-job-def-from-local"), &JobDefinitionProps{
	Container: &JobDefinitionContainer{
		// todo-list is a directory containing a Dockerfile to build the application
		Image: ecs.ContainerImage_FromAsset(jsii.String("../todo-list")),
	},
})

Providing custom log configuration

You can provide custom log driver and its configuration for the container.

import ssm "github.com/aws/aws-cdk-go/awscdk"


batch.NewJobDefinition(this, jsii.String("job-def"), &JobDefinitionProps{
	Container: &JobDefinitionContainer{
		Image: ecs.EcrImage_FromRegistry(jsii.String("docker/whalesay")),
		LogConfiguration: &LogConfiguration{
			LogDriver: batch.LogDriver_AWSLOGS,
			Options: map[string]*string{
				"awslogs-region": jsii.String("us-east-1"),
			},
			SecretOptions: []exposedSecret{
				batch.*exposedSecret_FromParametersStore(jsii.String("xyz"), ssm.StringParameter_FromStringParameterName(this, jsii.String("parameter"), jsii.String("xyz"))),
			},
		},
	},
})

Importing an existing Job Definition

From ARN

To import an existing batch job definition from its ARN, call JobDefinition.fromJobDefinitionArn().

Below is an example:

job := batch.JobDefinition_FromJobDefinitionArn(this, jsii.String("imported-job-definition"), jsii.String("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:

// Without revision
job1 := batch.JobDefinition_FromJobDefinitionName(this, jsii.String("imported-job-definition"), jsii.String("my-job-definition"))

// With revision
job2 := batch.JobDefinition_FromJobDefinitionName(this, jsii.String("imported-job-definition"), jsii.String("my-job-definition:3"))

# Functions

No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
Fetches an existing batch compute environment by its amazon resource name.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
User Parameters Store Parameter.
Use Secrets Manager Secret.
Imports an existing batch job definition by its amazon resource name.
Imports an existing batch job definition by its name.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Fetches an existing batch job queue by its amazon resource name.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Create a new `AWS::Batch::ComputeEnvironment`.
Create a new `AWS::Batch::ComputeEnvironment`.
Create a new `AWS::Batch::JobDefinition`.
Create a new `AWS::Batch::JobDefinition`.
Create a new `AWS::Batch::JobQueue`.
Create a new `AWS::Batch::JobQueue`.
Create a new `AWS::Batch::SchedulingPolicy`.
Create a new `AWS::Batch::SchedulingPolicy`.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.

# Constants

Batch will use the best fitting instance type will be used when assigning a batch job in this compute environment.
Batch will select additional instance types that are large enough to meet the requirements of the jobs in the queue, with a preference for instance types with a lower cost per unit vCPU.
This is only available for Spot Instance compute resources and will select additional instance types that are large enough to meet the requirements of the jobs in the queue, with a preference for instance types that are less likely to be interrupted.
Resources will be Fargate resources.
Resources will be Fargate Spot resources.
Resources will be EC2 On-Demand resources.
Resources will be EC2 SpotFleet resources.
Specifies the Amazon CloudWatch Logs logging driver.
Specifies the Fluentd logging driver.
Specifies the Graylog Extended Format (GELF) logging driver.
Specifies the journald logging driver.
Specifies the JSON file logging driver.
Specifies the logentries logging driver.
Specifies the Splunk logging driver.
Specifies the syslog logging driver.
Specifies EC2 environment.
Specifies Fargate environment.

# Structs

Details about the compute resources managed by the compute environment.
Provides information used to select Amazon Machine Images (AMIs) for instances in the compute environment.
Configuration for the Amazon EKS cluster that supports the AWS Batch compute environment.
An object that represents a launch template that's associated with a compute resource.
Specifies the infrastructure update policy for the compute environment.
Properties for defining a `CfnComputeEnvironment`.
The authorization configuration details for the Amazon EFS file system.
Container properties are used for Amazon ECS based job definitions.
An object that represents a container instance host device.
This is used when you're using an Amazon Elastic File System file system for job storage.
An environment variable.
EKS 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.
The volume mounts for a container for an Amazon EKS job.
An object that contains the properties for the Kubernetes resources of a job.
Example: // The code below shows an example of how to instantiate this type.
Specifies an Amazon EKS volume for a job definition.
Example: // The code below shows an example of how to instantiate this type.
The Environment property type specifies environment variables to use in a job definition.
Example: // The code below shows an example of how to instantiate this type.
Specifies an array of up to 5 conditions to be met, and an action to take ( `RETRY` or `EXIT` ) if all conditions are met.
The platform configuration for jobs that are running on Fargate resources.
Example: // The code below shows an example of how to instantiate this type.
Linux-specific modifications that are applied to the container, such as details for device mappings.
Log configuration options to send to a custom log driver for the container.
Example: // The code below shows an example of how to instantiate this type.
Details for a Docker volume mount point that's used in a job's container properties.
The network configuration for jobs that are running on Fargate resources.
An object that represents the node properties of a multi-node parallel job.
An object that represents the properties of the node range for a multi-node parallel job.
The properties for the pod.
The type and amount of a resource to assign to a container.
Example: // The code below shows an example of how to instantiate this type.
The retry strategy that's associated with a job.
An object that represents the secret to expose to your container.
Example: // The code below shows an example of how to instantiate this type.
An object that represents a job timeout configuration.
The container path, mount options, and size of the `tmpfs` mount.
The `ulimit` settings to pass to the container.
Determine whether your data volume persists on the host container instance and where it's stored.
A list of volumes that are associated with the job.
Properties for defining a `CfnJobDefinition`.
The order that compute environments are tried in for job placement within a queue.
Properties for defining a `CfnJobQueue`.
The fair share policy for a scheduling policy.
Specifies the weights for the fair share identifiers for the fair share policy.
Properties for defining a `CfnSchedulingPolicy`.
Properties for creating a new Compute Environment.
Properties for defining the structure of the batch compute cluster.
Properties of a job definition container.
Construction properties of the {@link JobDefinition} construct.
Properties for mapping a compute environment to a job queue.
Properties of a batch job queue.
Launch template property specification.
Log configuration options to send to a custom log driver for the container.

# Interfaces

A CloudFormation `AWS::Batch::ComputeEnvironment`.
A CloudFormation `AWS::Batch::JobDefinition`.
A CloudFormation `AWS::Batch::JobQueue`.
A CloudFormation `AWS::Batch::SchedulingPolicy`.
Batch Compute Environment.
Exposed secret for log configuration.
Properties of a compute environment.
An interface representing a job definition - either a new one, created with the CDK, *using the {@link JobDefinition} class, or existing ones, referenced using the {@link JobDefinition.fromJobDefinitionArn} method.
Properties of a Job Queue.
Properties for specifying multi-node properties for compute resources.
Properties for a multi-node batch job.
Batch Job Definition.
Batch Job Queue.

# Type aliases

Properties for how to prepare compute resources that are provisioned for a compute environment.
Property to specify if the compute environment uses On-Demand, SpotFleet, Fargate, or Fargate Spot compute resources.
The log driver to use for the container.
Platform capabilities.