2013-05-28 42 views
5

我想在我的cloudformation脚本中检索一个文件。如果我公开提供该文件,那么它工作正常。如果该文件是私有文件,则cfn脚本将失败,但在/ var/log /中会出现404错误。试图通过wget检索文件导致适当的403错误。如何访问CFN脚本中的受保护的S3文件?

如何从S3检索私人文件?

我的文件条款的样子:

"files" : { 
     "/etc/httpd/conf/httpd.conf" : { 
     "source" : "https://s3.amazonaws.com/myConfigBucket/httpd.conf" 
     } 
    }, 

我增加了一个认证条款和相应的参数:

"Parameters" : { 
    "BucketRole" : { 
    "Description" : "S3 role for access to bucket", 
    "Type" : "String", 
    "Default" : "S3Access", 
    "ConstraintDescription" : "Must be a valid IAM Role" 
    } 
} 

    "AWS::CloudFormation::Authentication": { 
     "default" : { 
     "type": "s3", 
     "buckets": [ "myConfigBucket" ], 
     "roleName": { "Ref" : "BucketRole" } 
     } 
    }, 

我的IAM角色的样子:

{ 
    "Statement": [ 
    { 
     "Effect": "Allow", 
     "Action": [ 
     "s3:Get*", 
     "s3:List*" 
     ], 
     "Resource": "*" 
    } 
    ] 
} 

回答

5

的解决方案是将IamInstanceProfile属性添加到实例创建中:

"Parameters" : { 
    ... 
    "RoleName" : { 
     "Description" : "IAM Role for access to S3", 
     "Type" : "String", 
     "Default" : "DefaultRoleName", 
     "ConstraintDescription" : "Must be a valid IAM Role" 
    } 
    }, 

    "Resources" : { 
    "InstanceName" : { 
     "Type" : "AWS::EC2::Instance", 
     "Properties" : { 
     "ImageId"    : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "64"] }, 
     "InstanceType"  : { "Ref" : "InstanceType" }, 
     "SecurityGroups"  : [ {"Ref" : "SecurityGroup"} ], 
     "IamInstanceProfile" : { "Ref" : "RoleName" }, 
     "KeyName"    : { "Ref" : "KeyName" } 
     } 
    }, 
    ...