use*_*925 5 powershell amazon-web-services amazon-iam aws-powershell
这更像是一种健全性检查,因为我已经解决了这个问题,但我不相信我已经以聪明的方式做到了.
问题
我有一些实例已经分配了一个允许他们访问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模块的错误,这个模块已经为我解决了这个问题.
然而,我无法摆脱这样的感觉:我错过了 AWS PowerShell 模块中的某些内容,该模块已经为我处理了这个问题。
:) - 您会很高兴听到这确实是开箱即用的,即适用于 Windows PowerShell 的 AWS 工具是基于适用于 .NET 的 AWS 开发工具包构建的,它会自动处理此问题,另请参阅凭证搜索顺序:
当您运行命令时,PowerShell 工具按以下顺序搜索凭据并使用第一个可用的集合。
[...]
6) 如果您在为 IAM 角色配置的 Amazon EC2 实例上运行该命令,请使用实例配置文件中存储的 EC2 实例凭证。
有关对 Amazon EC2 实例使用 IAM 角色的更多信息,请参阅适用于 .NET 的 AWS 开发人员指南。
归档时间: |
|
查看次数: |
1941 次 |
最近记录: |