|
| 1 | +resource "aws_batch_compute_environment" "cpu" { |
| 2 | + /* Unique name for compute environment. |
| 3 | + We use compute_environment_name_prefix opposed to just compute_environment_name as batch compute environments must |
| 4 | + be created and destroyed, never edited. This way when we go to make a "modification" we will stand up a new |
| 5 | + batch compute environment with a new unique name and once that succeeds, the old one will be torn down. If we had |
| 6 | + just used compute_environment_name, then there would be a conflict when we went to stand up the new |
| 7 | + compute_environment that had the modifications applied and the process would fail. |
| 8 | + */ |
| 9 | + compute_environment_name_prefix = local.cpu_compute_env_prefix_name |
| 10 | + |
| 11 | + # Give permissions so the batch service can make API calls. |
| 12 | + service_role = aws_iam_role.batch_service_role.arn |
| 13 | + type = "MANAGED" |
| 14 | + depends_on = [aws_iam_role_policy_attachment.batch_service_role] |
| 15 | + |
| 16 | + compute_resources { |
| 17 | + # Give permissions so the ECS container instances can make API call. |
| 18 | + instance_role = aws_iam_instance_profile.ecs_instance_role.arn |
| 19 | + |
| 20 | + # List of types that can be launched. |
| 21 | + instance_type = var.batch_cpu_instance_types |
| 22 | + |
| 23 | + # Range of number of CPUs. |
| 24 | + max_vcpus = var.batch_compute_environment_cpu_max_vcpus |
| 25 | + min_vcpus = var.batch_compute_environment_cpu_min_vcpus |
| 26 | + desired_vcpus = var.batch_compute_environment_cpu_desired_vcpus |
| 27 | + |
| 28 | + # Prefers cheap vCPU approaches |
| 29 | + allocation_strategy = "BEST_FIT" |
| 30 | + |
| 31 | + /* Links to a launch template who has more than the standard 8GB of disk space. So we can download training data. |
| 32 | + Always uses the "default version", which means we can update the Launch Template to a smaller or larger disk size |
| 33 | + and this compute environment will not have to be destroyed and then created to point to a new Launch Template. |
| 34 | + */ |
| 35 | + launch_template { |
| 36 | + launch_template_id = aws_launch_template.this.id |
| 37 | + version = aws_launch_template.this.latest_version |
| 38 | + } |
| 39 | + |
| 40 | + # Security group to apply to the instances launched. |
| 41 | + security_group_ids = [ |
| 42 | + aws_security_group.batch.id, |
| 43 | + ] |
| 44 | + |
| 45 | + # Which subnet to launch the instances into. |
| 46 | + subnets = [ |
| 47 | + var.subnet_private_1_id, |
| 48 | + var.subnet_private_2_id |
| 49 | + ] |
| 50 | + |
| 51 | + # Type of instance EC2 for on-demand. Can use "SPOT" to use unused instances at discount if available |
| 52 | + type = "EC2" |
| 53 | + |
| 54 | + tags = var.standard_tags |
| 55 | + } |
| 56 | + |
| 57 | + lifecycle { |
| 58 | + /* From here https://github.com/terraform-providers/terraform-provider-aws/issues/11077#issuecomment-560416740 |
| 59 | + helps with "modifying" batch compute environments which requires creating new ones and deleting old ones |
| 60 | + as no inplace modification can be made |
| 61 | + */ |
| 62 | + create_before_destroy = true |
| 63 | + # To ensure terraform redeploys do not silently overwrite an up to date desired_vcpus that metaflow may modify |
| 64 | + ignore_changes = [compute_resources.0.desired_vcpus] |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +resource "aws_batch_compute_environment" "large-cpu" { |
| 69 | + /* Unique name for compute environment. |
| 70 | + We use compute_environment_name_prefix opposed to just compute_environment_name as batch compute environments must |
| 71 | + be created and destroyed, never edited. This way when we go to make a "modification" we will stand up a new |
| 72 | + batch compute environment with a new unique name and once that succeeds, the old one will be torn down. If we had |
| 73 | + just used compute_environment_name, then there would be a conflict when we went to stand up the new |
| 74 | + compute_environment that had the modifications applied and the process would fail. |
| 75 | + */ |
| 76 | + compute_environment_name_prefix = local.large_cpu_compute_env_prefix_name |
| 77 | + |
| 78 | + # Give permissions so the batch service can make API calls. |
| 79 | + service_role = aws_iam_role.batch_service_role.arn |
| 80 | + type = "MANAGED" |
| 81 | + depends_on = [aws_iam_role_policy_attachment.batch_service_role] |
| 82 | + |
| 83 | + compute_resources { |
| 84 | + # Give permissions so the ECS container instances can make API call. |
| 85 | + instance_role = aws_iam_instance_profile.ecs_instance_role.arn |
| 86 | + |
| 87 | + # List of types that can be launched. |
| 88 | + instance_type = var.batch_large_cpu_instance_types |
| 89 | + |
| 90 | + # Range of number of CPUs. |
| 91 | + max_vcpus = var.batch_compute_environment_large_cpu_max_vcpus |
| 92 | + min_vcpus = var.batch_compute_environment_large_cpu_min_vcpus |
| 93 | + desired_vcpus = var.batch_compute_environment_large_cpu_desired_vcpus |
| 94 | + |
| 95 | + # Prefers cheap vCPU approaches |
| 96 | + allocation_strategy = "BEST_FIT" |
| 97 | + |
| 98 | + /* Links to a launch template who has more than the standard 8GB of disk space. So we can download training data. |
| 99 | + Always uses the "default version", which means we can update the Launch Template to a smaller or larger disk size |
| 100 | + and this compute environment will not have to be destroyed and then created to point to a new Launch Template. |
| 101 | + */ |
| 102 | + launch_template { |
| 103 | + launch_template_id = aws_launch_template.this.id |
| 104 | + version = aws_launch_template.this.latest_version |
| 105 | + } |
| 106 | + |
| 107 | + # Security group to apply to the instances launched. |
| 108 | + security_group_ids = [ |
| 109 | + aws_security_group.batch.id, |
| 110 | + ] |
| 111 | + |
| 112 | + # Which subnet to launch the instances into. |
| 113 | + subnets = [ |
| 114 | + var.subnet_private_1_id, |
| 115 | + var.subnet_private_2_id |
| 116 | + ] |
| 117 | + |
| 118 | + # Type of instance EC2 for on-demand. Can use "SPOT" to use unused instances at discount if available |
| 119 | + type = "EC2" |
| 120 | + |
| 121 | + tags = var.standard_tags |
| 122 | + } |
| 123 | + |
| 124 | + lifecycle { |
| 125 | + /* From here https://github.com/terraform-providers/terraform-provider-aws/issues/11077#issuecomment-560416740 |
| 126 | + helps with "modifying" batch compute environments which requires creating new ones and deleting old ones |
| 127 | + as no inplace modification can be made |
| 128 | + */ |
| 129 | + create_before_destroy = true |
| 130 | + # To ensure terraform redeploys do not silently overwrite an up to date desired_vcpus that metaflow may modify |
| 131 | + ignore_changes = [compute_resources.0.desired_vcpus] |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +resource "aws_batch_compute_environment" "gpu" { |
| 136 | + /* Unique name for compute environment. |
| 137 | + We use compute_environment_name_prefix opposed to just compute_environment_name as batch compute environments must |
| 138 | + be created and destroyed, never edited. This way when we go to make a "modification" we will stand up a new |
| 139 | + batch compute environment with a new unique name and once that succeeds, the old one will be torn down. If we had |
| 140 | + just used compute_environment_name, then there would be a conflict when we went to stand up the new |
| 141 | + compute_environment that had the modifications applied and the process would fail. |
| 142 | + */ |
| 143 | + compute_environment_name_prefix = local.gpu_compute_env_prefix_name |
| 144 | + |
| 145 | + # Give permissions so the batch service can make API calls. |
| 146 | + service_role = aws_iam_role.batch_service_role.arn |
| 147 | + type = "MANAGED" |
| 148 | + depends_on = [aws_iam_role_policy_attachment.batch_service_role] |
| 149 | + |
| 150 | + compute_resources { |
| 151 | + # Give permissions so the ECS container instances can make API call. |
| 152 | + instance_role = aws_iam_instance_profile.ecs_instance_role.arn |
| 153 | + |
| 154 | + # List of types that can be launched. |
| 155 | + instance_type = var.batch_gpu_instance_types |
| 156 | + |
| 157 | + # Range of number of CPUs. |
| 158 | + max_vcpus = var.batch_compute_environment_gpu_max_vcpus |
| 159 | + min_vcpus = var.batch_compute_environment_gpu_min_vcpus |
| 160 | + desired_vcpus = var.batch_compute_environment_gpu_desired_vcpus |
| 161 | + |
| 162 | + # Prefers cheap vCPU approaches |
| 163 | + allocation_strategy = "BEST_FIT" |
| 164 | + |
| 165 | + /* Links to a launch template who has more than the standard 8GB of disk space. So we can download training data. |
| 166 | + Always uses the "default version", which means we can update the Launch Template to a smaller or larger disk size |
| 167 | + and this compute environment will not have to be destroyed and then created to point to a new Launch Template. |
| 168 | + */ |
| 169 | + launch_template { |
| 170 | + launch_template_id = aws_launch_template.this.id |
| 171 | + version = aws_launch_template.this.latest_version |
| 172 | + } |
| 173 | + |
| 174 | + # Security group to apply to the instances launched. |
| 175 | + security_group_ids = [ |
| 176 | + aws_security_group.batch.id, |
| 177 | + ] |
| 178 | + |
| 179 | + # Which subnet to launch the instances into. |
| 180 | + subnets = [ |
| 181 | + var.subnet_private_1_id, |
| 182 | + var.subnet_private_2_id |
| 183 | + ] |
| 184 | + |
| 185 | + # Type of instance EC2 for on-demand. Can use "SPOT" to use unused instances at discount if available |
| 186 | + type = "EC2" |
| 187 | + |
| 188 | + tags = var.standard_tags |
| 189 | + } |
| 190 | + |
| 191 | + lifecycle { |
| 192 | + /* From here https://github.com/terraform-providers/terraform-provider-aws/issues/11077#issuecomment-560416740 |
| 193 | + helps with "modifying" batch compute environments which requires creating new ones and deleting old ones |
| 194 | + as no inplace modification can be made |
| 195 | + */ |
| 196 | + create_before_destroy = true |
| 197 | + # To ensure terraform redeploys do not silently overwrite an up to date desired_vcpus that metaflow may modify |
| 198 | + ignore_changes = [compute_resources.0.desired_vcpus] |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +resource "aws_batch_job_queue" "this" { |
| 203 | + name = local.batch_queue_name |
| 204 | + state = "ENABLED" |
| 205 | + priority = 1 |
| 206 | + compute_environments = [ |
| 207 | + aws_batch_compute_environment.cpu.arn, |
| 208 | + aws_batch_compute_environment.large-cpu.arn, |
| 209 | + aws_batch_compute_environment.gpu.arn |
| 210 | + ] |
| 211 | + |
| 212 | + tags = var.standard_tags |
| 213 | +} |
0 commit comments