我正在構建一個需要訪問私有S3存儲桶的堆棧以下載我的應用程序的最新版本。我使用的是IAM roles,這是一種相對較新的AWS功能,允許爲EC2實例分配特定角色,然後再與IAM策略配合使用。不幸的是,這些角色與實例化時生成的臨時API憑證一起提供。這不是死機,但它強迫我做這樣的事情雲的init腳本(簡化爲相關位):在雲模板中可以使用IAM角色臨時憑證嗎?
#!/bin/sh
# Grab our credentials from the meta-data and parse the response
CREDENTIALS=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/s3access)
S3_ACCESS_KEY=$(echo $CREDENTIALS | ruby -e "require 'rubygems'; require 'json'; puts JSON[STDIN.read]['AccessKeyId'];")
S3_SECRET_KEY=$(echo $CREDENTIALS | ruby -e "require 'rubygems'; require 'json'; puts JSON[STDIN.read]['SecretAccessKey'];")
S3_TOKEN=$(echo $CREDENTIALS | ruby -e "require 'rubygems'; require 'json'; puts JSON[STDIN.read]['Token'];")
# Create an executable script to pull the file
cat <<EOF> /tmp/pullS3.rb
require 'rubygems'
require 'aws-sdk'
AWS.config(
:access_key_id => "$S3_ACCESS_KEY",
:secret_access_key => "$S3_SECRET_KEY",
:session_token => "$S3_TOKEN")
s3 = AWS::S3.new()
myfile = s3.buckets['mybucket'].objects["path/to/my/file"]
File.open("/path/to/save/myfile", "w") do |f|
f.write(myfile.read)
end
EOF
# Downloading the file
ruby /tmp/pullS3.rb
首先:這工作,和工作得很好。儘管如此,我很樂意使用CloudFormation現有的源訪問支持。具體而言,cfn-init
支持使用authentication resources來獲取受保護的數據,包括S3存儲桶。無論如何,從cfn-init
內部獲取這些密鑰,或者將IAM角色綁定到身份驗證資源?
我想一個替代方案是將我的源頭放在其他一些經過驗證的服務之後,但目前這不是一個可行的選擇。
另一個有希望的領導是AWS::IAM::AccessKey resource,,但文檔不建議它可以與角色一起使用。無論如何,我會嘗試。
[伯特](http://boto.readthedocs.org/en/最新/),一個流行的Python AWS庫,可以很好地處理這個問題。有關更多詳細信息,請參閱[此答案](http://stackoverflow.com/a/11130701/877115)。 – Christopher 2013-02-12 15:08:35