

# 将 `AddRoleToInstanceProfile` 与 CLI 配合使用
<a name="iam_example_iam_AddRoleToInstanceProfile_section"></a>

以下代码示例演示如何使用 `AddRoleToInstanceProfile`。

------
#### [ CLI ]

**AWS CLI**  
**向实例配置文件添加角色**  
以下 `add-role-to-instance-profile` 命令可将名为 `S3Access` 的角色添加到名为 `Webserver` 的实例配置文件。  

```
aws iam add-role-to-instance-profile \
    --role-name S3Access \
    --instance-profile-name Webserver
```
此命令不生成任何输出。  
要创建实例配置文件，请使用 `create-instance-profile` 命令。  
有关更多信息，请参阅《AWS IAM 用户指南》**中的[使用 IAM 角色为 Amazon EC2 实例上运行的应用程序授予权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [AddRoleToInstanceProfile](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/iam/add-role-to-instance-profile.html)。

------
#### [ PowerShell ]

**适用于 PowerShell V4 的工具**  
**示例 1：此命令将名为 `S3Access` 的角色添加到名为 `webserver` 的现有实例配置文件中。要创建实例配置文件，请使用 `New-IAMInstanceProfile` 命令。使用此命令创建实例配置文件并将其与角色关联后，您可以将其附加到 EC2 实例。为此，请使用带有 `InstanceProfile_Arn` 或 `InstanceProfile-Name` 参数的 `New-EC2Instance` cmdlet 来启动新实例。**  

```
Add-IAMRoleToInstanceProfile -RoleName "S3Access" -InstanceProfileName "webserver"
```
+  有关 API 详细信息，请参阅《AWS Tools for PowerShell Cmdlet Reference (V4)》**中的 [AddRoleToInstanceProfile](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**示例 1：此命令将名为 `S3Access` 的角色添加到名为 `webserver` 的现有实例配置文件中。要创建实例配置文件，请使用 `New-IAMInstanceProfile` 命令。使用此命令创建实例配置文件并将其与角色关联后，您可以将其附加到 EC2 实例。为此，请使用带有 `InstanceProfile_Arn` 或 `InstanceProfile-Name` 参数的 `New-EC2Instance` cmdlet 来启动新实例。**  

```
Add-IAMRoleToInstanceProfile -RoleName "S3Access" -InstanceProfileName "webserver"
```
+  有关 API 详细信息，请参阅《*AWS Tools for PowerShell Cmdlet 参考 (V5)*》中的 [AddRoleToInstanceProfile](https://docs.aws.amazon.com/powershell/v5/reference)。

------

有关 AWS SDK 开发人员指南和代码示例的完整列表，请参阅 [将此服务与 AWS SDK 结合使用](sdk-general-information-section.md) 本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。