Batch Constructs¶
AWS Batch infrastructure, compute environments, and job management.
batch
¶
Modules¶
infrastructure
¶
AWS Batch infrastructure constructs.
This module provides CDK constructs for creating and managing AWS Batch compute environments, job queues, and related infrastructure.
Classes¶
Batch
¶
Batch(
scope: Construct,
id: str,
env_base: EnvBase,
vpc: IVpc,
instance_role_name: str | None = None,
instance_role_policy_statements: list[PolicyStatement]
| None = None,
)
Bases: EnvBaseConstruct
AWS Batch infrastructure construct for creating multiple Batch environments.
This construct simplifies the creation of Batch Environments. It allows for the creation of multiple Batch Environments with different configurations and launch templates, but using the same instance role and security group.
Note
Instance Roles are created with managed policies commonly used by Batch jobs, including access to S3, Lambda, and DynamoDB.
Defines
- Batch Compute Environment (Spot and OnDemand)
- Instance Role
- Launch Template
- Queue(s)
Initialize Batch infrastructure construct.
Creates the shared infrastructure for Batch Environments. Has the ability to create multiple Batch Environments with different configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
The construct scope. |
required |
id
|
str
|
The construct ID. |
required |
env_base
|
EnvBase
|
Environment base to use for resource naming. |
required |
vpc
|
IVpc
|
VPC to use for Batch infrastructure. |
required |
instance_role_name
|
Optional[str]
|
Name for the instance role. Defaults to None (auto-generated). |
None
|
instance_role_policy_statements
|
Optional[List[PolicyStatement]]
|
Additional policy statements for the instance role. Defaults to None. |
None
|
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
environments
property
¶environments: list[BatchEnvironment]
Get all Batch environments sorted by name.
Returns:
| Type | Description |
|---|---|
list[BatchEnvironment]
|
List of BatchEnvironment instances. |
create_instance_role
¶create_instance_role(
role_name: str | None = None,
statements: list[PolicyStatement] | None = None,
) -> Role
Create an IAM role for Batch EC2 instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role_name
|
Optional[str]
|
Name for the role. Defaults to None. |
None
|
statements
|
Optional[List[PolicyStatement]]
|
Additional policy statements to attach. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Role
|
The created IAM role. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
create_instance_profile
¶Create an instance profile for the Batch instance role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance_role_name
|
str
|
Name of the IAM role to associate. |
required |
Returns:
| Type | Description |
|---|---|
CfnInstanceProfile
|
The created instance profile. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
create_security_group
¶Create a security group for Batch instances.
Returns:
| Type | Description |
|---|---|
SecurityGroup
|
The created security group with all outbound and self-ingress allowed. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
grant_instance_role_permissions
¶grant_instance_role_permissions(
read_write_resources: Iterable[
Bucket | FileSystem | IFileSystem
]
| None = None,
read_only_resources: Iterable[
Bucket | FileSystem | IFileSystem
]
| None = None,
) -> None
Grant the instance role permissions to access resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
read_write_resources
|
Optional[Iterable[Union[Bucket, FileSystem, IFileSystem]]]
|
Resources to grant read/write access to. |
None
|
read_only_resources
|
Optional[Iterable[Union[Bucket, FileSystem, IFileSystem]]]
|
Resources to grant read-only access to. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unsupported resource type is provided. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
setup_batch_environment
¶setup_batch_environment(
descriptor: IBatchEnvironmentDescriptor,
config: BatchEnvironmentConfig,
launch_template_builder: IBatchLaunchTemplateBuilder
| None = None,
) -> BatchEnvironment
Set up a new Batch environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptor
|
IBatchEnvironmentDescriptor
|
Descriptor defining the environment. |
required |
config
|
BatchEnvironmentConfig
|
Configuration for the environment. |
required |
launch_template_builder
|
Optional[IBatchLaunchTemplateBuilder]
|
Optional builder for creating a launch template. |
None
|
Returns:
| Type | Description |
|---|---|
BatchEnvironment
|
The created BatchEnvironment construct. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
BatchEnvironmentConfig
dataclass
¶
BatchEnvironmentConfig(
allocation_strategy: AllocationStrategy | None,
instance_types: list[str | InstanceType] | None,
use_public_subnets: bool,
use_spot: bool,
use_fargate: bool = False,
maxv_cpus: int | None = DEFAULT_MAXV_CPUS,
minv_cpus: int | None = DEFAULT_MINV_CPUS,
)
Configuration for a Batch compute environment.
Attributes:
| Name | Type | Description |
|---|---|---|
allocation_strategy |
Optional[AllocationStrategy]
|
The allocation strategy for the compute environment. |
instance_types |
Optional[List[Union[str, InstanceType]]]
|
Instance types to use in the compute environment. |
use_public_subnets |
bool
|
Whether to use public subnets. |
use_spot |
bool
|
Whether to use spot instances. |
use_fargate |
bool
|
Whether to use Fargate. Defaults to False. |
maxv_cpus |
Optional[int]
|
Maximum vCPUs for the environment. Defaults to 10240. |
minv_cpus |
Optional[int]
|
Minimum vCPUs for the environment. Defaults to 0. |
BatchEnvironment
¶
BatchEnvironment(
scope: Construct,
id: str,
env_base: EnvBase,
descriptor: IBatchEnvironmentDescriptor,
config: BatchEnvironmentConfig,
vpc: IVpc,
instance_role: IRole,
security_group: SecurityGroup,
launch_template_builder: IBatchLaunchTemplateBuilder
| None = None,
)
Bases: EnvBaseConstruct
A single Batch compute environment with its job queue.
This construct creates a Batch compute environment and associated job queue. It supports both Fargate and EC2 compute environments, with customizable configurations and launch templates.
Initialize a BatchEnvironment construct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Construct
|
The construct scope. |
required |
id
|
str
|
The construct ID. |
required |
env_base
|
EnvBase
|
Environment base for resource naming. |
required |
descriptor
|
IBatchEnvironmentDescriptor
|
Environment descriptor. |
required |
config
|
BatchEnvironmentConfig
|
Environment configuration. |
required |
vpc
|
IVpc
|
VPC for the environment. |
required |
instance_role
|
IRole
|
IAM role for instances. |
required |
security_group
|
SecurityGroup
|
Security group for instances. |
required |
launch_template_builder
|
Optional[IBatchLaunchTemplateBuilder]
|
Optional launch template builder. |
None
|
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
descriptor
property
¶Get the environment descriptor.
Returns:
| Type | Description |
|---|---|
IBatchEnvironmentDescriptor
|
The batch environment descriptor. |
job_queue_name
property
¶Get the job queue name.
Returns:
| Type | Description |
|---|---|
str
|
The name of the job queue. |
instance_types
property
¶Get the configured instance types.
Returns:
| Type | Description |
|---|---|
Sequence[InstanceType] | None
|
List of instance types, or None if not configured. |
vpc_subnets
property
¶Get the VPC subnet selection.
Returns:
| Type | Description |
|---|---|
SubnetSelection | None
|
SubnetSelection for public subnets if configured, None otherwise. |
launch_template_user_data_hash
property
¶Get a hash of the launch template user data.
Returns:
| Type | Description |
|---|---|
str | None
|
SHA256 hash of user data, or None if no launch template. |
compute_resource_tags
property
¶Get tags for compute resources.
Includes a hash of user data to force recreation when launch template changes.
Returns:
| Type | Description |
|---|---|
Mapping[str, str] | None
|
Dictionary of tags, or None for Fargate environments. |
compute_resource_type
property
¶Get the compute resource type.
Returns:
| Type | Description |
|---|---|
Literal['ON_DEMAND', 'SPOT', 'FARGATE', 'FARGATE_SPOT']
|
The compute resource type based on configuration. |
launch_template
¶Get or create the launch template.
Returns:
| Type | Description |
|---|---|
LaunchTemplate | None
|
The launch template, or None for Fargate environments. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
create_compute_environment
¶Create the compute environment.
Returns:
| Type | Description |
|---|---|
IComputeEnvironment
|
A Fargate or EC2 compute environment based on configuration. |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
grant_file_system_access
¶Grant compute environments access to EFS file systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*file_systems
|
IFileSystem
|
Variable number of file systems to grant access to. |
()
|
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/infrastructure.py
Functions¶
launch_template
¶
Classes¶
CloudWatchConfigBuilder
dataclass
¶
to_json
¶Builds a CW Agent config
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html¶
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: config |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/launch_template.py
get_logs_config
¶Generates CW Agent sub config for logs
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html#CloudWatch-Agent-Configuration-File-Logssection
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: CW logs config |
Source code in src/aibs_informatics_cdk_lib/constructs_/batch/launch_template.py
get_metrics_config
¶Generates metrics config section of CW Agent config
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html#CloudWatch-Agent-Configuration-File-Metricssection
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict[str, Any]: metrics config |