AWS cfn-init是否需要DescribeStackResource的配置文件/角色?

时间:2015-04-01 14:36:50

标签: amazon-web-services amazon-ec2 amazon-cloudformation

来自this页面:

  

要使用AWS CloudFormation引导程序功能,您需要提供AWS凭据   引导脚本。我们强烈建议您在EC2实例上分配IAM角色   实例启动时。

这看起来非常简单,但是当我查看来自AWS文档中所有地方的任何示例时,他们从未为此设置角色或配置文件。例如,here

我错过了什么?是否存在cfn-init需要额外权限而非其他权限的情况?

2 个答案:

答案 0 :(得分:4)

不,您不再需要将Cloudformation:DescribeStackResource添加到与实例配置文件关联的角色的任何策略,以便访问CloudFormation元数据。诸如cfn-get-metadata和cfn-init之类的脚本使用特殊的CFN标头而不是标准AWS Authorization header进行授权。来自CFN脚本的请求如下所示:

Operation not permitted @ chmod_internal - /Users/km/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/.codeclimate.yml

CFN授权标头是http://169.254.169.254/latest/dynamic/instance-identity/documenthttp://169.254.169.254/latest/dynamic/instance-identity/signature的串联,只允许实例从自己的堆栈中查看CloudFormation元数据。

相反,使用实例配置文件的请求如下所示:

# This command succeeds regardless of your instance profile
cfn-get-metadata --region us-west-1 --stack cftest --resource LaunchConfig  --key AWS::CloudFormation::Init

GET /?Action=DescribeStackResource&StackName=cftest&Version=2010-05-15&ContentType=JSON&LogicalResourceId=LaunchConfig HTTP/1.1
Host: cloudformation.us-west-1.amazonaws.com
Connection: keep-alive
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: CFN_V1 ewogICJwcml2YXRlSX(truncated)==:b9ZM3/EnzeX(truncated)=
User-Agent: CloudFormation Tools

实例配置文件临时凭证是从http://169.254.169.254/latest/meta-data/iam/security-credentials/中提取的IAM Roles for EC2

(注意:为了收集这些请求,我运行了# This command fails if you don’t have cloudformation:DescribeStackResource permission! aws cloudformation --region us-west-1 describe-stack-resource --stack-name cftest --logical-resource-id LaunchConfig POST / HTTP/1.1 Host: cloudformation.us-west-1.amazonaws.com Accept-Encoding: identity Content-Length: 95 X-Amz-Date: 20160630T010040Z User-Agent: aws-cli/1.10.43 Python/2.7.11+ Linux/4.4.0-28-generic botocore/1.4.33 X-Amz-Security-Token: FQoDY(truncated-token)= Content-Type: application/x-www-form-urlencoded Authorization: AWS4-HMAC-SHA256 Credential=ASIA(truncated)/20160630/us-west-1/cloudformation/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token, Signature=fbad7aeef75186cb18bbd44810c4d0379d7d1cf1b8a80be14ea1e3192d2ec531 Action=DescribeStackResource&StackName=cftest&Version=2010-05-15&LogicalResourceId=LaunchConfig 并运行了nc -l 80 &cfn-get-metadata --url http://localhost。)

这个CFNSigner功能在aws-cfn-bootstrap-1.1(2012-03)和aws-cfn-bootstrap-1.3.6(2012-09)之间添加到客户端。在2012年之前,您确实需要使用具有云形式的角色:DescribeStackResource权限,如2011年文档Boostrapping Applications With AWS CloudFormation中所述。请注意,只有cfn- *脚本使用CFNSigner;如果你想使用aws --endpoint-url http://localhost,你需要确保你的角色允许它。

答案 1 :(得分:0)

clou-init脚本需要连接到AWS Services才能检索您在CFN模板中提供的元数据。

要连接到AWS服务,您可以直接将访问/密钥传递给cfn-init脚本,或者将具有必要权限的IAM角色附加到EC2实例,以便cfn-init脚本将连接使用IAM角色访问AWS服务。

进一步阅读:http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html

cfn-init提供的所有示例都假定您已将IAM角色附加到实例,因此无需将访问/分配密钥直接传递给cfn-init脚本。