Using service-linked roles for Amazon Batch - Amazon Batch
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using service-linked roles for Amazon Batch

Amazon Batch uses Amazon Identity and Access Management (IAM) service-linked roles. A service-linked role is a unique type of IAM role that is linked directly to Amazon Batch. Service-linked roles are predefined by Amazon Batch and include all the permissions that the service requires to call other Amazon services on your behalf.

A service-linked role makes setting up Amazon Batch easier because you don’t have to manually add the necessary permissions. Amazon Batch defines the permissions of its service-linked roles, and unless defined otherwise, only Amazon Batch can assume its roles. The defined permissions include the trust policy and the permissions policy, and that permissions policy cannot be attached to any other IAM entity.

Note

Do one of the following to specify a service role for an Amazon Batch compute environment.

  • Use an empty string for the service role. This lets Amazon Batch create the service role.

  • Specify the service role in the following format: arn:aws-cn:iam::account_number:role/aws-service-role/batch.amazonaws.com/AWSServiceRoleForBatch.

For more information, see Incorrect role name or ARN in the Amazon Batch User Guide.

You can delete a service-linked role only after first deleting their related resources. This protects your Amazon Batch resources because you can't inadvertently remove permission to access the resources.

For information about other services that support service-linked roles, see Amazon Services That Work with IAM and look for the services that have Yes in the Service-Linked Role column. Choose a Yes with a link to view the service-linked role documentation for that service.

Service-linked role permissions for Amazon Batch

Amazon Batch uses the service-linked role named AWSServiceRoleForBatch. The AWSServiceRoleForBatch role allows Amazon Batch to create and manage Amazon resources on your behalf.

The AWSServiceRoleForBatch service-linked role trusts the batch.amazonaws.com service principal to assume the role.

The IAM policy named BatchServiceRolePolicy allows Amazon Batch to complete the following actions on specific resources:

  • autoscaling – Allows Amazon Batch to create and manage Amazon EC2 Auto Scaling resources. Amazon Batch creates and manages Amazon EC2 Auto Scaling groups for most compute environments.

  • ec2 – Allows Amazon Batch to control the lifecycle of Amazon EC2 instances as well as create and manage launch templates and tags. Amazon Batch creates and manages EC2 Spot Fleet requests for some EC2 Spot compute environments.

  • ecs - Allows Amazon Batch to create and managed Amazon ECS clusters, task-definitions and tasks for job execution.

  • eks - Allows Amazon Batch to describe the Amazon EKS cluster resource for validations.

  • iam - Allows Amazon Batch to validate and pass roles provided by owner to Amazon EC2, Amazon EC2 Auto Scaling and Amazon ECS.

  • logs – Allows Amazon Batch to create and manage log groups and log streams for Amazon Batch jobs.

You must configure permissions to allow an IAM entity (such as a user, group, or role) to create, edit, or delete a service-linked role. For more information, see Service-Linked Role Permissions in the IAM User Guide.

Creating a service-linked role for Amazon Batch

You don't need to manually create a service-linked role. When you CreateComputeEnvironment in the Amazon Web Services Management Console, the Amazon CLI, or the Amazon API, and don't specify a value for the serviceRole parameter, Amazon Batch creates the service-linked role for you.

Important

This service-linked role can appear in your account if you completed an action in another service that uses the features supported by this role. Also, if you were using the Amazon Batch service before March 10, 2021, when it began supporting service-linked roles, then Amazon Batch created the AWSServiceRoleForBatch role in your account. To learn more, see A New Role Appeared in My IAM Account.

If you delete this service-linked role, and then need to create it again, you can use the same process to recreate the role in your account. When you CreateComputeEnvironment, Amazon Batch creates the service-linked role for you again.

Editing a service-linked role for Amazon Batch

With Amazon Batch, you can't edit the AWSServiceRoleForBatch service-linked role. After you create a service-linked role, you can't change the name of the role because various entities might reference the role. However, you can edit the description of the role using IAM. For more information, see Editing a Service-Linked Role in the IAM User Guide.

To allow an IAM entity to edit the description of the AWSServiceRoleForBatch service-linked role

Add the following statement to the permissions policy. This allows the IAM entity to edit the description of a service-linked role.

{ "Effect": "Allow", "Action": [ "iam:UpdateRoleDescription" ], "Resource": "arn:aws-cn:iam::*:role/aws-service-role/batch.amazonaws.com/AWSServiceRoleForBatch", "Condition": {"StringLike": {"iam:AWSServiceName": "batch.amazonaws.com"}} }

Deleting a service-linked role for Amazon Batch

We recommend, if you no longer need to use a feature or service that requires a service-linked role, you delete that role. That way, you don’t have an unused entity that's not actively monitored or maintained. However, you must clean up the resources for your service-linked role before you can manually delete it.

To allow an IAM entity to delete the AWSServiceRoleForBatch service-linked role

Add the following statement to the permissions policy. This allows the IAM entity to delete a service-linked role.

{ "Effect": "Allow", "Action": [ "iam:DeleteServiceLinkedRole", "iam:GetServiceLinkedRoleDeletionStatus" ], "Resource": "arn:aws-cn:iam::*:role/aws-service-role/batch.amazonaws.com/AWSServiceRoleForBatch", "Condition": {"StringLike": {"iam:AWSServiceName": "batch.amazonaws.com"}} }

Cleaning up a service-linked role

Before you can use IAM to delete a service-linked role, you must first confirm that the role has no active sessions and delete all of the Amazon Batch compute environments that use the role in all Amazon Regions in a single partition.

To check whether the service-linked role has an active session
  1. Open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane, choose Roles and then the AWSServiceRoleForBatch name (not the check box).

  3. On the Summary page, choose Access Advisor and review recent activity for the service-linked role.

    Note

    If you don't know whether Amazon Batch is using the AWSServiceRoleForBatch role, you can try to delete the role. If the service is using the role, then the role will fail to delete. You can view the Regions where the role is being used. If the role is being used, then you must wait for the session to end before you can delete the role. You can't revoke the session for a service-linked role.

To remove Amazon Batch resources used by the AWSServiceRoleForBatch service-linked role

You must delete all Amazon Batch compute environments that use the AWSServiceRoleForBatch role in all Amazon Regions before you can delete the AWSServiceRoleForBatch role.

  1. Open the Amazon Batch console at https://console.amazonaws.cn/batch/.

  2. From the navigation bar, select the Region to use.

  3. In the navigation pane, choose Compute environments.

  4. Select the compute environment.

  5. Choose Disable. Wait for the State to change to DISABLED.

  6. Select the compute environment.

  7. Choose Delete. Confirm that you want to delete the compute environment by choosing Delete compute environment.

  8. Repeat steps 1–7 for all compute environments that use the service-linked role in all Regions.

Deleting a service-linked role in IAM (Console)

You can use the IAM console to delete a service-linked role.

To delete a service-linked role (console)
  1. Sign in to the Amazon Web Services Management Console and open the IAM console at https://console.amazonaws.cn/iam/.

  2. In the navigation pane of the IAM console, choose Roles. Then select the check box next to AWSServiceRoleForBatch, not the name or row itself.

  3. Choose Delete role.

  4. In the confirmation dialog box, review the service last accessed data, which shows when each of the selected roles last accessed an Amazon Web Service. This helps you to confirm whether the role is currently active. If you want to proceed, choose Yes, Delete to submit the service-linked role for deletion.

  5. Watch the IAM console notifications to monitor the progress of the service-linked role deletion. Because the IAM service-linked role deletion is asynchronous, after you submit the role for deletion, the deletion task can succeed or fail.

    • If the task succeeds, then the role is removed from the list and a notification of success appears at the top of the page.

    • If the task fails, you can choose View details or View Resources from the notifications to learn why the deletion failed. If the deletion fails because the role is using the service's resources, then the notification includes a list of resources, if the service returns that information. You can then clean up the resources and submit the deletion again.

      Note

      You might have to repeat this process several times, depending on the information that the service returns. For example, your service-linked role might use six resources and your service might return information about five of them. If you clean up the five resources and submit the role for deletion again, the deletion fails and the service reports the one remaining resource. A service might return all of the resources, a few of them, or it might not report any resources.

    • If the task fails and the notification does not include a list of resources, then the service might not return that information. To learn how to clean up the resources for that service, see Amazon Web Services that work with IAM. Find your service in the table, and choose the Yes link to view the service-linked role documentation for that service.

Deleting a service-linked role in IAM (Amazon CLI)

You can use IAM commands from the Amazon Command Line Interface to delete a service-linked role.

To delete a service-linked role (CLI)
  1. Because a service-linked role can't be deleted if it's being used or has associated resources, you must submit a deletion request. That request can be denied if these conditions aren't met. You must capture the deletion-task-id from the response to check the status of the deletion task. Enter the following command to submit a service-linked role deletion request:

    $ aws iam delete-service-linked-role --role-name AWSServiceRoleForBatch
  2. Use the following command to check the status of the deletion task:

    $ aws iam get-service-linked-role-deletion-status --deletion-task-id deletion-task-id

    The status of the deletion task can be NOT_STARTED, IN_PROGRESS, SUCCEEDED, or FAILED. If the deletion fails, the call returns the reason that it failed so that you can troubleshoot. If the deletion fails because the role is using the service's resources, then the notification includes a list of resources, if the service returns that information. You can then clean up the resources and submit the deletion again.

    Note

    You might have to repeat this process several times, depending on the information that the service returns. For example, your service-linked role might use six resources and your service might return information about five of them. If you clean up the five resources and submit the role for deletion again, the deletion fails and the service reports the one remaining resource. A service might return all of the resources, a few of them. Or, it might not report any resources. To learn how to clean up the resources for a service that doesn't report any resources, see Amazon services that work with IAM. Find your service in the table, and choose the Yes link to view the service-linked role documentation for that service.

Deleting a service-linked role in IAM (AmazonAPI)

You can use the IAM API to delete a service-linked role.

To delete a service-linked role (API)
  1. To submit a deletion request for a service-linked roll, call DeleteServiceLinkedRole. In the request, specify the AWSServiceRoleForBatch role name.

    Because a service-linked role cannot be deleted if it is being used or has associated resources, you must submit a deletion request. That request can be denied if these conditions are not met. You must capture the DeletionTaskId from the response to check the status of the deletion task.

  2. To check the status of the deletion, call GetServiceLinkedRoleDeletionStatus. In the request, specify the DeletionTaskId.

    The status of the deletion task can be NOT_STARTED, IN_PROGRESS, SUCCEEDED, or FAILED. If the deletion fails, the call returns the reason that it failed so that you can troubleshoot. If the deletion fails because the role is using the service's resources, then the notification includes a list of resources, if the service returns that information. You can then clean up the resources and submit the deletion again.

    Note

    You might have to repeat this process several times, depending on the information that the service returns. For example, your service-linked role might use six resources and your service might return information about five of them. If you clean up the five resources and submit the role for deletion again, the deletion fails and the service reports the one remaining resource. A service might return all of the resources, a few of them, or it might not report any resources. To learn how to clean up the resources for a service that does not report any resources, see Amazon Web Services that work with IAM. Find your service in the table, and choose the Yes link to view the service-linked role documentation for that service.

Supported Regions for Amazon Batch service-linked roles

Amazon Batch supports using service-linked roles in all of the Regions where the service is available. For more information, see Amazon Batch endpoints.