可以在云信息模板中使用IAM角色临时凭证吗?

时间:2012-07-19 18:56:12

标签: amazon-ec2 amazon-web-services amazon-iam amazon-cloudformation cloud-init

我正在构建一个需要访问私有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,,但文档并未暗示它可以与角色一起使用。无论如何我都会尝试。

2 个答案:

答案 0 :(得分:11)

我不确定何时添加了支持,但您可以同时使用IAM角色对AWS::CloudFormation::Initfilessources部分的S3下载进行身份验证。

只需使用roleName代替accessKeyId&amp; secretKey(有关详细信息,请参阅AWS::CloudFormation::Authentication),例如:

"Metadata": {
    "AWS::CloudFormation::Init": {
        "download": {
            "files": {
                "/tmp/test.txt": {
                    "source": "http://myBucket.s3.amazonaws.com/test.txt"
                }
             }
        }
    },
    "AWS::CloudFormation::Authentication": {
        "default" : {
            "type": "s3",
            "buckets": [ "myBucket" ],
            "roleName": { "Ref": "myRole" }
        }
    }
}

使用aws-cfn-bootstrap-1.3-11

进行测试

答案 1 :(得分:1)

我设法让这个工作。我使用的是来自此交换的代码: https://forums.aws.amazon.com/message.jspa?messageID=319465

代码不使用IAM策略 - 它使用AWS :: S3 :: BucketPolicy。

Cloud Formation code snippet:

"Resources" : {     

"CfnUser" : {
  "Type" : "AWS::IAM::User",
  "Properties" : {
    "Path": "/",
    "Policies": [{
      "PolicyName": "root",
      "PolicyDocument": { "Statement":[{
        "Effect"   : "Allow",
        "Action"   : [
          "cloudformation:DescribeStackResource",
          "s3:GetObject"
        ],
        "Resource" :"*"
      }]}
    }]
  }
},

"CfnKeys" : {
  "Type" : "AWS::IAM::AccessKey",
  "Properties" : {
    "UserName" : {"Ref": "CfnUser"}
  }
},

"BucketPolicy" : {
  "Type" : "AWS::S3::BucketPolicy",
  "Properties" : {
    "PolicyDocument": {
      "Version"      : "2008-10-17",
      "Id"           : "CfAccessPolicy",
      "Statement"    : [{
        "Sid"        : "ReadAccess",
        "Action"     : ["s3:GetObject"],
        "Effect"     : "Allow",
        "Resource"   : { "Fn::Join" : ["", ["arn:aws:s3:::<MY_BUCKET>/*"]]},
        "Principal"  : { "AWS": {"Fn::GetAtt" : ["CfnUser", "Arn"]} }
      }]
    },
    "Bucket" : "<MY_BUCKET>"
  }
},

"WebServer": {  
  "Type": "AWS::EC2::Instance",
  "DependsOn" : "BucketPolicy",
  "Metadata" : {

    "AWS::CloudFormation::Init" : {
      "config" : {

        "sources" : {
          "/etc/<MY_PATH>" : "https://s3.amazonaws.com/<MY_BUCKET>/<MY_FILE>"
        }

      }
    },

    "AWS::CloudFormation::Authentication" : {
      "S3AccessCreds" : {
        "type" : "S3",
        "accessKeyId" : { "Ref" : "CfnKeys" },
        "secretKey" : {"Fn::GetAtt": ["CfnKeys", "SecretAccessKey"]},
        "buckets" : [ "<MY_BUCKET>" ]
      }
    }
  },

  "Properties": {
    "ImageId" : "<MY_INSTANCE_ID>", 
    "InstanceType" : { "Ref" : "WebServerInstanceType" },
    "KeyName" : {"Ref": "KeyName"},
    "SecurityGroups" : [ "<MY_SECURITY_GROUP>" ],

    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash\n",

      "# Helper function\n",
      "function error_exit\n",
      "{\n",
      "  cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
      "  exit 1\n",
      "}\n",

      "# Install Webserver Packages etc \n",
      "cfn-init -v --region ", { "Ref" : "AWS::Region" },
      "    -s ", { "Ref" : "AWS::StackName" }, " -r WebServer ",
      "         --access-key ", { "Ref" : "CfnKeys" },
      "         --secret-key ", {"Fn::GetAtt": ["CfnKeys", "SecretAccessKey"]}, " || error_exit 'Failed to run cfn-init'\n",

      "# All is well so signal success\n",
      "cfn-signal -e 0 -r \"Setup complete\" '", { "Ref" : "WaitHandle" }, "'\n"

    ]]}}        
  }
}

显然用您的值替换MY_BUCKET,MY_FILE,MY_INSTANCE_ID,MY_SECURITY_GROUP。

相关问题