将 AddRoleToInstanceProfile 与 Amazon SDK 或命令行工具配合使用 - Amazon Identity and Access Management
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

AddRoleToInstanceProfile 与 Amazon SDK 或命令行工具配合使用

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

CLI
Amazon 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 命令。

有关更多信息,请参阅《Amazon IAM 用户指南》中的使用 IAM 角色为 Amazon EC2 实例上运行的应用程序授予权限

PowerShell
适用于 PowerShell 的工具

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

Add-IAMRoleToInstanceProfile -RoleName "S3Access" -InstanceProfileName "webserver"

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 IAM 与 Amazon 开发工具包配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。