AddRoleToInstanceProfileCommand

Adds the specified IAM role to the specified instance profile. An instance profile can contain only one role, and this quota cannot be increased. You can remove the existing role and then add a different role to an instance profile. You must then wait for the change to appear across all of Amazon Web Services because of eventual consistency . To force the change, you must disassociate the instance profile  and then associate the instance profile , or you can stop your instance and then restart it.

The caller of this operation must be granted the PassRole permission on the IAM role by a permissions policy.

When using the iam:AssociatedResourceArn  condition in a policy to restrict the PassRole  IAM action, special considerations apply if the policy is intended to define access for the AddRoleToInstanceProfile action. In this case, you cannot specify a Region or instance ID in the EC2 instance ARN. The ARN value must be arn:aws:ec2:*:CallerAccountId:instance/*. Using any other ARN value may lead to unexpected evaluation results.

For more information about roles, see IAM roles  in the IAM User Guide. For more information about instance profiles, see Using instance profiles  in the IAM User Guide.

Example Syntax

Use a bare-bones client and the command you need to make an API call.

import { IAMClient, AddRoleToInstanceProfileCommand } from "@aws-sdk/client-iam"; // ES Modules import
// const { IAMClient, AddRoleToInstanceProfileCommand } = require("@aws-sdk/client-iam"); // CommonJS import
const client = new IAMClient(config);
const input = { // AddRoleToInstanceProfileRequest
  InstanceProfileName: "STRING_VALUE", // required
  RoleName: "STRING_VALUE", // required
};
const command = new AddRoleToInstanceProfileCommand(input);
const response = await client.send(command);
// {};

Example Usage

 Loading code editorLoading code editor

AddRoleToInstanceProfileCommand Input

Parameter
Type
Description
InstanceProfileName
Required
string | undefined

The name of the instance profile to update.

This parameter allows (through its regex pattern ) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: _+=,.-

RoleName
Required
string | undefined

The name of the role to add.

This parameter allows (through its regex pattern ) a string of characters consisting of upper and lowercase alphanumeric characters with no spaces. You can also include any of the following characters: _+=,.-

AddRoleToInstanceProfileCommand Output

Parameter
Type
Description
$metadata
Required
ResponseMetadata
Metadata pertaining to this request.

Throws

Name
Fault
Details
EntityAlreadyExistsException
client

The request was rejected because it attempted to create a resource that already exists.

LimitExceededException
client

The request was rejected because it attempted to create resources beyond the current Amazon Web Services account limits. The error message describes the limit exceeded.

NoSuchEntityException
client

The request was rejected because it referenced a resource entity that does not exist. The error message describes the resource.

ServiceFailureException
server

The request processing has failed because of an unknown error, exception or failure.

UnmodifiableEntityException
client

The request was rejected because service-linked roles are protected Amazon Web Services resources. Only the service that depends on the service-linked role can modify or delete the role on your behalf. The error message includes the name of the service that depends on this service-linked role. You must request the change through that service.

IAMServiceException
Base exception class for all service exceptions from IAM service.