AWS EC2 with IAM Role: No credentials specified or obtained from persisted/shell defaults.

Get-EC2Instance : No credentials specified or obtained from persisted/shell defaults.
At line:1 char:1
+ Get-EC2Instance
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe…2InstanceCmdlet:GetEC2InstanceCmdlet) [Get-EC2Insta
nce], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Amazon.PowerShell.Cmdlets.EC2.GetEC2InstanceCmdlet

For EC2 machines that are assigned IAM roles, AccessKeyId, SecreteAccessKey and SessionToken needs to be obtained frmo metadata before making AWS calls.


$iam = (Invoke-WebRequest http://169.254.169.254/latest/meta-data/iam/security-credentials).Content
$iamProfileInfo= ConvertFrom-Json (Invoke-WebRequest http://169.254.169.254/latest/meta-data/iam/security-credentials/$iam).Content
Set-AWSCredentials -AccessKey $iamProfileInfo.AccessKeyId -SecretKey $iamProfileInfo.SecretAccessKey -SessionTok
en $iamProfileInfo.Token

Leave a Reply

Your email address will not be published. Required fields are marked *