Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
将 IAM 角色附加到实例
在启动过程中或启动之后,您可以创建一个 IAM 角色并将其附加到实例。您也可以替换或分离 IAM 角色。
要在启动时使用 Amazon EC2 控制台将 IAM 角色附加到实例,请展开高级详细信息。对于 IAM 实例配置文件,选择 IAM 角色。
如果您是使用 IAM 控制台创建的 IAM 角色,则为您创建了实例配置文件,并提供了与角色相同的名称。如果使用 Amazon CLI、API 或 Amazon SDK 创建了 IAM 角色,则可能为实例配置文件指定了与角色不同的名称。
您可以将 IAM 角色附加到正在运行或已停止的实例。如果实例已附加 IAM 角色,则必须将其替换为新的 IAM 角色。
- Console
-
将 IAM 角色附加到实例
通过以下网址打开 Amazon EC2 控制台:https://console.aws.amazon.com/ec2/。
-
在导航窗格中,选择实例。
-
选择实例。
-
依次选择 Actions(操作)、Security(安全)和 Modify IAM role(修改 IAM 角色)。
-
对于 IAM 角色,选择 IAM 实例配置文件。
-
选择更新 IAM 角色。
- Amazon CLI
-
将 IAM 角色附加到实例
使用 associate-iam-instance-profile 命令将 IAM 角色附加到该实例。指定实例配置文件时,您可以使用实例配置文件的 Amazon 资源名称(ARN),也可以使用其名称。
aws ec2 associate-iam-instance-profile \
--instance-id i-1234567890abcdef0
\
--iam-instance-profile Name="TestRole-1
"
- PowerShell
-
将 IAM 角色附加到实例
使用 Register-EC2IamInstanceProfile cmdlet。
Register-EC2IamInstanceProfile `
-InstanceId i-1234567890abcdef0
`
-IamInstanceProfile_Name TestRole-1
要在已附加了 IAM 角色的实例上替换该 IAM 角色,该实例必须处于正在运行状态。如果要更改实例的 IAM 角色而先不分离现有角色,则您可以执行此操作。例如,您可以执行此操作,以确保正在实例上运行的应用程序所执行的 API 操作不会被中断。
- Console
-
替换实例的 IAM 角色
通过以下网址打开 Amazon EC2 控制台:https://console.aws.amazon.com/ec2/。
-
在导航窗格中,选择实例。
-
选择实例。
-
依次选择 Actions(操作)、Security(安全)和 Modify IAM role(修改 IAM 角色)。
-
对于 IAM 角色,选择 IAM 实例配置文件。
-
选择更新 IAM 角色。
- Amazon CLI
-
替换实例的 IAM 角色
-
需要时使用 describe-iam-instance-profile-associations 命令来获取关联 ID。
aws ec2 describe-iam-instance-profile-associations \
--filters Name=instance-id,Values=i-1234567890abcdef0
\
--query IamInstanceProfileAssociations.AssociationId
-
使用 replace-iam-instance-profile-association 命令。指定现有实例配置文件的关联 ID,以及新实例配置文件的 ARN 或名称。
aws ec2 replace-iam-instance-profile-association \
--association-id iip-assoc-0044d817db6c0a4ba
\
--iam-instance-profile Name="TestRole-2
"
- PowerShell
-
替换实例的 IAM 角色
-
需要时使用 Get-EC2IamInstanceProfileAssociation cmdlet 来获取关联 ID。
(Get-EC2IamInstanceProfileAssociation -Filter @{Name="instance-id"; Values="i-0636508011d8e966a
"}).AssociationId
-
使用 Set-EC2IamInstanceProfileAssociation cmdlet。指定现有实例配置文件的关联 ID,以及新实例配置文件的 ARN 或名称。
Set-EC2IamInstanceProfileAssociation `
-AssociationId iip-assoc-0044d817db6c0a4ba
`
-IamInstanceProfile_Name TestRole-2
您可以将 IAM 角色从正在运行或已停止的实例上分离。
- Console
-
从实例中分离 IAM 角色
通过以下网址打开 Amazon EC2 控制台:https://console.aws.amazon.com/ec2/。
-
在导航窗格中,选择实例。
-
选择实例。
-
依次选择 Actions(操作)、Security(安全)和 Modify IAM role(修改 IAM 角色)。
-
对于 IAM 角色,请选择无 IAM 角色。
-
选择更新 IAM 角色。
-
提示进行确认时,输入分离,然后选择分离。
- Amazon CLI
-
从实例中分离 IAM 角色
-
需要时使用 describe-iam-instance-profile-associations,来获取要替换的 IAM 实例配置文件的关联 ID。
aws ec2 describe-iam-instance-profile-associations \
--filters Name=instance-id,Values=i-1234567890abcdef0
\
--query IamInstanceProfileAssociations.AssociationId
-
使用 disassociate-iam-instance-profile 命令。
aws ec2 disassociate-iam-instance-profile --association-id iip-assoc-0044d817db6c0a4ba
- PowerShell
-