卓:如何以编程方式向实例添加角色?

AWS: How do I programmatically add a role to an instance?

我想向现有 (运行?) 实例添加角色。这可以通过编程方式实现吗?

这是不可能的(甚至在 AWS 仪表板中也不可能)。您只能在启动实例时添加 IAM 角色

https://aws.amazon.com/iam/faqs/

Q: Can I change the IAM role on a running EC2 instance? No, at this time you cannot change the IAM role on a running EC2 instance. You can change the permissions on the IAM role associated with a running instance, and the updated permissions will take effect almost immediately.

现在您可以从控制台和 CLI 将角色附加到 运行 实例

aws ec2 associate-iam-instance-profile --instance-id *InstanceId* --iam-instance-profile Name=*NewInstanceProfileName*

官宣在这里

You can now attach or replace an AWS Identity and Access Management (IAM) role to your existing Amazon EC2 instance. IAM roles enable your applications running on EC2 to use temporary security credentials that AWS creates, distributes, and rotates automatically. Using temporary credentials reduces the risk of long-term key compromise.

了解更多信息。 Click here