createComputeEnvironment

Creates an Batch compute environment. You can create MANAGED or UNMANAGED 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.

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.

Batch doesn't automatically upgrade the AMIs in a compute environment after it's created. For more information on how to update a compute environment's AMI, see Updating compute environments in the Batch User Guide.

Samples

import aws.sdk.kotlin.services.batch.model.CeState
import aws.sdk.kotlin.services.batch.model.CeType
import aws.sdk.kotlin.services.batch.model.ComputeResource
import aws.sdk.kotlin.services.batch.model.CrType

fun main() { 
   //sampleStart 
   // This example creates a managed compute environment with specific C4 instance types that are launched
// on demand. The compute environment is called C4OnDemand.
val resp = batchClient.createComputeEnvironment {
    computeEnvironmentName = "C4OnDemand"
    state = CeState.fromValue("ENABLED")
    type = CeType.fromValue("MANAGED")
    computeResources = ComputeResource {
        subnets = listOf<String>(
            "subnet-220c0e0a",
            "subnet-1a95556d",
            "subnet-978f6dce"
        )
        tags = mapOf<String, String>(
            "Name" to "Batch Instance - C4OnDemand"
        )
        desiredvCpus = 48
        minvCpus = 0
        instanceTypes = listOf<String>(
            "c4.large",
            "c4.xlarge",
            "c4.2xlarge",
            "c4.4xlarge",
            "c4.8xlarge"
        )
        securityGroupIds = listOf<String>(
            "sg-cf5093b2"
        )
        instanceRole = "ecsInstanceRole"
        maxvCpus = 128
        type = CrType.fromValue("EC2")
        ec2KeyPair = "id_rsa"
    }
    serviceRole = "arn:aws:iam::012345678910:role/AWSBatchServiceRole"
} 
   //sampleEnd
}
import aws.sdk.kotlin.services.batch.model.CeState
import aws.sdk.kotlin.services.batch.model.CeType
import aws.sdk.kotlin.services.batch.model.ComputeResource
import aws.sdk.kotlin.services.batch.model.CrType

fun main() { 
   //sampleStart 
   // 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.
val resp = batchClient.createComputeEnvironment {
    computeEnvironmentName = "M4Spot"
    state = CeState.fromValue("ENABLED")
    type = CeType.fromValue("MANAGED")
    computeResources = ComputeResource {
        subnets = listOf<String>(
            "subnet-220c0e0a",
            "subnet-1a95556d",
            "subnet-978f6dce"
        )
        type = CrType.fromValue("SPOT")
        spotIamFleetRole = "arn:aws:iam::012345678910:role/aws-ec2-spot-fleet-role"
        tags = mapOf<String, String>(
            "Name" to "Batch Instance - M4Spot"
        )
        desiredvCpus = 4
        minvCpus = 0
        instanceTypes = listOf<String>(
            "m4"
        )
        securityGroupIds = listOf<String>(
            "sg-cf5093b2"
        )
        instanceRole = "ecsInstanceRole"
        maxvCpus = 128
        bidPercentage = 20
        ec2KeyPair = "id_rsa"
    }
    serviceRole = "arn:aws:iam::012345678910:role/AWSBatchServiceRole"
} 
   //sampleEnd
}