Batch / Client / create_compute_environment
create_compute_environment#
- Batch.Client.create_compute_environment(**kwargs)#
Creates an Batch compute environment. You can create
MANAGED
orUNMANAGED
compute environments.MANAGED
compute environments can use Amazon EC2 or Fargate resources.UNMANAGED
compute environments can only use EC2 resources.In a managed compute environment, Batch manages the capacity and instance types of the compute resources within the environment. This is based on the compute resource specification that you define or the launch template that you specify when you create the compute environment. Either, you can choose to use EC2 On-Demand Instances and EC2 Spot Instances. Or, you can use Fargate and Fargate Spot capacity in your managed compute environment. You can optionally set a maximum price so that Spot Instances only launch when the Spot Instance price is less than a specified percentage of the On-Demand price.
Note
Multi-node parallel jobs aren’t supported on Spot Instances.
In an unmanaged compute environment, you can manage your own EC2 compute resources and have flexibility with how you configure your compute resources. For example, you can use custom AMIs. However, you must verify that each of your AMIs meet the Amazon ECS container instance AMI specification. For more information, see container instance AMIs in the Amazon Elastic Container Service Developer Guide. After you created your unmanaged compute environment, you can use the DescribeComputeEnvironments operation to find the Amazon ECS cluster that’s associated with it. Then, launch your container instances into that Amazon ECS cluster. For more information, see Launching an Amazon ECS container instance in the Amazon Elastic Container Service Developer Guide.
Note
To create a compute environment that uses EKS resources, the caller must have permissions to call
eks:DescribeCluster
.Note
Batch doesn’t automatically upgrade the AMIs in a compute environment after it’s created. For example, it also doesn’t update the AMIs in your compute environment when a newer version of the Amazon ECS optimized AMI is available. You’re responsible for the management of the guest operating system. This includes any updates and security patches. You’re also responsible for any additional application software or utilities that you install on the compute resources. There are two ways to use a new AMI for your Batch jobs. The original method is to complete these steps:
Create a new compute environment with the new AMI.
Add the compute environment to an existing job queue.
Remove the earlier compute environment from your job queue.
Delete the earlier compute environment.
In April 2022, Batch added enhanced support for updating compute environments. For more information, see Updating compute environments. To use the enhanced updating of compute environments to update AMIs, follow these rules:
Either don’t set the service role (
serviceRole
) parameter or set it to the AWSBatchServiceRole service-linked role.Set the allocation strategy (
allocationStrategy
) parameter toBEST_FIT_PROGRESSIVE
orSPOT_CAPACITY_OPTIMIZED
.Set the update to latest image version (
updateToLatestImageVersion
) parameter totrue
. TheupdateToLatestImageVersion
parameter is used when you update a compute environment. This parameter is ignored when you create a compute environment.Don’t specify an AMI ID in
imageId
,imageIdOverride
(in ec2Configuration), or in the launch template (launchTemplate
). In that case, Batch selects the latest Amazon ECS optimized AMI that’s supported by Batch at the time the infrastructure update is initiated. Alternatively, you can specify the AMI ID in theimageId
orimageIdOverride
parameters, or the launch template identified by theLaunchTemplate
properties. Changing any of these properties starts an infrastructure update. If the AMI ID is specified in the launch template, it can’t be replaced by specifying an AMI ID in either theimageId
orimageIdOverride
parameters. It can only be replaced by specifying a different launch template, or if the launch template version is set to$Default
or$Latest
, by setting either a new default version for the launch template (if$Default
) or by adding a new version to the launch template (if$Latest
).
If these rules are followed, any update that starts an infrastructure update causes the AMI ID to be re-selected. If the
version
setting in the launch template (launchTemplate
) is set to$Latest
or$Default
, the latest or default version of the launch template is evaluated up at the time of the infrastructure update, even if thelaunchTemplate
wasn’t updated.See also: AWS API Documentation
Request Syntax
response = client.create_compute_environment( computeEnvironmentName='string', type='MANAGED'|'UNMANAGED', state='ENABLED'|'DISABLED', unmanagedvCpus=123, computeResources={ 'type': 'EC2'|'SPOT'|'FARGATE'|'FARGATE_SPOT', 'allocationStrategy': 'BEST_FIT'|'BEST_FIT_PROGRESSIVE'|'SPOT_CAPACITY_OPTIMIZED', 'minvCpus': 123, 'maxvCpus': 123, 'desiredvCpus': 123, 'instanceTypes': [ 'string', ], 'imageId': 'string', 'subnets': [ 'string', ], 'securityGroupIds': [ 'string', ], 'ec2KeyPair': 'string', 'instanceRole': 'string', 'tags': { 'string': 'string' }, 'placementGroup': 'string', 'bidPercentage': 123, 'spotIamFleetRole': 'string', 'launchTemplate': { 'launchTemplateId': 'string', 'launchTemplateName': 'string', 'version': 'string' }, 'ec2Configuration': [ { 'imageType': 'string', 'imageIdOverride': 'string', 'imageKubernetesVersion': 'string' }, ] }, serviceRole='string', tags={ 'string': 'string' }, eksConfiguration={ 'eksClusterArn': 'string', 'kubernetesNamespace': 'string' } )
- Parameters:
computeEnvironmentName (string) –
[REQUIRED]
The name for your compute environment. It can be up to 128 characters long. It can contain uppercase and lowercase letters, numbers, hyphens (-), and underscores (_).
type (string) –
[REQUIRED]
The type of the compute environment:
MANAGED
orUNMANAGED
. For more information, see Compute Environments in the Batch User Guide.state (string) –
The state of the compute environment. If the state is
ENABLED
, then the compute environment accepts jobs from a queue and can scale out automatically based on queues.If the state is
ENABLED
, then the Batch scheduler can attempt to place jobs from an associated job queue on the compute resources within the environment. If the compute environment is managed, then it can scale its instances out or in automatically, based on the job queue demand.If the state is
DISABLED
, then the Batch scheduler doesn’t attempt to place jobs within the environment. Jobs in aSTARTING
orRUNNING
state continue to progress normally. Managed compute environments in theDISABLED
state don’t scale out.Note
Compute environments in a
DISABLED
state may continue to incur billing charges. To prevent additional charges, turn off and then delete the compute environment. For more information, see State in the Batch User Guide.When an instance is idle, the instance scales down to the
minvCpus
value. However, the instance size doesn’t change. For example, consider ac5.8xlarge
instance with aminvCpus
value of4
and adesiredvCpus
value of36
. This instance doesn’t scale down to ac5.large
instance.unmanagedvCpus (integer) –
The maximum number of vCPUs for an unmanaged compute environment. This parameter is only used for fair share scheduling to reserve vCPU capacity for new share identifiers. If this parameter isn’t provided for a fair share job queue, no vCPU capacity is reserved.
Note
This parameter is only supported when the
type
parameter is set toUNMANAGED
.computeResources (dict) –
Details about the compute resources managed by the compute environment. This parameter is required for managed compute environments. For more information, see Compute Environments in the Batch User Guide.
type (string) – [REQUIRED]
The type of compute environment:
EC2
,SPOT
,FARGATE
, orFARGATE_SPOT
. For more information, see Compute environments in the Batch User Guide.If you choose
SPOT
, you must also specify an Amazon EC2 Spot Fleet role with thespotIamFleetRole
parameter. For more information, see Amazon EC2 spot fleet role in the Batch User Guide.allocationStrategy (string) –
The allocation strategy to use for the compute resource if not enough instances of the best fitting instance type can be allocated. This might be because of availability of the instance type in the Region or Amazon EC2 service limits. For more information, see Allocation strategies in the Batch User Guide.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
BEST_FIT (default)
Batch selects an instance type that best fits the needs of the jobs with a preference for the lowest-cost instance type. If additional instances of the selected instance type aren’t available, Batch waits for the additional instances to be available. If there aren’t enough instances available or the user is reaching Amazon EC2 service limits, additional jobs aren’t run until the currently running jobs are completed. This allocation strategy keeps costs lower but can limit scaling. If you’re using Spot Fleets with
BEST_FIT
, the Spot Fleet IAM Role must be specified. Compute resources that use aBEST_FIT
allocation strategy don’t support infrastructure updates and can’t update some parameters. For more information, see Updating compute environments in the Batch User Guide.BEST_FIT_PROGRESSIVE
Batch selects additional instance types that are large enough to meet the requirements of the jobs in the queue. Its preference is for instance types with lower cost vCPUs. If additional instances of the previously selected instance types aren’t available, Batch selects new instance types.
SPOT_CAPACITY_OPTIMIZED
Batch selects one or more instance types that are large enough to meet the requirements of the jobs in the queue. Its preference is for instance types that are less likely to be interrupted. This allocation strategy is only available for Spot Instance compute resources.
With both
BEST_FIT_PROGRESSIVE
andSPOT_CAPACITY_OPTIMIZED
strategies using On-Demand or Spot Instances, and theBEST_FIT
strategy using Spot Instances, Batch might need to exceedmaxvCpus
to meet your capacity requirements. In this event, Batch never exceedsmaxvCpus
by more than a single instance.minvCpus (integer) –
The minimum number of vCPUs that a compute environment should maintain (even if the compute environment is
DISABLED
).Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
maxvCpus (integer) – [REQUIRED]
The maximum number of vCPUs that a compute environment can support.
Note
With both
BEST_FIT_PROGRESSIVE
andSPOT_CAPACITY_OPTIMIZED
allocation strategies using On-Demand or Spot Instances, and theBEST_FIT
strategy using Spot Instances, Batch might need to exceedmaxvCpus
to meet your capacity requirements. In this event, Batch never exceedsmaxvCpus
by more than a single instance. For example, no more than a single instance from among those specified in your compute environment is allocated.desiredvCpus (integer) –
The desired number of vCPUS in the compute environment. Batch modifies this value between the minimum and maximum values based on job queue demand.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
instanceTypes (list) –
The instances types that can be launched. You can specify instance families to launch any instance type within those families (for example,
c5
orp3
), or you can specify specific sizes within a family (such asc5.8xlarge
). You can also chooseoptimal
to select instance types (from the C4, M4, and R4 instance families) that match the demand of your job queues.Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
Note
When you create a compute environment, the instance types that you select for the compute environment must share the same architecture. For example, you can’t mix x86 and ARM instances in the same compute environment.
Note
Currently,
optimal
uses instance types from the C4, M4, and R4 instance families. In Regions that don’t have instance types from those instance families, instance types from the C5, M5, and R5 instance families are used.(string) –
imageId (string) –
The Amazon Machine Image (AMI) ID used for instances launched in the compute environment. This parameter is overridden by the
imageIdOverride
member of theEc2Configuration
structure.Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
Note
The AMI that you choose for a compute environment must match the architecture of the instance types that you intend to use for that compute environment. For example, if your compute environment uses A1 instance types, the compute resource AMI that you choose must support ARM instances. Amazon ECS vends both x86 and ARM versions of the Amazon ECS-optimized Amazon Linux 2 AMI. For more information, see Amazon ECS-optimized Amazon Linux 2 AMI in the Amazon Elastic Container Service Developer Guide.
subnets (list) – [REQUIRED]
The VPC subnets where the compute resources are launched. These subnets must be within the same VPC. Fargate compute resources can contain up to 16 subnets. For more information, see VPCs and subnets in the Amazon VPC User Guide.
Note
Batch on Amazon EC2 and Batch on Amazon EKS support Local Zones. For more information, see Local Zones in the Amazon EC2 User Guide for Linux Instances, Amazon EKS and Amazon Web Services Local Zones in the Amazon EKS User Guide and Amazon ECS clusters in Local Zones, Wavelength Zones, and Amazon Web Services Outposts in the Amazon ECS Developer Guide.
Batch on Fargate doesn’t currently support Local Zones.
(string) –
securityGroupIds (list) –
The Amazon EC2 security groups that are associated with instances launched in the compute environment. One or more security groups must be specified, either in
securityGroupIds
or using a launch template referenced inlaunchTemplate
. This parameter is required for jobs that are running on Fargate resources and must contain at least one security group. Fargate doesn’t support launch templates. If security groups are specified using bothsecurityGroupIds
andlaunchTemplate
, the values insecurityGroupIds
are used.(string) –
ec2KeyPair (string) –
The Amazon EC2 key pair that’s used for instances launched in the compute environment. You can use this key pair to log in to your instances with SSH.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
instanceRole (string) –
The Amazon ECS instance profile applied to Amazon EC2 instances in a compute environment. You can specify the short name or full Amazon Resource Name (ARN) of an instance profile. For example,
ecsInstanceRole
or ``arn:aws:iam::<aws_account_id>:instance-profile/ecsInstanceRole ``. For more information, see Amazon ECS instance role in the Batch User Guide.Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
tags (dict) –
Key-value pair tags to be applied to EC2 resources that are launched in the compute environment. For Batch, these take the form of
"String1": "String2"
, whereString1
is the tag key andString2
is the tag value-for example,{ "Name": "Batch Instance - C4OnDemand" }
. This is helpful for recognizing your Batch instances in the Amazon EC2 console. Updating these tags requires an infrastructure update to the compute environment. For more information, see Updating compute environments in the Batch User Guide. These tags aren’t seen when using the BatchListTagsForResource
API operation.Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
(string) –
(string) –
placementGroup (string) –
The Amazon EC2 placement group to associate with your compute resources. If you intend to submit multi-node parallel jobs to your compute environment, you should consider creating a cluster placement group and associate it with your compute resources. This keeps your multi-node parallel job on a logical grouping of instances within a single Availability Zone with high network flow potential. For more information, see Placement groups in the Amazon EC2 User Guide for Linux Instances.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
bidPercentage (integer) –
The maximum percentage that a Spot Instance price can be when compared with the On-Demand price for that instance type before instances are launched. For example, if your maximum percentage is 20%, then the Spot price must be less than 20% of the current On-Demand price for that Amazon EC2 instance. You always pay the lowest (market) price and never more than your maximum percentage. If you leave this field empty, the default value is 100% of the On-Demand price. For most use cases, we recommend leaving this field empty.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
spotIamFleetRole (string) –
The Amazon Resource Name (ARN) of the Amazon EC2 Spot Fleet IAM role applied to a
SPOT
compute environment. This role is required if the allocation strategy set toBEST_FIT
or if the allocation strategy isn’t specified. For more information, see Amazon EC2 spot fleet role in the Batch User Guide.Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
Warning
To tag your Spot Instances on creation, the Spot Fleet IAM role specified here must use the newer AmazonEC2SpotFleetTaggingRole managed policy. The previously recommended AmazonEC2SpotFleetRole managed policy doesn’t have the required permissions to tag Spot Instances. For more information, see Spot instances not tagged on creation in the Batch User Guide.
launchTemplate (dict) –
The launch template to use for your compute resources. Any other compute resource parameters that you specify in a CreateComputeEnvironment API operation override the same parameters in the launch template. You must specify either the launch template ID or launch template name in the request, but not both. For more information, see Launch template support in the Batch User Guide.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
launchTemplateId (string) –
The ID of the launch template.
launchTemplateName (string) –
The name of the launch template.
version (string) –
The version number of the launch template,
$Latest
, or$Default
.If the value is
$Latest
, the latest version of the launch template is used. If the value is$Default
, the default version of the launch template is used.Warning
If the AMI ID that’s used in a compute environment is from the launch template, the AMI isn’t changed when the compute environment is updated. It’s only changed if the
updateToLatestImageVersion
parameter for the compute environment is set totrue
. During an infrastructure update, if either$Latest
or$Default
is specified, Batch re-evaluates the launch template version, and it might use a different version of the launch template. This is the case even if the launch template isn’t specified in the update. When updating a compute environment, changing the launch template requires an infrastructure update of the compute environment. For more information, see Updating compute environments in the Batch User Guide.Default:
$Default
.
ec2Configuration (list) –
Provides information that’s used to select Amazon Machine Images (AMIs) for EC2 instances in the compute environment. If
Ec2Configuration
isn’t specified, the default isECS_AL2
.One or two values can be provided.
Note
This parameter isn’t applicable to jobs that are running on Fargate resources. Don’t specify it.
(dict) –
Provides information used to select Amazon Machine Images (AMIs) for instances in the compute environment. If
Ec2Configuration
isn’t specified, the default isECS_AL2
( Amazon Linux 2).Note
This object isn’t applicable to jobs that are running on Fargate resources.
imageType (string) – [REQUIRED]
The image type to match with the instance type to select an AMI. The supported values are different for
ECS
andEKS
resources.ECS
If the
imageIdOverride
parameter isn’t specified, then a recent Amazon ECS-optimized Amazon Linux 2 AMI (ECS_AL2
) is used. If a new image type is specified in an update, but neither animageId
nor aimageIdOverride
parameter is specified, then the latest Amazon ECS optimized AMI for that image type that’s supported by Batch is used.ECS_AL2
Amazon Linux 2: Default for all non-GPU instance families.
ECS_AL2_NVIDIA
Amazon Linux 2 (GPU): Default for all GPU instance families (for example
P4
andG4
) and can be used for all non Amazon Web Services Graviton-based instance types.ECS_AL1
Amazon Linux. Amazon Linux has reached the end-of-life of standard support. For more information, see Amazon Linux AMI.
EKS
If the
imageIdOverride
parameter isn’t specified, then a recent Amazon EKS-optimized Amazon Linux AMI (EKS_AL2
) is used. If a new image type is specified in an update, but neither animageId
nor aimageIdOverride
parameter is specified, then the latest Amazon EKS optimized AMI for that image type that Batch supports is used.EKS_AL2
Amazon Linux 2: Default for all non-GPU instance families.
EKS_AL2_NVIDIA
Amazon Linux 2 (accelerated): Default for all GPU instance families (for example,
P4
andG4
) and can be used for all non Amazon Web Services Graviton-based instance types.imageIdOverride (string) –
The AMI ID used for instances launched in the compute environment that match the image type. This setting overrides the
imageId
set in thecomputeResource
object.Note
The AMI that you choose for a compute environment must match the architecture of the instance types that you intend to use for that compute environment. For example, if your compute environment uses A1 instance types, the compute resource AMI that you choose must support ARM instances. Amazon ECS vends both x86 and ARM versions of the Amazon ECS-optimized Amazon Linux 2 AMI. For more information, see Amazon ECS-optimized Amazon Linux 2 AMI in the Amazon Elastic Container Service Developer Guide.
imageKubernetesVersion (string) –
The Kubernetes version for the compute environment. If you don’t specify a value, the latest version that Batch supports is used.
serviceRole (string) –
The full Amazon Resource Name (ARN) of the IAM role that allows Batch to make calls to other Amazon Web Services services on your behalf. For more information, see Batch service IAM role in the Batch User Guide.
Warning
If your account already created the Batch service-linked role, that role is used by default for your compute environment unless you specify a different role here. If the Batch service-linked role doesn’t exist in your account, and no role is specified here, the service attempts to create the Batch service-linked role in your account.
If your specified role has a path other than
/
, then you must specify either the full role ARN (recommended) or prefix the role name with the path. For example, if a role with the namebar
has a path of/foo/
, specify/foo/bar
as the role name. For more information, see Friendly names and paths in the IAM User Guide.Note
Depending on how you created your Batch service role, its ARN might contain the
service-role
path prefix. When you only specify the name of the service role, Batch assumes that your ARN doesn’t use theservice-role
path prefix. Because of this, we recommend that you specify the full ARN of your service role when you create compute environments.tags (dict) –
The tags that you apply to the compute environment to help you categorize and organize your resources. Each tag consists of a key and an optional value. For more information, see Tagging Amazon Web Services Resources in Amazon Web Services General Reference.
These tags can be updated or removed using the TagResource and UntagResource API operations. These tags don’t propagate to the underlying compute resources.
(string) –
(string) –
eksConfiguration (dict) –
The details for the Amazon EKS cluster that supports the compute environment.
eksClusterArn (string) – [REQUIRED]
The Amazon Resource Name (ARN) of the Amazon EKS cluster. An example is ``arn:aws:eks:us-east-1:123456789012:cluster/ClusterForBatch ``.
kubernetesNamespace (string) – [REQUIRED]
The namespace of the Amazon EKS cluster. Batch manages pods in this namespace. The value can’t left empty or null. It must be fewer than 64 characters long, can’t be set to
default
, can’t start with “kube-
,” and must match this regular expression:^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
. For more information, see Namespaces in the Kubernetes documentation.
- Return type:
dict
- Returns:
Response Syntax
{ 'computeEnvironmentName': 'string', 'computeEnvironmentArn': 'string' }
Response Structure
(dict) –
computeEnvironmentName (string) –
The name of the compute environment. It can be up to 128 characters long. It can contain uppercase and lowercase letters, numbers, hyphens (-), and underscores (_).
computeEnvironmentArn (string) –
The Amazon Resource Name (ARN) of the compute environment.
Exceptions
Batch.Client.exceptions.ClientException
Batch.Client.exceptions.ServerException
Examples
This example creates a managed compute environment with specific C4 instance types that are launched on demand. The compute environment is called C4OnDemand.
response = client.create_compute_environment( type='MANAGED', computeEnvironmentName='C4OnDemand', computeResources={ 'type': 'EC2', 'desiredvCpus': 48, 'ec2KeyPair': 'id_rsa', 'instanceRole': 'ecsInstanceRole', 'instanceTypes': [ 'c4.large', 'c4.xlarge', 'c4.2xlarge', 'c4.4xlarge', 'c4.8xlarge', ], 'maxvCpus': 128, 'minvCpus': 0, 'securityGroupIds': [ 'sg-cf5093b2', ], 'subnets': [ 'subnet-220c0e0a', 'subnet-1a95556d', 'subnet-978f6dce', ], 'tags': { 'Name': 'Batch Instance - C4OnDemand', }, }, serviceRole='arn:aws:iam::012345678910:role/AWSBatchServiceRole', state='ENABLED', ) print(response)
Expected Output:
{ 'computeEnvironmentArn': 'arn:aws:batch:us-east-1:012345678910:compute-environment/C4OnDemand', 'computeEnvironmentName': 'C4OnDemand', 'ResponseMetadata': { '...': '...', }, }
This example creates a managed compute environment with the M4 instance type that is launched when the Spot bid price is at or below 20% of the On-Demand price for the instance type. The compute environment is called M4Spot.
response = client.create_compute_environment( type='MANAGED', computeEnvironmentName='M4Spot', computeResources={ 'type': 'SPOT', 'bidPercentage': 20, 'desiredvCpus': 4, 'ec2KeyPair': 'id_rsa', 'instanceRole': 'ecsInstanceRole', 'instanceTypes': [ 'm4', ], 'maxvCpus': 128, 'minvCpus': 0, 'securityGroupIds': [ 'sg-cf5093b2', ], 'spotIamFleetRole': 'arn:aws:iam::012345678910:role/aws-ec2-spot-fleet-role', 'subnets': [ 'subnet-220c0e0a', 'subnet-1a95556d', 'subnet-978f6dce', ], 'tags': { 'Name': 'Batch Instance - M4Spot', }, }, serviceRole='arn:aws:iam::012345678910:role/AWSBatchServiceRole', state='ENABLED', ) print(response)
Expected Output:
{ 'computeEnvironmentArn': 'arn:aws:batch:us-east-1:012345678910:compute-environment/M4Spot', 'computeEnvironmentName': 'M4Spot', 'ResponseMetadata': { '...': '...', }, }