0

Is there any way to attach IAM role to an existing instance via AWS Tools for Windows PowerShell? I've seen in https://aws.amazon.com/blogs/security/new-attach-an-aws-iam-role-to-an-existing-amazon-ec2-instance-by-using-the-aws-cli/ that aws-cli is able do it, but I'd prefer powershell if I had a choice.

Shanjohn
  • 66
  • 2

2 Answers2

0

The command you are looking for is Register-EC2IamInstanceProfile, which generates a new profile association for a given Instance ID and existing Profile ARN.

https://docs.aws.amazon.com/ja_jp/powershell/latest/reference/items/Register-EC2IamInstanceProfile.html

The other answer is for associating an existing profile association; this command generates a new profile association, which I believe is what you are trying to do (and what I was trying to do, and what brought me to this page). I figured this out by using the Powershell command get-command -noun *IamInstance* and searching through all of the AWS cmdlets, looking for something that looked like the right command for the job.

Since this question is the top search result on this topic (how to associate IAM profiles with EC2 instances using Powershell) I figured I should add an answer despite it being three years old.

Formica
  • 101
0

You can do it in powershell, here is the command documentation: https://docs.aws.amazon.com/powershell/latest/reference/items/Set-EC2IamInstanceProfileAssociation.html

Darek
  • 466
  • 3
  • 6
  • Yes, I found this command too, but it is not clear for me what parameters does it use. `Set-EC2IamInstanceProfileAssociation-IamInstanceProfile_Arn -AssociationId -IamInstanceProfile_Name -Force ` Are IamInstanceProfile and IamInstanceProfile_Name related to the same resource? Also there is no AssociationId in a first place, until you link your instance to a role first time. I don't see something like "InstanceId" in parameters and I don't know how to make reference for target instance in this case – Shanjohn Mar 19 '18 at 09:28