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

// 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"
}
// 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"
}
// This example creates an ECS Managed Instances compute environment that targets On Demand Capacity
// Reservations for predictable capacity.
val resp = batchClient.createComputeEnvironment {
    computeEnvironmentName = "my-reserved-managed-instances-ce"
    state = CeState.fromValue("ENABLED")
    type = CeType.fromValue("MANAGED")
    computeResources = ComputeResource {
        type = CrType.fromValue("ECS_MANAGED_INSTANCES")
        maxvCpus = 512
        managedInstancesProvider = ManagedInstancesProvider {
            infrastructureRoleArn = "arn:aws:iam::123456789012:role/ecsInfrastructureRole"
            instanceLaunchTemplate = InstanceLaunchTemplate {
                ec2InstanceProfileArn = "arn:aws:iam::123456789012:instance-profile/ecsInstanceProfile"
                networkConfiguration = ManagedInstancesNetworkConfiguration {
                    subnets = listOf<String>(
                        "subnet-abcde012",
                        "subnet-bcde012a"
                    )
                    securityGroups = listOf<String>(
                        "sg-abcde012"
                    )
                }
                instanceRequirements = InstanceRequirementsRequest {
                    allowedInstanceTypes = listOf<String>(
                        "m5.xlarge",
                        "m5.2xlarge"
                    )
                }
                capacityReservations = CapacityReservationRequest {
                    reservationGroupArn = "arn:aws:ec2:us-east-1:123456789012:capacity-reservation-group/my-reservation-group"
                    reservationPreference = "RESERVATIONS_FIRST"
                }
            }
        }
    }
}
// This example creates a managed compute environment that uses ECS Managed Instances.
val resp = batchClient.createComputeEnvironment {
    computeEnvironmentName = "my-managed-instances-ce"
    state = CeState.fromValue("ENABLED")
    type = CeType.fromValue("MANAGED")
    computeResources = ComputeResource {
        type = CrType.fromValue("ECS_MANAGED_INSTANCES")
        maxvCpus = 256
        managedInstancesProvider = ManagedInstancesProvider {
            infrastructureRoleArn = "arn:aws:iam::123456789012:role/ecsInfrastructureRole"
            instanceLaunchTemplate = InstanceLaunchTemplate {
                ec2InstanceProfileArn = "arn:aws:iam::123456789012:instance-profile/ecsInstanceProfile"
                networkConfiguration = ManagedInstancesNetworkConfiguration {
                    subnets = listOf<String>(
                        "subnet-abcde012",
                        "subnet-bcde012a"
                    )
                    securityGroups = listOf<String>(
                        "sg-abcde012"
                    )
                }
            }
        }
    }
}
// This example creates a Spot backed ECS Managed Instances compute environment constrained to specific
// instance types.
val resp = batchClient.createComputeEnvironment {
    computeEnvironmentName = "my-spot-managed-instances-ce"
    state = CeState.fromValue("ENABLED")
    type = CeType.fromValue("MANAGED")
    computeResources = ComputeResource {
        type = CrType.fromValue("ECS_MANAGED_INSTANCES")
        maxvCpus = 1000
        managedInstancesProvider = ManagedInstancesProvider {
            infrastructureRoleArn = "arn:aws:iam::123456789012:role/ecsInfrastructureRole"
            instanceLaunchTemplate = InstanceLaunchTemplate {
                ec2InstanceProfileArn = "arn:aws:iam::123456789012:instance-profile/ecsInstanceProfile"
                networkConfiguration = ManagedInstancesNetworkConfiguration {
                    subnets = listOf<String>(
                        "subnet-abcde012",
                        "subnet-bcde012a"
                    )
                    securityGroups = listOf<String>(
                        "sg-abcde012"
                    )
                }
                instanceRequirements = InstanceRequirementsRequest {
                    allowedInstanceTypes = listOf<String>(
                        "m5.large",
                        "m5.xlarge",
                        "m6i.large",
                        "m6i.xlarge"
                    )
                }
                capacityOptionType = "SPOT"
            }
        }
    }
}