0

I have an EC2 instance (EC2_Windows_2019_Server_Test) which is attached to an "IAM role".

How can I know the Name of the "IAM role" attached to EC2 using PowerShell from EC2 Instance itself?

Nigam Rout
  • 141
  • 3
  • 9

2 Answers2

3

Using Powershell you should be able to get from the EC2 instance metadata

$instanceRole = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/iam/security-credentials")

Can find a list of available metadata here

maafk
  • 6,176
  • 5
  • 35
  • 58
  • Thanks @maafk for input. I did a small update (removed `role-name` from the URL) to your script and it worked. So the working script is: $instanceRole = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/iam/security-credentials/role-name") – Nigam Rout May 18 '20 at 17:27
  • 1
    A more streamlined command would be `Invoke-RestMethod http://169.254.169.254/latest/meta-data/iam/security-credentials` – Stoinov Nov 30 '21 at 23:30
0

Thanks to @maafk.

$instanceRole = (New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/iam/security-credentials/")
Nigam Rout
  • 141
  • 3
  • 9