diff --git a/modules/computation/README.md b/modules/computation/README.md
index 719b878..9eab847 100644
--- a/modules/computation/README.md
+++ b/modules/computation/README.md
@@ -20,6 +20,7 @@ To read more, see [the Metaflow docs](https://docs.metaflow.org/metaflow-on-aws/
| [compute\_environment\_instance\_types](#input\_compute\_environment\_instance\_types) | The instance types for the compute environment as a comma-separated list | `list(string)` | n/a | yes |
| [compute\_environment\_max\_vcpus](#input\_compute\_environment\_max\_vcpus) | Maximum VCPUs for Batch Compute Environment [16-96] | `number` | n/a | yes |
| [compute\_environment\_min\_vcpus](#input\_compute\_environment\_min\_vcpus) | Minimum VCPUs for Batch Compute Environment [0-16] for EC2 Batch Compute Environment (ignored for Fargate) | `number` | n/a | yes |
+| [custom\_batch\_service\_role\_arn](#input\_custom\_batch\_service\_role\_arn) | The ARN of a custom service role for AWS Batch managed services to leverage. For more information, refer to https://docs.aws.amazon.com/batch/latest/userguide/using-service-linked-roles.html | `string` | `null` | no |
| [iam\_partition](#input\_iam\_partition) | IAM Partition (Select aws-us-gov for AWS GovCloud, otherwise leave as is) | `string` | `"aws"` | no |
| [launch\_template\_http\_endpoint](#input\_launch\_template\_http\_endpoint) | Whether the metadata service is available. Can be 'enabled' or 'disabled' | `string` | `"enabled"` | no |
| [launch\_template\_http\_put\_response\_hop\_limit](#input\_launch\_template\_http\_put\_response\_hop\_limit) | The desired HTTP PUT response hop limit for instance metadata requests. Can be an integer from 1 to 64 | `number` | `2` | no |
diff --git a/modules/computation/batch.tf b/modules/computation/batch.tf
index 655e4a6..6b037ee 100644
--- a/modules/computation/batch.tf
+++ b/modules/computation/batch.tf
@@ -8,17 +8,7 @@ resource "aws_batch_compute_environment" "this" {
*/
compute_environment_name_prefix = local.compute_env_prefix_name
- # Give permissions so the batch service can make API calls.
- service_role = aws_iam_role.batch_execution_role.arn
- type = "MANAGED"
-
- # On destroy, this avoids removing these policies below until compute environments are destroyed
- depends_on = [
- aws_iam_role_policy.grant_iam_pass_role,
- aws_iam_role_policy.grant_custom_access_policy,
- aws_iam_role_policy.grant_iam_custom_policies,
- aws_iam_role_policy.grant_ec2_custom_policies,
- ]
+ type = "MANAGED"
compute_resources {
# Give permissions so the ECS container instances can make API call.
@@ -73,6 +63,15 @@ resource "aws_batch_compute_environment" "this" {
# To ensure terraform redeploys do not silently overwrite an up to date desired_vcpus that metaflow may modify
ignore_changes = [compute_resources.0.desired_vcpus]
}
+
+ /* Define a custom service role for AWS Batch. Usually this is not
+ needed, as AWS Batch will create a Service-Linked Role (SLR)
+ when you create your first compute environment.
+
+ For more information, refer to https://docs.aws.amazon.com/batch/latest/userguide/using-service-linked-roles.html.
+ */
+ service_role = var.custom_batch_service_role_arn
+
}
resource "aws_batch_job_queue" "this" {
diff --git a/modules/computation/iam-batch-execution.tf b/modules/computation/iam-batch-execution.tf
deleted file mode 100644
index 733cab6..0000000
--- a/modules/computation/iam-batch-execution.tf
+++ /dev/null
@@ -1,184 +0,0 @@
-data "aws_iam_policy_document" "batch_execution_role_assume_role" {
- statement {
- actions = [
- "sts:AssumeRole"
- ]
-
- effect = "Allow"
-
- principals {
- identifiers = [
- "batch.amazonaws.com",
- ]
- type = "Service"
- }
- }
-}
-
-resource "aws_iam_role" "batch_execution_role" {
- name = local.batch_execution_role_name
- # Learn more by reading this Terraform documentation https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/batch_compute_environment#argument-reference
- # Learn more by reading this AWS Batch documentation https://docs.aws.amazon.com/batch/latest/userguide/service_IAM_role.html
- description = "This role is passed to AWS Batch as a `service_role`. This allows AWS Batch to make calls to other AWS services on our behalf."
-
- assume_role_policy = data.aws_iam_policy_document.batch_execution_role_assume_role.json
-
- tags = var.standard_tags
-}
-
-data "aws_iam_policy_document" "iam_pass_role" {
- statement {
- actions = [
- "iam:PassRole"
- ]
-
- effect = "Allow"
-
- resources = [
- "*"
- ]
-
- condition {
- test = "StringEquals"
- variable = "iam:PassedToService"
- values = ["ec2.amazonaws.com", "ec2.amazonaws.com.cn", "ecs-tasks.amazonaws.com"]
- }
- }
-}
-
-data "aws_iam_policy_document" "custom_access_policy" {
- statement {
- actions = [
- "ec2:DescribeAccountAttributes",
- "ec2:DescribeInstances",
- "ec2:DescribeInstanceAttribute",
- "ec2:DescribeInstanceStatus",
- "ec2:DescribeSubnets",
- "ec2:DescribeSecurityGroups",
- "ec2:DescribeKeyPairs",
- "ec2:DescribeImages",
- "ec2:DescribeImageAttribute",
- "ec2:DescribeSpotInstanceRequests",
- "ec2:DescribeSpotFleetInstances",
- "ec2:DescribeSpotFleetRequests",
- "ec2:DescribeSpotPriceHistory",
- "ec2:DescribeVpcClassicLink",
- "ec2:DescribeLaunchTemplateVersions",
- "ec2:CreateLaunchTemplate",
- "ec2:DeleteLaunchTemplate",
- "ec2:RequestSpotFleet",
- "ec2:CancelSpotFleetRequests",
- "ec2:ModifySpotFleetRequest",
- "ec2:TerminateInstances",
- "ec2:RunInstances",
- "autoscaling:DescribeAccountLimits",
- "autoscaling:DescribeAutoScalingGroups",
- "autoscaling:DescribeLaunchConfigurations",
- "autoscaling:DescribeAutoScalingInstances",
- "autoscaling:CreateLaunchConfiguration",
- "autoscaling:CreateAutoScalingGroup",
- "autoscaling:UpdateAutoScalingGroup",
- "autoscaling:SetDesiredCapacity",
- "autoscaling:DeleteLaunchConfiguration",
- "autoscaling:DeleteAutoScalingGroup",
- "autoscaling:CreateOrUpdateTags",
- "autoscaling:SuspendProcesses",
- "autoscaling:PutNotificationConfiguration",
- "autoscaling:TerminateInstanceInAutoScalingGroup",
- "ecs:DescribeClusters",
- "ecs:DescribeContainerInstances",
- "ecs:DescribeTaskDefinition",
- "ecs:DescribeTasks",
- "ecs:ListClusters",
- "ecs:ListContainerInstances",
- "ecs:ListTaskDefinitionFamilies",
- "ecs:ListTaskDefinitions",
- "ecs:ListTasks",
- "ecs:CreateCluster",
- "ecs:DeleteCluster",
- "ecs:RegisterTaskDefinition",
- "ecs:DeregisterTaskDefinition",
- "ecs:RunTask",
- "ecs:StartTask",
- "ecs:StopTask",
- "ecs:UpdateContainerAgent",
- "ecs:DeregisterContainerInstance",
- "logs:CreateLogGroup",
- "logs:CreateLogStream",
- "logs:PutLogEvents",
- "logs:DescribeLogGroups",
- "iam:GetInstanceProfile",
- "iam:GetRole",
- ]
-
- effect = "Allow"
-
- resources = [
- "*"
- ]
- }
-}
-
-data "aws_iam_policy_document" "iam_custom_policies" {
- statement {
- actions = [
- "iam:CreateServiceLinkedRole"
- ]
-
- effect = "Allow"
-
- resources = [
- "*",
- ]
-
- condition {
- test = "StringEquals"
- variable = "iam:AWSServiceName"
- values = ["autoscaling.amazonaws.com", "ecs.amazonaws.com"]
- }
- }
-}
-
-data "aws_iam_policy_document" "ec2_custom_policies" {
- statement {
- actions = [
- "ec2:CreateTags"
- ]
-
- effect = "Allow"
-
- resources = [
- "*",
- ]
-
- condition {
- test = "StringEquals"
- variable = "ec2:CreateAction"
- values = ["RunInstances"]
- }
- }
-}
-
-resource "aws_iam_role_policy" "grant_iam_pass_role" {
- name = "iam_pass_role"
- role = aws_iam_role.batch_execution_role.name
- policy = data.aws_iam_policy_document.iam_pass_role.json
-}
-
-resource "aws_iam_role_policy" "grant_custom_access_policy" {
- name = "custom_access"
- role = aws_iam_role.batch_execution_role.name
- policy = data.aws_iam_policy_document.custom_access_policy.json
-}
-
-resource "aws_iam_role_policy" "grant_iam_custom_policies" {
- name = "iam_custom"
- role = aws_iam_role.batch_execution_role.name
- policy = data.aws_iam_policy_document.iam_custom_policies.json
-}
-
-resource "aws_iam_role_policy" "grant_ec2_custom_policies" {
- name = "ec2_custom"
- role = aws_iam_role.batch_execution_role.name
- policy = data.aws_iam_policy_document.ec2_custom_policies.json
-}
diff --git a/modules/computation/variables.tf b/modules/computation/variables.tf
index 92d11ba..cffca38 100644
--- a/modules/computation/variables.tf
+++ b/modules/computation/variables.tf
@@ -102,3 +102,10 @@ variable "launch_template_image_id" {
nullable = true
default = null
}
+
+variable "custom_batch_service_role_arn" {
+ type = string
+ description = "The ARN of a custom service role for AWS Batch managed services to leverage. For more information, refer to https://docs.aws.amazon.com/batch/latest/userguide/using-service-linked-roles.html"
+ nullable = true
+ default = null
+}