这更像是一种健全性检查,因为我已经解决了这个问题,但我不相信我已经以聪明的方式做到了.
问题
我有一些实例已经分配了一个允许他们访问S3存储桶的IAM角色.然后我需要运行一些PowerShell脚本来访问S3存储桶以下载一些对象.
解决方案
要获取/设置要使用的凭据,我已编写此PowerShell函数:
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
}
Run Code Online (Sandbox Code Playgroud)
然后,当我需要从AWS模块运行PowerShell cmdlet时,我首先调用此函数.
但是,我无法摆脱我已经错过了AWS PowerShell模块的错误,这个模块已经为我解决了这个问题.