This is more a sanity check because I've solved the problem but I'm unconvinced I've done it the smart way.
The Problem
I have some instances that have been assigned an IAM roles that allow them to access an S3 bucket. I then need to run some PowerShell scripts that will access that S3 bucket to download some objects.
The Solution
To get/set the credentials to use I've written this PowerShell function:
function Set-MyInstanceProfileCredentials {
param(
[parameter()]
[string]
$StoredCredentialsName = "MyInstanceProfileCredentials"
)
$Uri = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
Write-Verbose "Retrieving instance profile from $($Uri)"
$Uri = "$Uri$(Invoke-RestMethod -Uri $Uri)"
Write-Verbose "Retrieving security credentials from $($Uri)"
$Response = Invoke-RestMethod -Uri $Uri
Set-AWSCredentials -AccessKey $Response.AccessKey -SecretKey $Response.SecretAccessKey -StoreAs $StoredCredentialsName
Get-AWSCredentials -StoredCredentials $StoredCredentialsName
}
Then when I need to run a PowerShell cmdlet from the AWS module I just call this function first.
However I can't shake the feeling that I've missed something from the AWS PowerShell module that is already taking care of this for me.