Mark Harrison
Mark Harrison

Reputation: 304584

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

I would like to add a role to an existing (running?) instance. Is this possible programmatically?

Upvotes: 2

Views: 468

Answers (2)

rocky
rocky

Reputation: 163

Now you can attach a role to Running instance from Console and from CLI as well

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

Official Announcment here

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.

For more information. Click here

Upvotes: 2

helloV
helloV

Reputation: 52423

It is not possible (not even in AWS dashboard). You can add an IAM role only when launching an instance

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.

Upvotes: 6

Related Questions