【发布时间】:2012-07-19 00:42:36
【问题描述】:
我正在构建一个堆栈,它需要访问私有 S3 存储桶才能下载我的应用程序的最新版本。我正在使用IAM roles,这是一个相对较新的 AWS 功能,它允许为 EC2 实例分配特定角色,然后与 IAM 策略相结合。不幸的是,这些角色带有在实例化时生成的临时 API 凭证。这并不严重,但它迫使我做这个 cloud-init 脚本之类的事情(简化为相关部分):
#!/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,,但文档不建议它可以与角色一起使用。反正我要试试。
【问题讨论】:
-
boto,一个流行的 python AWS 库,可以优雅地处理这个问题。有关详细信息,请参阅this answer。
标签: amazon-ec2 amazon-web-services amazon-iam amazon-cloudformation cloud-init